| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Admin Legacy Resource Notice class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Renders a non-dismissible warning notice in the WordPress admin when a |
| 11 |
* Post or Page references a Legacy Form or Legacy Landing Page. |
| 12 |
* |
| 13 |
* @since 3.3.7 |
| 14 |
*/ |
| 15 |
class ConvertKit_Admin_Legacy_Resource_Notice { |
| 16 |
|
| 17 |
/** |
| 18 |
* The Gutenberg notice ID, used both as the deduplication key and as |
| 19 |
* the JS-side selector when tests need to locate the notice. |
| 20 |
* |
| 21 |
* @since 3.3.7 |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
const GUTENBERG_NOTICE_ID = 'convertkit-legacy-resource-warning'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Registers action and filter hooks. |
| 29 |
* |
| 30 |
* @since 3.3.7 |
| 31 |
*/ |
| 32 |
public function __construct() { |
| 33 |
|
| 34 |
add_action( 'admin_notices', array( $this, 'output_classic_editor_notice' ) ); |
| 35 |
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_gutenberg_notice' ) ); |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Outputs the consolidated warning notice on the classic editor's post |
| 41 |
* edit screen. Non-dismissible (no `is-dismissible` class). |
| 42 |
* |
| 43 |
* @since 3.3.7 |
| 44 |
*/ |
| 45 |
public function output_classic_editor_notice() { |
| 46 |
|
| 47 |
// Bail if not on a post edit screen. |
| 48 |
if ( convertkit_get_current_screen( 'base' ) !== 'post' ) { |
| 49 |
return; |
| 50 |
} |
| 51 |
if ( ! in_array( convertkit_get_current_screen( 'post_type' ), convertkit_get_supported_post_types(), true ) ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
// Bail if no post ID is found. |
| 56 |
$post_id = $this->get_current_post_id(); |
| 57 |
if ( ! $post_id ) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
// Get warnings. |
| 62 |
$warnings = $this->get_legacy_warnings_for_post_settings( $post_id ); |
| 63 |
|
| 64 |
// Bail if no warnings are found. |
| 65 |
if ( empty( $warnings ) ) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
// Output warnings. |
| 70 |
?> |
| 71 |
<div class="notice notice-warning"> |
| 72 |
<p> |
| 73 |
<strong><?php esc_html_e( 'Kit', 'convertkit' ); ?>:</strong> |
| 74 |
<?php esc_html_e( 'The Kit settings for this post reference legacy resources. They still work, but should be migrated:', 'convertkit' ); ?> |
| 75 |
</p> |
| 76 |
<ul> |
| 77 |
<?php |
| 78 |
foreach ( $warnings as $warning ) { |
| 79 |
echo '<li>' . esc_html( $warning ) . '</li>'; |
| 80 |
} |
| 81 |
?> |
| 82 |
</ul> |
| 83 |
</div> |
| 84 |
<?php |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Enqueues a small JS handler that renders the same warning inside |
| 90 |
* Gutenberg via wp.data.dispatch('core/notices'). Non-dismissible via |
| 91 |
* the `isDismissible: false` option. |
| 92 |
* |
| 93 |
* @since 3.3.7 |
| 94 |
*/ |
| 95 |
public function enqueue_gutenberg_notice() { |
| 96 |
|
| 97 |
// Bail if not on a post edit screen. |
| 98 |
if ( convertkit_get_current_screen( 'base' ) !== 'post' ) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
// Bail if not on a supported post type. |
| 103 |
if ( ! in_array( convertkit_get_current_screen( 'post_type' ), convertkit_get_supported_post_types(), true ) ) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
// Bail if no post ID is found. |
| 108 |
$post_id = $this->get_current_post_id(); |
| 109 |
if ( ! $post_id ) { |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
// Get warnings. |
| 114 |
$warnings = $this->get_legacy_warnings_for_post_settings( $post_id ); |
| 115 |
|
| 116 |
// Bail if no warnings are found. |
| 117 |
if ( empty( $warnings ) ) { |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
// Enqueue script. |
| 122 |
wp_enqueue_script( |
| 123 |
'convertkit-admin-legacy-resource-notice', |
| 124 |
CONVERTKIT_PLUGIN_URL . 'resources/backend/js/legacy-resource-notice.js', |
| 125 |
array( 'wp-data', 'wp-notices' ), |
| 126 |
CONVERTKIT_PLUGIN_VERSION, |
| 127 |
true |
| 128 |
); |
| 129 |
|
| 130 |
// Localize script. |
| 131 |
wp_localize_script( |
| 132 |
'convertkit-admin-legacy-resource-notice', |
| 133 |
'convertkit_legacy_resource_notice', |
| 134 |
array( |
| 135 |
'id' => self::GUTENBERG_NOTICE_ID, |
| 136 |
'intro' => __( 'Kit: The Kit settings for this post reference legacy resources. They still work, but should be migrated:', 'convertkit' ), |
| 137 |
'warnings' => $warnings, |
| 138 |
) |
| 139 |
); |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Returns an array of warning strings for the given Post, if the Post settings |
| 145 |
* reference a Legacy Form or Legacy Landing Page. |
| 146 |
* |
| 147 |
* @since 3.3.7 |
| 148 |
* |
| 149 |
* @param int $post_id Post ID. |
| 150 |
* @return array |
| 151 |
*/ |
| 152 |
private function get_legacy_warnings_for_post_settings( $post_id ) { |
| 153 |
|
| 154 |
$convertkit_post = new ConvertKit_Post( $post_id ); |
| 155 |
|
| 156 |
return $this->get_legacy_warnings_for_settings( $convertkit_post->get() ); |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Returns an array of warning strings for the given Kit settings array, |
| 162 |
* if the settings reference a Legacy Form or Legacy Landing Page. |
| 163 |
* |
| 164 |
* Public so it can be exercised directly from integration tests without |
| 165 |
* having to create a real Post; the hook handlers use the private |
| 166 |
* post-ID wrapper above. |
| 167 |
* |
| 168 |
* @since 3.3.7 |
| 169 |
* |
| 170 |
* @param array $settings Kit settings array (form, landing_page, restrict_content). |
| 171 |
* @return array |
| 172 |
*/ |
| 173 |
public function get_legacy_warnings_for_settings( $settings ) { |
| 174 |
|
| 175 |
// Get resources. |
| 176 |
$forms = new ConvertKit_Resource_Forms(); |
| 177 |
$landing_pages = new ConvertKit_Resource_Landing_Pages(); |
| 178 |
|
| 179 |
// Initialize warnings array. |
| 180 |
$warnings = array(); |
| 181 |
|
| 182 |
// Form. |
| 183 |
if ( ! empty( $settings['form'] ) && $forms->is_legacy( $settings['form'] ) ) { |
| 184 |
$warnings[] = sprintf( |
| 185 |
/* translators: %s: Form name */ |
| 186 |
__( 'Form: %s', 'convertkit' ), |
| 187 |
$this->get_form_display_name( $settings['form'], $forms ) |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
// Landing Page. |
| 192 |
if ( ! empty( $settings['landing_page'] ) && $landing_pages->is_legacy( $settings['landing_page'] ) ) { |
| 193 |
$warnings[] = sprintf( |
| 194 |
/* translators: %s: Landing page name */ |
| 195 |
__( 'Landing Page: %s', 'convertkit' ), |
| 196 |
$this->get_landing_page_display_name( $settings['landing_page'], $landing_pages ) |
| 197 |
); |
| 198 |
} |
| 199 |
|
| 200 |
// Restrict Content. Value shape is `<type>_<id>` — e.g. `form_123`, |
| 201 |
// `tag_456`, `product_789`. Only form types have a legacy variant. |
| 202 |
if ( ! empty( $settings['restrict_content'] ) && strpos( (string) $settings['restrict_content'], '_' ) !== false ) { |
| 203 |
list( $type, $id ) = explode( '_', (string) $settings['restrict_content'], 2 ); |
| 204 |
$id = (int) $id; |
| 205 |
|
| 206 |
if ( 'form' === $type && $forms->is_legacy( $id ) ) { |
| 207 |
$warnings[] = sprintf( |
| 208 |
/* translators: %s: Form name */ |
| 209 |
__( 'Member Content: %s', 'convertkit' ), |
| 210 |
$this->get_form_display_name( $id, $forms ) |
| 211 |
); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
return $warnings; |
| 216 |
|
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Returns an array of warning strings for the Plugin's General Settings, |
| 221 |
* when one or more Default Form settings reference Legacy Forms. |
| 222 |
* |
| 223 |
* @since 3.3.9 |
| 224 |
* |
| 225 |
* @param array $settings Plugin settings array. |
| 226 |
* @return array |
| 227 |
*/ |
| 228 |
public function get_legacy_warnings_for_plugin_settings( $settings ) { |
| 229 |
|
| 230 |
// Get Forms resource. |
| 231 |
$forms = new ConvertKit_Resource_Forms(); |
| 232 |
|
| 233 |
// Initialize warnings array. |
| 234 |
$warnings = array(); |
| 235 |
|
| 236 |
// Check each supported Post Type's Default Form setting. |
| 237 |
foreach ( convertkit_get_supported_post_types() as $supported_post_type ) { |
| 238 |
// Get Post Type object. |
| 239 |
$post_type = get_post_type_object( $supported_post_type ); |
| 240 |
if ( ! $post_type ) { |
| 241 |
continue; |
| 242 |
} |
| 243 |
|
| 244 |
// Get Default Form setting. |
| 245 |
$setting_key = $supported_post_type . '_form'; |
| 246 |
if ( empty( $settings[ $setting_key ] ) || ! $forms->is_legacy( $settings[ $setting_key ] ) ) { |
| 247 |
continue; |
| 248 |
} |
| 249 |
|
| 250 |
$warnings[] = sprintf( |
| 251 |
/* translators: 1: Post Type name, plural; 2: Form name */ |
| 252 |
__( 'Default Form (%1$s): %2$s', 'convertkit' ), |
| 253 |
$post_type->label, |
| 254 |
$this->get_form_display_name( $settings[ $setting_key ], $forms ) |
| 255 |
); |
| 256 |
} |
| 257 |
|
| 258 |
return $warnings; |
| 259 |
|
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Resolves the form ID to a human-readable "Form Name" string, falling |
| 264 |
* back to "a Legacy Form" when the resource isn't cached. |
| 265 |
* |
| 266 |
* @since 3.3.7 |
| 267 |
* |
| 268 |
* @param int $form_id Form ID. |
| 269 |
* @param ConvertKit_Resource_Forms $forms Forms resource class. |
| 270 |
* @return string |
| 271 |
*/ |
| 272 |
private function get_form_display_name( $form_id, $forms ) { |
| 273 |
|
| 274 |
$form = $forms->get_by_id( (int) $form_id ); |
| 275 |
if ( $form && ! empty( $form['name'] ) ) { |
| 276 |
return sanitize_text_field( $form['name'] ); |
| 277 |
} |
| 278 |
|
| 279 |
return __( 'a Legacy Form', 'convertkit' ); |
| 280 |
|
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Resolves the landing page identifier (numeric ID or URL string) to a |
| 285 |
* human-readable "Landing Page Name" string, falling back to |
| 286 |
* "a Legacy Landing Page" when the resource isn't cached. |
| 287 |
* |
| 288 |
* @since 3.3.7 |
| 289 |
* |
| 290 |
* @param int|string $id_or_url Landing Page ID or URL. |
| 291 |
* @param ConvertKit_Resource_Landing_Pages $landing_pages Landing Pages resource class. |
| 292 |
* @return string |
| 293 |
*/ |
| 294 |
private function get_landing_page_display_name( $id_or_url, $landing_pages ) { |
| 295 |
|
| 296 |
// URL-string legacy assignments (pre-1.9.6) don't have a cached |
| 297 |
// resource entry we can look up by ID, so fall back straight away. |
| 298 |
if ( is_string( $id_or_url ) && strstr( $id_or_url, 'http' ) ) { |
| 299 |
return __( 'a Legacy Landing Page', 'convertkit' ); |
| 300 |
} |
| 301 |
|
| 302 |
$landing_page = $landing_pages->get_by_id( (int) $id_or_url ); |
| 303 |
if ( $landing_page && ! empty( $landing_page['name'] ) ) { |
| 304 |
return sanitize_text_field( $landing_page['name'] ); |
| 305 |
} |
| 306 |
|
| 307 |
return __( 'a Legacy Landing Page', 'convertkit' ); |
| 308 |
|
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Returns the current post ID being edited, or 0 if not on a post edit |
| 313 |
* screen. Reads $_GET['post'] because $post isn't always set in the |
| 314 |
* admin_notices / enqueue_block_editor_assets hook contexts. |
| 315 |
* |
| 316 |
* @since 3.3.7 |
| 317 |
* |
| 318 |
* @return int |
| 319 |
*/ |
| 320 |
private function get_current_post_id() { |
| 321 |
|
| 322 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 323 |
if ( isset( $_GET['post'] ) ) { |
| 324 |
return absint( $_GET['post'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 325 |
} |
| 326 |
|
| 327 |
return 0; |
| 328 |
|
| 329 |
} |
| 330 |
|
| 331 |
} |
| 332 |
|