| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Form Link Block Formatter class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* ConvertKit Form Link Block Formatter class. |
| 11 |
* |
| 12 |
* @package ConvertKit |
| 13 |
* @author ConvertKit |
| 14 |
*/ |
| 15 |
class ConvertKit_Block_Formatter_Form_Link extends ConvertKit_Block_Formatter { |
| 16 |
|
| 17 |
/** |
| 18 |
* Holds the ConvertKit Forms resource class. |
| 19 |
* |
| 20 |
* @since 2.2.0 |
| 21 |
* |
| 22 |
* @var bool|ConvertKit_Resource_Forms |
| 23 |
*/ |
| 24 |
public $forms = false; |
| 25 |
|
| 26 |
/** |
| 27 |
* Holds the Post's content. |
| 28 |
* |
| 29 |
* @since 2.2.0 |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
public $content = ''; |
| 34 |
|
| 35 |
/** |
| 36 |
* Constructor |
| 37 |
* |
| 38 |
* @since 2.2.0 |
| 39 |
*/ |
| 40 |
public function __construct() { |
| 41 |
|
| 42 |
// Register this as a Gutenberg block formatter in the ConvertKit Plugin, |
| 43 |
// if non-inline forms exist on ConvertKit. |
| 44 |
$this->forms = new ConvertKit_Resource_Forms( 'block_formatter_register' ); |
| 45 |
if ( $this->forms->non_inline_exist() ) { |
| 46 |
add_filter( 'convertkit_get_block_formatters', array( $this, 'register' ) ); |
| 47 |
} |
| 48 |
|
| 49 |
// Enqueue JS in footer if links exist in the content that have the formatter applied. |
| 50 |
add_filter( 'the_content', array( $this, 'maybe_enqueue_scripts' ) ); |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Returns this formatter's programmatic name, excluding the convertkit- prefix. |
| 56 |
* |
| 57 |
* @since 2.2.0 |
| 58 |
* |
| 59 |
* @return string |
| 60 |
*/ |
| 61 |
public function get_name() { |
| 62 |
|
| 63 |
return 'form-link'; |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Returns this formatters's Title, Description and Icon |
| 69 |
* |
| 70 |
* @since 2.2.0 |
| 71 |
* |
| 72 |
* @return array |
| 73 |
*/ |
| 74 |
public function get_overview() { |
| 75 |
|
| 76 |
return array( |
| 77 |
'title' => __( 'Kit Form Trigger', 'convertkit' ), |
| 78 |
'description' => __( 'Displays a modal, sticky bar or slide in form to display when the link is pressed.', 'convertkit' ), |
| 79 |
'icon' => 'resources/backend/images/block-icon-formtrigger.svg', |
| 80 |
|
| 81 |
// Gutenberg: Block Icon in Editor. |
| 82 |
'gutenberg_icon' => convertkit_get_file_contents( CONVERTKIT_PLUGIN_PATH . '/resources/backend/images/block-icon-formtrigger.svg' ), |
| 83 |
); |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Returns this formatter's attributes, which are applied |
| 89 |
* to the tag. |
| 90 |
* |
| 91 |
* @since 2.2.0 |
| 92 |
* |
| 93 |
* @return array |
| 94 |
*/ |
| 95 |
public function get_attributes() { |
| 96 |
|
| 97 |
return array( |
| 98 |
'data-id' => '', |
| 99 |
'data-formkit-toggle' => '', |
| 100 |
'href' => '', |
| 101 |
); |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Returns this formatter's fields to display when the formatter |
| 107 |
* button is clicked in the toolbar. |
| 108 |
* |
| 109 |
* @since 2.2.0 |
| 110 |
* |
| 111 |
* @return bool|array |
| 112 |
*/ |
| 113 |
public function get_fields() { |
| 114 |
|
| 115 |
// Bail if the request is not for the WordPress Administration or frontend editor. |
| 116 |
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) { |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
// Get non-inline ConvertKit Forms. |
| 121 |
$forms = array(); |
| 122 |
$forms_data = array(); |
| 123 |
if ( $this->forms->exist() ) { |
| 124 |
foreach ( $this->forms->get_non_inline() as $form ) { |
| 125 |
// Add this form's necessary to the attribute arrays. |
| 126 |
// Legacy forms don't include a `format` key, so define them as inline. |
| 127 |
$forms[ absint( $form['id'] ) ] = sprintf( |
| 128 |
'%s [%s]', |
| 129 |
sanitize_text_field( $form['name'] ), |
| 130 |
( ! empty( $form['format'] ) ? sanitize_text_field( $form['format'] ) : 'inline' ) |
| 131 |
); |
| 132 |
$forms_data[ absint( $form['id'] ) ] = array( |
| 133 |
'data-id' => sanitize_text_field( $form['id'] ), |
| 134 |
'data-formkit-toggle' => sanitize_text_field( $form['uid'] ), |
| 135 |
'href' => $form['embed_url'], |
| 136 |
); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
// Return field. |
| 141 |
return array( |
| 142 |
'data-id' => array( |
| 143 |
'label' => __( 'Form', 'convertkit' ), |
| 144 |
'type' => 'select', |
| 145 |
'description' => __( 'The modal, sticky bar or slide in form to display when the text is clicked.', 'convertkit' ), |
| 146 |
|
| 147 |
// Key/value pairs for the <select> dropdown. |
| 148 |
'values' => $forms, |
| 149 |
|
| 150 |
// Contains all additional data required to build the link. |
| 151 |
'data' => $forms_data, |
| 152 |
), |
| 153 |
); |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Filters the Post / Page's content, enqueuring a form's ConvertKit script where |
| 159 |
* modal, sticky bar or slide in form links have been added by this block formatter |
| 160 |
* in Gutenberg. |
| 161 |
* |
| 162 |
* @since 2.2.0 |
| 163 |
* |
| 164 |
* @param string $content Page/Post Content. |
| 165 |
* @return string Page/Post Content |
| 166 |
*/ |
| 167 |
public function maybe_enqueue_scripts( $content ) { |
| 168 |
|
| 169 |
// Store content in class. |
| 170 |
$this->content = $content; |
| 171 |
|
| 172 |
// Return content, unedited, if no form links exist in the content. |
| 173 |
if ( strpos( $content, 'data-formkit-toggle' ) === false ) { |
| 174 |
return $content; |
| 175 |
} |
| 176 |
|
| 177 |
// Return content, unedited, if no Forms exist. |
| 178 |
if ( ! $this->forms->exist() ) { |
| 179 |
return $content; |
| 180 |
} |
| 181 |
|
| 182 |
// Enqueue scripts. |
| 183 |
$content = preg_replace_callback( |
| 184 |
'#<' . $this->get_tag() . ' data-id="([^"]*)" data-formkit-toggle="([^"]*)".*?href="([^"]*)".*?>([^>]*)</' . $this->get_tag() . '>#i', |
| 185 |
array( $this, 'enqueue_scripts' ), |
| 186 |
$content |
| 187 |
); |
| 188 |
|
| 189 |
// Return. |
| 190 |
return $content; |
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Callback function to enqueue a script to a matching link. |
| 196 |
* |
| 197 |
* @since 2.2.0 |
| 198 |
* |
| 199 |
* @param array $preg_match preg_replace_callback() match. |
| 200 |
* @return string Link with script appended |
| 201 |
*/ |
| 202 |
public function enqueue_scripts( $preg_match ) { |
| 203 |
|
| 204 |
// Get Form by its ID. |
| 205 |
$form = $this->forms->get_by_id( absint( $preg_match[1] ) ); |
| 206 |
|
| 207 |
// Just return the original element, unedited, if the Form could not be found. |
| 208 |
if ( ! $form ) { |
| 209 |
return $preg_match[0]; |
| 210 |
} |
| 211 |
|
| 212 |
// Return the original link, unedited, if the Form doesn't have a UID or JS embed. |
| 213 |
// This prevents issues with legacy modal forms. |
| 214 |
if ( ! array_key_exists( 'uid', $form ) ) { |
| 215 |
return $preg_match[0]; |
| 216 |
} |
| 217 |
if ( ! array_key_exists( 'embed_js', $form ) ) { |
| 218 |
return $preg_match[0]; |
| 219 |
} |
| 220 |
|
| 221 |
// Register the script, so it's only loaded once for this non-inline form across the entire page. |
| 222 |
add_filter( |
| 223 |
'convertkit_output_scripts_footer', |
| 224 |
function ( $scripts ) use ( $form ) { |
| 225 |
|
| 226 |
$scripts[] = array( |
| 227 |
'async' => true, |
| 228 |
'data-uid' => $form['uid'], |
| 229 |
'src' => $form['embed_js'], |
| 230 |
); |
| 231 |
|
| 232 |
return $scripts; |
| 233 |
|
| 234 |
} |
| 235 |
); |
| 236 |
|
| 237 |
// Return original link. |
| 238 |
return $preg_match[0]; |
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
} |
| 243 |
|