| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Forminator Admin Settings class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Registers Forminator Settings that can be edited at Settings > Kit > Forminator. |
| 11 |
* |
| 12 |
* @package ConvertKit |
| 13 |
* @author ConvertKit |
| 14 |
*/ |
| 15 |
class ConvertKit_Forminator_Admin_Section extends ConvertKit_Admin_Section_Base { |
| 16 |
|
| 17 |
/** |
| 18 |
* Constructor |
| 19 |
* |
| 20 |
* @since 2.3.0 |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
|
| 24 |
// Define the class that reads/writes settings. |
| 25 |
$this->settings = new ConvertKit_Forminator_Settings(); |
| 26 |
|
| 27 |
// Define the settings key. |
| 28 |
$this->settings_key = $this->settings::SETTINGS_NAME; |
| 29 |
|
| 30 |
// Define the programmatic name, Title and Tab Text. |
| 31 |
$this->name = 'forminator'; |
| 32 |
$this->title = __( 'Forminator Integration Settings', 'convertkit' ); |
| 33 |
$this->tab_text = __( 'Forminator', 'convertkit' ); |
| 34 |
|
| 35 |
// Define settings sections. |
| 36 |
$this->settings_sections = array( |
| 37 |
'general' => array( |
| 38 |
'title' => $this->title, |
| 39 |
'callback' => array( $this, 'print_section_info' ), |
| 40 |
'wrap' => false, |
| 41 |
), |
| 42 |
); |
| 43 |
|
| 44 |
parent::__construct(); |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Prints help info for this section. |
| 50 |
* |
| 51 |
* @since 2.3.0 |
| 52 |
*/ |
| 53 |
public function print_section_info() { |
| 54 |
|
| 55 |
?> |
| 56 |
<p> |
| 57 |
<?php |
| 58 |
esc_html_e( 'Kit seamlessly integrates with Forminator to let you add subscribers using Forminator forms.', 'convertkit' ); |
| 59 |
?> |
| 60 |
</p> |
| 61 |
<p> |
| 62 |
<?php |
| 63 |
esc_html_e( 'The Forminator form must have Name and Email fields. These fields will be sent to Kit for the subscription', 'convertkit' ); |
| 64 |
?> |
| 65 |
</p> |
| 66 |
<p> |
| 67 |
<?php esc_html_e( 'Each Forminator form and quiz has the following Kit options:', 'convertkit' ); ?> |
| 68 |
<br /> |
| 69 |
<code><?php esc_html_e( 'Do not subscribe', 'convertkit' ); ?></code>: <?php esc_html_e( 'Do not subscribe the email address to Kit', 'convertkit' ); ?> |
| 70 |
<br /> |
| 71 |
<code><?php esc_html_e( 'Subscribe', 'convertkit' ); ?></code>: <?php esc_html_e( 'Subscribes the email address to Kit', 'convertkit' ); ?> |
| 72 |
<br /> |
| 73 |
<code><?php esc_html_e( 'Form', 'convertkit' ); ?></code>: <?php esc_html_e( 'Subscribes the email address to Kit, and adds the subscriber to the Kit form', 'convertkit' ); ?> |
| 74 |
<br /> |
| 75 |
<code><?php esc_html_e( 'Tag', 'convertkit' ); ?></code>: <?php esc_html_e( 'Subscribes the email address to Kit, tagging the subscriber', 'convertkit' ); ?> |
| 76 |
<br /> |
| 77 |
<code><?php esc_html_e( 'Sequence', 'convertkit' ); ?></code>: <?php esc_html_e( 'Subscribes the email address to Kit, and adds the subscriber to the Kit sequence', 'convertkit' ); ?> |
| 78 |
</p> |
| 79 |
<?php |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Returns the URL for the ConvertKit documentation for this setting section. |
| 85 |
* |
| 86 |
* @since 2.3.0 |
| 87 |
* |
| 88 |
* @return string Documentation URL. |
| 89 |
*/ |
| 90 |
public function documentation_url() { |
| 91 |
|
| 92 |
return 'https://help.kit.com/en/articles/2502591-how-to-set-up-the-kit-plugin-on-your-wordpress-website'; |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Outputs the section as a WP_List_Table of Forminator Forms, with options to choose |
| 98 |
* a ConvertKit Form mapping for each. |
| 99 |
* |
| 100 |
* @since 2.3.0 |
| 101 |
*/ |
| 102 |
public function render() { |
| 103 |
|
| 104 |
// Render opening container. |
| 105 |
$this->render_container_start(); |
| 106 |
|
| 107 |
do_settings_sections( $this->settings_key ); |
| 108 |
|
| 109 |
// Get Forminator Forms. |
| 110 |
$forminator_forms = $this->get_forminator_forms(); |
| 111 |
|
| 112 |
// Bail with an error if no Forminator Forms exist. |
| 113 |
if ( ! $forminator_forms ) { |
| 114 |
$this->output_error( __( 'No Forminator Forms or Quizzes exist in the Forminator Plugin.', 'convertkit' ) ); |
| 115 |
$this->render_container_end(); |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
// Get Creator Network Recommendations script. |
| 120 |
$creator_network_recommendations = new ConvertKit_Resource_Creator_Network_Recommendations( 'forminator' ); |
| 121 |
$creator_network_recommendations_enabled = $creator_network_recommendations->enabled(); |
| 122 |
|
| 123 |
// Setup WP_List_Table. |
| 124 |
$table = new ConvertKit_WP_List_Table(); |
| 125 |
$table->add_column( 'title', __( 'Forminator Form', 'convertkit' ), true ); |
| 126 |
$table->add_column( 'form', __( 'Kit', 'convertkit' ), false ); |
| 127 |
$table->add_column( 'creator_network_recommendations', __( 'Enable Creator Network Recommendations', 'convertkit' ), false ); |
| 128 |
|
| 129 |
// Iterate through Forminator Forms, building table array. |
| 130 |
$table_rows = array(); |
| 131 |
foreach ( $forminator_forms as $forminator_form ) { |
| 132 |
// Build row. |
| 133 |
$table_row = array( |
| 134 |
'title' => $forminator_form['name'], |
| 135 |
'form' => convertkit_get_subscription_dropdown_field( |
| 136 |
'_wp_convertkit_integration_forminator_settings[' . $forminator_form['id'] . ']', |
| 137 |
(string) $this->settings->get_convertkit_subscribe_setting_by_forminator_form_id( $forminator_form['id'] ), |
| 138 |
'_wp_convertkit_integration_forminator_settings_' . $forminator_form['id'], |
| 139 |
'widefat', |
| 140 |
'forminator' |
| 141 |
), |
| 142 |
); |
| 143 |
|
| 144 |
// Add Creator Network Recommendations table column. |
| 145 |
if ( $creator_network_recommendations_enabled ) { |
| 146 |
// Show checkbox to enable Creator Network Recommendations for this Forminator Form. |
| 147 |
$table_row['creator_network_recommendations'] = $this->get_checkbox_field( |
| 148 |
'creator_network_recommendations_' . $forminator_form['id'], |
| 149 |
'1', |
| 150 |
$this->settings->get_creator_network_recommendations_enabled_by_forminator_form_id( $forminator_form['id'] ) |
| 151 |
); |
| 152 |
} else { |
| 153 |
// Show a link to the ConvertKit billing page, as a paid plan is required for Creator Network Recommendations. |
| 154 |
$table_row['creator_network_recommendations'] = sprintf( |
| 155 |
'%s <a href="%s" target="_blank">%s</a>', |
| 156 |
esc_html__( 'Creator Network Recommendations requires a', 'convertkit' ), |
| 157 |
convertkit_get_billing_url(), |
| 158 |
esc_html__( 'paid Kit Plan', 'convertkit' ) |
| 159 |
); |
| 160 |
} |
| 161 |
|
| 162 |
// Add row to table of settings. |
| 163 |
$table_rows[] = $table_row; |
| 164 |
} |
| 165 |
|
| 166 |
// Sort table rows. |
| 167 |
$table_rows = $table->reorder( $table_rows ); |
| 168 |
|
| 169 |
// Set items. |
| 170 |
$table->add_items( $table_rows ); |
| 171 |
$table->set_total_items( count( $table_rows ) ); |
| 172 |
|
| 173 |
// Prepare and display WP_List_Table. |
| 174 |
$table->prepare_items(); |
| 175 |
$table->display(); |
| 176 |
|
| 177 |
// Register settings field. |
| 178 |
settings_fields( $this->settings_key ); |
| 179 |
|
| 180 |
// Render closing container. |
| 181 |
$this->render_container_end(); |
| 182 |
|
| 183 |
// Render submit button. |
| 184 |
submit_button(); |
| 185 |
|
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Gets available forms from Forminator |
| 190 |
* |
| 191 |
* @since 2.3.0 |
| 192 |
* |
| 193 |
* @return bool|array |
| 194 |
*/ |
| 195 |
private function get_forminator_forms() { |
| 196 |
|
| 197 |
$forms = array(); |
| 198 |
|
| 199 |
// Get all forms using Forminator API class. |
| 200 |
foreach ( Forminator_API::get_forms( null, 1, -1 ) as $forminator_form ) { |
| 201 |
$forms[] = array( |
| 202 |
'id' => $forminator_form->id, |
| 203 |
'name' => sprintf( |
| 204 |
'%s: %s', |
| 205 |
esc_html__( 'Form', 'convertkit' ), |
| 206 |
$forminator_form->name |
| 207 |
), |
| 208 |
); |
| 209 |
} |
| 210 |
|
| 211 |
foreach ( Forminator_API::get_quizzes( null, 1, -1 ) as $forminator_form ) { |
| 212 |
$forms[] = array( |
| 213 |
'id' => $forminator_form->id, |
| 214 |
'name' => sprintf( |
| 215 |
'%s: %s', |
| 216 |
esc_html__( 'Quiz', 'convertkit' ), |
| 217 |
$forminator_form->name |
| 218 |
), |
| 219 |
); |
| 220 |
} |
| 221 |
|
| 222 |
// If no Forms or Quizzes were found in Forminator, return false. |
| 223 |
if ( ! count( $forms ) ) { |
| 224 |
return false; |
| 225 |
} |
| 226 |
|
| 227 |
return $forms; |
| 228 |
|
| 229 |
} |
| 230 |
|
| 231 |
} |
| 232 |
|
| 233 |
// Register Admin Settings section. |
| 234 |
add_filter( |
| 235 |
'convertkit_admin_settings_register_sections', |
| 236 |
/** |
| 237 |
* Register Forminator as a settings section at Settings > Kit. |
| 238 |
* |
| 239 |
* @param array $sections Settings Sections. |
| 240 |
* @return array |
| 241 |
*/ |
| 242 |
function ( $sections ) { |
| 243 |
|
| 244 |
// Bail if Forminator isn't enabled. |
| 245 |
if ( ! defined( 'FORMINATOR_VERSION' ) ) { |
| 246 |
return $sections; |
| 247 |
} |
| 248 |
|
| 249 |
// Register this class as a section at Settings > Kit. |
| 250 |
$sections['forminator'] = new ConvertKit_Forminator_Admin_Section(); |
| 251 |
return $sections; |
| 252 |
|
| 253 |
} |
| 254 |
); |
| 255 |
|