| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Admin Importer Kit Legacy Forms class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Import and migrate data from Kit Legacy Forms to Kit. |
| 11 |
* |
| 12 |
* @package ConvertKit |
| 13 |
* @author ConvertKit |
| 14 |
*/ |
| 15 |
class ConvertKit_Admin_Importer_ConvertKit_Legacy_Forms extends ConvertKit_Admin_Importer { |
| 16 |
|
| 17 |
/** |
| 18 |
* Holds the programmatic name of the importer (lowercase, no spaces). |
| 19 |
* |
| 20 |
* @since 3.3.5 |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
public $name = 'convertkit_legacy_forms'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Holds the title of the importer (for display in the importer list). |
| 28 |
* |
| 29 |
* @since 3.3.5 |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
public $title = 'Kit Legacy Forms'; |
| 34 |
|
| 35 |
/** |
| 36 |
* Holds the shortcode name for ConvertKit Legacy Forms. |
| 37 |
* |
| 38 |
* @since 3.3.5 |
| 39 |
* |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
public $shortcode_name = 'convertkit_form'; |
| 43 |
|
| 44 |
/** |
| 45 |
* Holds the ID attribute names for Kit Legacy Form shortcodes. |
| 46 |
* |
| 47 |
* Both `form` and `id` are matched because Kit's `[convertkit_form]` |
| 48 |
* shortcode has supported both attribute names historically. |
| 49 |
* |
| 50 |
* @since 3.3.5 |
| 51 |
* |
| 52 |
* @var array |
| 53 |
*/ |
| 54 |
public $shortcode_id_attribute = array( 'form', 'id' ); |
| 55 |
|
| 56 |
/** |
| 57 |
* Holds the block name for ConvertKit Legacy Forms. |
| 58 |
* |
| 59 |
* @since 3.3.5 |
| 60 |
* |
| 61 |
* @var string |
| 62 |
*/ |
| 63 |
public $block_name = 'convertkit/form'; |
| 64 |
|
| 65 |
/** |
| 66 |
* Holds the ID attribute names for Kit Legacy Form blocks. |
| 67 |
* |
| 68 |
* Both `form` and `id` are matched because Kit's form block has supported |
| 69 |
* both attribute names historically. |
| 70 |
* |
| 71 |
* @since 3.3.5 |
| 72 |
* |
| 73 |
* @var array |
| 74 |
*/ |
| 75 |
public $block_id_attribute = array( 'form', 'id' ); |
| 76 |
|
| 77 |
/** |
| 78 |
* Constructor |
| 79 |
* |
| 80 |
* @since 3.3.5 |
| 81 |
*/ |
| 82 |
public function __construct() { |
| 83 |
|
| 84 |
// Define a custom description for this importer. |
| 85 |
$this->description = __( 'Kit Legacy Forms are being phased out. Use this tool to replace Kit Form shortcodes and blocks using a Legacy Form with a new Kit Form.', 'convertkit' ); |
| 86 |
|
| 87 |
// Register this as an importer, if ConvertKit Legacy Forms exist. |
| 88 |
add_filter( 'convertkit_get_form_importers', array( $this, 'register' ) ); |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Returns an array of Kit Legacy Forms form IDs and titles. |
| 94 |
* |
| 95 |
* @since 3.3.5 |
| 96 |
* |
| 97 |
* @return array |
| 98 |
*/ |
| 99 |
public function get_forms() { |
| 100 |
|
| 101 |
// Query resource class to fetch legacy forms. |
| 102 |
$forms = array(); |
| 103 |
$convertkit_forms = new ConvertKit_Resource_Forms( 'settings' ); |
| 104 |
if ( $convertkit_forms->exist() ) { |
| 105 |
foreach ( $convertkit_forms->get() as $form ) { |
| 106 |
// Skip if not a Legacy Form. |
| 107 |
if ( ! $convertkit_forms->is_legacy( $form['id'] ) ) { |
| 108 |
continue; |
| 109 |
} |
| 110 |
|
| 111 |
$forms[ $form['id'] ] = $form['name']; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
return $forms; |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Returns an array of legacy Kit form IDs (as strings) for use in |
| 121 |
* filtering shortcodes/blocks that reference them. |
| 122 |
* |
| 123 |
* @since 3.3.5 |
| 124 |
* |
| 125 |
* @return array |
| 126 |
*/ |
| 127 |
private function get_legacy_form_ids() { |
| 128 |
|
| 129 |
$legacy_ids = array(); |
| 130 |
$convertkit_forms = new ConvertKit_Resource_Forms( 'settings' ); |
| 131 |
|
| 132 |
if ( ! $convertkit_forms->exist() ) { |
| 133 |
return $legacy_ids; |
| 134 |
} |
| 135 |
|
| 136 |
foreach ( $convertkit_forms->get() as $form ) { |
| 137 |
if ( ! $convertkit_forms->is_legacy( $form['id'] ) ) { |
| 138 |
continue; |
| 139 |
} |
| 140 |
$legacy_ids[] = (string) $form['id']; |
| 141 |
} |
| 142 |
|
| 143 |
return $legacy_ids; |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Overrides the parent method to: |
| 149 |
* - return form IDs found in both shortcodes AND blocks (the parent only |
| 150 |
* handles shortcodes), and |
| 151 |
* - filter the result so only legacy form IDs are returned. |
| 152 |
* |
| 153 |
* @since 3.3.5 |
| 154 |
* |
| 155 |
* @param string $content Content containing Kit Form shortcodes / blocks. |
| 156 |
* @return array |
| 157 |
*/ |
| 158 |
public function get_form_ids_from_content( $content ) { |
| 159 |
|
| 160 |
// Get shortcode-derived form IDs from the parent. |
| 161 |
$shortcode_ids = parent::get_form_ids_from_content( $content ); |
| 162 |
|
| 163 |
// Get block-derived form IDs (parent only handles shortcodes). |
| 164 |
$block_ids = $this->get_block_form_ids_from_content( $content ); |
| 165 |
|
| 166 |
// Combine and filter to legacy form IDs only. |
| 167 |
$all_ids = array_unique( array_merge( $shortcode_ids, $block_ids ) ); |
| 168 |
$legacy_ids = $this->get_legacy_form_ids(); |
| 169 |
|
| 170 |
// Cast both sides to strings for safe comparison. |
| 171 |
$all_ids = array_map( 'strval', $all_ids ); |
| 172 |
|
| 173 |
return array_values( array_intersect( $all_ids, $legacy_ids ) ); |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Returns an array of form IDs from convertkit/form blocks in the given |
| 179 |
* content. Walks innerBlocks recursively. |
| 180 |
* |
| 181 |
* @since 3.3.5 |
| 182 |
* |
| 183 |
* @param string $content Content containing Kit Form blocks. |
| 184 |
* @return array |
| 185 |
*/ |
| 186 |
private function get_block_form_ids_from_content( $content ) { |
| 187 |
|
| 188 |
return $this->extract_block_form_ids( parse_blocks( $content ) ); |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Recursively walks blocks (and innerBlocks) and returns an array of form |
| 194 |
* IDs from any convertkit/form block's `form` attribute. |
| 195 |
* |
| 196 |
* @since 3.3.5 |
| 197 |
* |
| 198 |
* @param array $blocks Blocks. |
| 199 |
* @return array |
| 200 |
*/ |
| 201 |
private function extract_block_form_ids( $blocks ) { |
| 202 |
|
| 203 |
$form_ids = array(); |
| 204 |
|
| 205 |
// Normalise the block ID attribute(s) to an array so we can match |
| 206 |
// against multiple attribute names (e.g. both `form` and `id`). |
| 207 |
$id_attributes = (array) $this->block_id_attribute; |
| 208 |
|
| 209 |
foreach ( $blocks as $block ) { |
| 210 |
if ( ! empty( $block['innerBlocks'] ) ) { |
| 211 |
$form_ids = array_merge( |
| 212 |
$form_ids, |
| 213 |
$this->extract_block_form_ids( $block['innerBlocks'] ) |
| 214 |
); |
| 215 |
} |
| 216 |
|
| 217 |
if ( $block['blockName'] !== $this->block_name ) { |
| 218 |
continue; |
| 219 |
} |
| 220 |
|
| 221 |
// Record the form ID from the first matching attribute. If a block |
| 222 |
// somehow has both `form` and `id` set, they'd be the same value, so |
| 223 |
// we stop after the first match to avoid double-counting. |
| 224 |
foreach ( $id_attributes as $id_attribute ) { |
| 225 |
if ( empty( $block['attrs'][ $id_attribute ] ) ) { |
| 226 |
continue; |
| 227 |
} |
| 228 |
$form_ids[] = (string) $block['attrs'][ $id_attribute ]; |
| 229 |
break; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
return $form_ids; |
| 234 |
|
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Overrides the parent method to only return post IDs whose content |
| 239 |
* contains a Kit Form shortcode or block referencing a legacy form ID. |
| 240 |
* |
| 241 |
* The parent's broad SQL match returns any post containing a |
| 242 |
* `[convertkit_form` shortcode or `<!-- wp:convertkit/form` block, which |
| 243 |
* for the Kit Legacy Forms importer is too broad: non-legacy uses of these |
| 244 |
* shortcodes/blocks are valid and should not appear in the importer UI. |
| 245 |
* |
| 246 |
* @since 3.3.5 |
| 247 |
* |
| 248 |
* @return array |
| 249 |
*/ |
| 250 |
public function get_forms_in_posts() { |
| 251 |
|
| 252 |
$candidate_post_ids = parent::get_forms_in_posts(); |
| 253 |
|
| 254 |
if ( empty( $candidate_post_ids ) ) { |
| 255 |
return array(); |
| 256 |
} |
| 257 |
|
| 258 |
$legacy_ids = $this->get_legacy_form_ids(); |
| 259 |
if ( empty( $legacy_ids ) ) { |
| 260 |
return array(); |
| 261 |
} |
| 262 |
|
| 263 |
$matched_post_ids = array(); |
| 264 |
foreach ( $candidate_post_ids as $post_id ) { |
| 265 |
$content = get_post_field( 'post_content', $post_id ); |
| 266 |
|
| 267 |
// get_form_ids_from_content() (overridden above) returns only legacy IDs |
| 268 |
// from both shortcodes and blocks, so any non-empty result means this |
| 269 |
// post contains at least one legacy form reference. |
| 270 |
$legacy_form_ids_in_content = $this->get_form_ids_from_content( $content ); |
| 271 |
if ( ! empty( $legacy_form_ids_in_content ) ) { |
| 272 |
$matched_post_ids[] = $post_id; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
return $matched_post_ids; |
| 277 |
|
| 278 |
} |
| 279 |
|
| 280 |
} |
| 281 |
|