Skip to content

Instantly share code, notes, and snippets.

@pederan
Created January 31, 2018 20:50
Show Gist options
  • Save pederan/0def9cd720bb7b76d91aa4b5d1651998 to your computer and use it in GitHub Desktop.
Save pederan/0def9cd720bb7b76d91aa4b5d1651998 to your computer and use it in GitHub Desktop.
class-mailpoet-hogan-form-provider.php
<?php
/**
* MailPoet Form Provider class for Hogan Form
*
*/
declare( strict_types = 1 );
namespace Dekode\Hogan;
use \MailPoet\Models;
use \MailPoet\Form;
if ( ! \interface_exists( '\\Dekode\\Hogan\\Form_Provider' ) ) {
return;
}
class MailPoet_Hogan_Form_Provider implements \Dekode\Hogan\Form_Provider {
/**
* Get provider full name, i.e. "Gravity Forms"
*
* @return string Provider name
*/
public function get_name() : string {
return 'MailPoet Forms';
}
/**
* Get provider identifier, i.e. "gf"
*
* @return string Provider indentifier
*/
public function get_identifier() : string {
return 'mp';
}
/**
* Get provider forms
*
* @return array Forms as array with identifier and form id as key and form title as value, i.e. [ 'gf-1', 'Form Title' ]
*/
public function get_forms() : array {
$array = [];
$forms = Models\Form::getPublished()->orderByAsc( 'name' )->findArray();
foreach ( $forms as $form ) {
$array[ $this->get_identifier() . '-' . $form['id'] ] = $form['name'];
}
return $array;
}
/**
* Get rendered form HTML
*
* @param int $id Form ID.
*
* @return string Form HTML
*/
public function get_form_html( int $id ) : string {
$form_widget = new Form\Widget();
return $form_widget->widget( [
'form' => $id,
'form_type' => 'php',
] );
}
/**
* Finds whether a provider is enabled
*
* @return bool Returns TRUE if provider is enabled, FALSE otherwise.
*/
public function enabled() : bool {
// https://codex.wordpress.org/Function_Reference/is_plugin_active .
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
return function_exists( 'is_plugin_active' ) &&
\is_plugin_active( 'mailpoet/mailpoet.php' ) &&
apply_filters( 'hogan/module/form/mailpoet/enabled', true );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment