| @@ -49,40 +49,14 @@ | ||
| 49 | 49 | $context |
| 50 | 50 | ); |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | - // Get last query time and existing resources. | |
| 54 | - $this->last_queried = get_option( $this->settings_name . '_last_queried' ); | |
| 55 | - $this->resources = get_option( $this->settings_name ); | |
| 53 | + // Call parent initialization function. | |
| 54 | + parent::init(); | |
| 56 | 55 | |
| 57 | 56 | } |
| 58 | 57 | |
| 59 | 58 | /** |
| 60 | - * Fetches resources (forms, landing pages or tags) from the API, storing them in the options table | |
| 61 | - * with a last queried timestamp. | |
| 62 | - * | |
| 63 | - * If the refresh results in a 401, removes the access and refresh tokens from the settings. | |
| 64 | - * | |
| 65 | - * @since 3.1.2 | |
| 66 | - * | |
| 67 | - * @return WP_Error|array | |
| 68 | - */ | |
| 69 | - public function refresh() { | |
| 70 | - | |
| 71 | - // Call parent refresh method. | |
| 72 | - $result = parent::refresh(); | |
| 73 | - | |
| 74 | - // If an error occurred, maybe delete credentials from the Plugin's settings | |
| 75 | - // if the error is a 401 unauthorized. | |
| 76 | - if ( is_wp_error( $result ) ) { | |
| 77 | - convertkit_maybe_delete_credentials( $result, CONVERTKIT_OAUTH_CLIENT_ID ); | |
| 78 | - } | |
| 79 | - | |
| 80 | - return $result; | |
| 81 | - | |
| 82 | - } | |
| 83 | - | |
| 84 | - /** | |
| 85 | 59 | * Returns all inline forms based on the sort order. |
| 86 | 60 | * |
| 87 | 61 | * @since 2.7.3 |
| 88 | 62 | * |
| @@ -153,44 +127,8 @@ | ||
| 153 | 127 | |
| 154 | 128 | } |
| 155 | 129 | |
| 156 | 130 | /** |
| 157 | - * Returns all non-legacy forms (any v4 format: inline, modal, slide in | |
| 158 | - * or sticky bar) based on the sort order. Legacy forms lack a `format` | |
| 159 | - * key and are therefore excluded. | |
| 160 | - * | |
| 161 | - * @since 3.3.7 | |
| 162 | - * | |
| 163 | - * @return bool|array | |
| 164 | - */ | |
| 165 | - public function get_non_legacy() { | |
| 166 | - | |
| 167 | - // If the ConvertKit WordPress Libraries are < 1.3.6 (e.g. loaded by an outdated | |
| 168 | - // addon), or a WordPress site updates this Plugin before other ConvertKit Plugins, | |
| 169 | - // get_by() won't be available and will cause an E_ERROR, crashing the site. | |
| 170 | - // @see https://wordpress.org/support/topic/error-1795/. | |
| 171 | - if ( ! method_exists( $this, 'get_by' ) ) { // @phpstan-ignore-line Older WordPress Libraries won't have this function. | |
| 172 | - return false; | |
| 173 | - } | |
| 174 | - | |
| 175 | - return $this->get_by( 'format', array( 'inline', 'modal', 'slide in', 'sticky bar' ) ); | |
| 176 | - | |
| 177 | - } | |
| 178 | - | |
| 179 | - /** | |
| 180 | - * Returns whether any non-legacy forms exist in the options table. | |
| 181 | - * | |
| 182 | - * @since 3.3.7 | |
| 183 | - * | |
| 184 | - * @return bool | |
| 185 | - */ | |
| 186 | - public function non_legacy_exist() { | |
| 187 | - | |
| 188 | - return (bool) $this->get_non_legacy(); | |
| 189 | - | |
| 190 | - } | |
| 191 | - | |
| 192 | - /** | |
| 193 | 131 | * Determines if the given Form ID is a legacy Form or Landing Page. |
| 194 | 132 | * |
| 195 | 133 | * @since 2.5.0 |
| 196 | 134 | * |
| @@ -231,9 +169,9 @@ | ||
| 231 | 169 | */ |
| 232 | 170 | public function get_select_field_all( $name, $id, $css_classes, $selected_option, $prepend_options = false, $attributes = false, $description = false ) { |
| 233 | 171 | |
| 234 | 172 | return $this->get_select_field( |
| 235 | - $this->get_forms_for_select_field( $selected_option ), | |
| 173 | + $this->get(), | |
| 236 | 174 | $name, |
| 237 | 175 | $id, |
| 238 | 176 | $css_classes, |
| 239 | 177 | $selected_option, |
| @@ -244,67 +182,8 @@ | ||
| 244 | 182 | |
| 245 | 183 | } |
| 246 | 184 | |
| 247 | 185 | /** |
| 248 | - * Outputs a <select> field populated with all forms, based on the given parameters. | |
| 249 | - * | |
| 250 | - * @since 2.8.5 | |
| 251 | - * | |
| 252 | - * @param string $name Name. | |
| 253 | - * @param string $id ID. | |
| 254 | - * @param bool|array $css_classes <select> CSS class(es). | |
| 255 | - * @param string $selected_option <option> value to mark as selected. | |
| 256 | - * @param bool|array $prepend_options <option> elements to prepend before resources. | |
| 257 | - * @param bool|array $attributes <select> attributes. | |
| 258 | - * @param bool|string|array $description Description. | |
| 259 | - */ | |
| 260 | - public function output_select_field_all( $name, $id, $css_classes, $selected_option, $prepend_options = false, $attributes = false, $description = false ) { | |
| 261 | - | |
| 262 | - $this->output_select_field( | |
| 263 | - $this->get_forms_for_select_field( $selected_option ), | |
| 264 | - $name, | |
| 265 | - $id, | |
| 266 | - $css_classes, | |
| 267 | - $selected_option, | |
| 268 | - $prepend_options, | |
| 269 | - $attributes, | |
| 270 | - $description | |
| 271 | - ); | |
| 272 | - | |
| 273 | - } | |
| 274 | - | |
| 275 | - /** | |
| 276 | - * Returns the array of forms to display in an "all forms" dropdown: every | |
| 277 | - * non-legacy form, plus the currently-selected legacy form (if any) so | |
| 278 | - * existing saved legacy assignments continue to render as selected in the | |
| 279 | - * UI without exposing other legacy forms as new selection options. | |
| 280 | - * | |
| 281 | - * @since 3.3.7 | |
| 282 | - * | |
| 283 | - * @param string|int $selected_option Currently-selected form ID. | |
| 284 | - * @return array | |
| 285 | - */ | |
| 286 | - private function get_forms_for_select_field( $selected_option ) { | |
| 287 | - | |
| 288 | - $forms = $this->get_non_legacy(); | |
| 289 | - if ( ! is_array( $forms ) ) { | |
| 290 | - $forms = array(); | |
| 291 | - } | |
| 292 | - | |
| 293 | - // If the currently-selected form is a legacy form, append it so the | |
| 294 | - // dropdown shows the saved value as selected. | |
| 295 | - if ( $selected_option && $this->is_legacy( $selected_option ) ) { | |
| 296 | - $legacy_form = $this->get_by_id( (int) $selected_option ); | |
| 297 | - if ( $legacy_form ) { | |
| 298 | - $forms[] = $legacy_form; | |
| 299 | - } | |
| 300 | - } | |
| 301 | - | |
| 302 | - return $forms; | |
| 303 | - | |
| 304 | - } | |
| 305 | - | |
| 306 | - /** | |
| 307 | 186 | * Returns a <select> field populated with all non-inline forms, based on the given parameters. |
| 308 | 187 | * |
| 309 | 188 | * @since 2.3.9 |
| 310 | 189 | * |
| @@ -332,38 +211,8 @@ | ||
| 332 | 211 | |
| 333 | 212 | } |
| 334 | 213 | |
| 335 | 214 | /** |
| 336 | - * Outputs a <select> field populated with all non-inline forms, based on the given parameters. | |
| 337 | - * | |
| 338 | - * @since 2.3.9 | |
| 339 | - * | |
| 340 | - * @param string $name Name. | |
| 341 | - * @param string $id ID. | |
| 342 | - * @param bool|array $css_classes <select> CSS class(es). | |
| 343 | - * @param array $selected_options <option> values to mark as selected. | |
| 344 | - * @param bool|array $prepend_options <option> elements to prepend before resources. | |
| 345 | - * @param bool|array $attributes <select> attributes. | |
| 346 | - * @param bool|string|array $description Description. | |
| 347 | - */ | |
| 348 | - public function output_select_field_non_inline( $name, $id, $css_classes, $selected_options, $prepend_options = false, $attributes = false, $description = false ) { | |
| 349 | - | |
| 350 | - echo wp_kses( | |
| 351 | - $this->get_select_field_non_inline( | |
| 352 | - $name, | |
| 353 | - $id, | |
| 354 | - $css_classes, | |
| 355 | - $selected_options, | |
| 356 | - $prepend_options, | |
| 357 | - $attributes, | |
| 358 | - $description | |
| 359 | - ), | |
| 360 | - convertkit_kses_allowed_html() | |
| 361 | - ); | |
| 362 | - | |
| 363 | - } | |
| 364 | - | |
| 365 | - /** | |
| 366 | 215 | * Returns a <select> field populated with the resources, based on the given parameters. |
| 367 | 216 | * |
| 368 | 217 | * @since 2.3.9 |
| 369 | 218 | * |
| @@ -444,40 +293,8 @@ | ||
| 444 | 293 | |
| 445 | 294 | } |
| 446 | 295 | |
| 447 | 296 | /** |
| 448 | - * Outputs a <select> field populated with the resources, based on the given parameters. | |
| 449 | - * | |
| 450 | - * @since 2.8.5 | |
| 451 | - * | |
| 452 | - * @param array $forms Forms. | |
| 453 | - * @param string $name Name. | |
| 454 | - * @param string $id ID. | |
| 455 | - * @param bool|array $css_classes <select> CSS class(es). | |
| 456 | - * @param string $selected_option <option> value to mark as selected. | |
| 457 | - * @param bool|array $prepend_options <option> elements to prepend before resources. | |
| 458 | - * @param bool|array $attributes <select> attributes. | |
| 459 | - * @param bool|string|array $description Description. | |
| 460 | - */ | |
| 461 | - private function output_select_field( $forms, $name, $id, $css_classes, $selected_option, $prepend_options = false, $attributes = false, $description = false ) { | |
| 462 | - | |
| 463 | - echo wp_kses( | |
| 464 | - $this->get_select_field( | |
| 465 | - $forms, | |
| 466 | - $name, | |
| 467 | - $id, | |
| 468 | - $css_classes, | |
| 469 | - $selected_option, | |
| 470 | - $prepend_options, | |
| 471 | - $attributes, | |
| 472 | - $description | |
| 473 | - ), | |
| 474 | - convertkit_kses_allowed_html() | |
| 475 | - ); | |
| 476 | - | |
| 477 | - } | |
| 478 | - | |
| 479 | - /** | |
| 480 | 297 | * Returns a <select> field populated with the resources, based on the given parameters, |
| 481 | 298 | * that supports multiple selection. |
| 482 | 299 | * |
| 483 | 300 | * @since 2.6.9 |
| @@ -566,13 +383,12 @@ | ||
| 566 | 383 | * Current Forms will return a <script> embed string. |
| 567 | 384 | * |
| 568 | 385 | * @since 1.9.6 |
| 569 | 386 | * |
| 570 | - * @param int $id Form ID. | |
| 571 | - * @param int $post_id Post ID that requested the Form. | |
| 387 | + * @param int $id Form ID. | |
| 572 | 388 | * @return WP_Error|string |
| 573 | 389 | */ |
| 574 | - public function get_html( $id, $post_id = 0 ) { | |
| 390 | + public function get_html( $id ) { | |
| 575 | 391 | |
| 576 | 392 | // Cast ID to integer. |
| 577 | 393 | $id = absint( $id ); |
| 578 | 394 | |
| @@ -588,19 +404,18 @@ | ||
| 588 | 404 | sprintf( |
| 589 | 405 | /* translators: ConvertKit Form ID */ |
| 590 | 406 | __( 'Kit Form ID %s does not exist on Kit.', 'convertkit' ), |
| 591 | 407 | $id |
| 592 | - ), | |
| 593 | - 404 | |
| 408 | + ) | |
| 594 | 409 | ); |
| 595 | 410 | } |
| 596 | 411 | |
| 597 | - // Initialize Settings. | |
| 598 | - $settings = new ConvertKit_Settings(); | |
| 599 | - | |
| 600 | 412 | // If no uid is present in the Form API data, this is a legacy form that's served by directly fetching the HTML |
| 601 | 413 | // from forms.kit.com. |
| 602 | 414 | if ( ! isset( $this->resources[ $id ]['uid'] ) ) { |
| 415 | + // Initialize Settings. | |
| 416 | + $settings = new ConvertKit_Settings(); | |
| 417 | + | |
| 603 | 418 | // Bail if no Access Token is specified in the Plugin Settings. |
| 604 | 419 | if ( ! $settings->has_access_token() ) { |
| 605 | 420 | return new WP_Error( |
| 606 | 421 | 'convertkit_resource_forms_get_html', |
| @@ -630,26 +445,16 @@ | ||
| 630 | 445 | // displaying twice. |
| 631 | 446 | if ( $this->resources[ $id ]['format'] !== 'inline' ) { |
| 632 | 447 | add_filter( |
| 633 | 448 | 'convertkit_output_scripts_footer', |
| 634 | - function ( $scripts ) use ( $id, $post_id, $settings ) { | |
| 449 | + function ( $scripts ) use ( $id ) { | |
| 635 | 450 | |
| 636 | - // Build script. | |
| 637 | - $script = array( | |
| 638 | - 'async' => true, | |
| 639 | - 'data-uid' => $this->resources[ $id ]['uid'], | |
| 640 | - 'src' => $this->resources[ $id ]['embed_js'], | |
| 641 | - 'data-kit-limit-per-session' => $settings->non_inline_form_limit_per_session() ? '1' : '0', | |
| 451 | + $scripts[] = array( | |
| 452 | + 'async' => true, | |
| 453 | + 'data-uid' => $this->resources[ $id ]['uid'], | |
| 454 | + 'src' => $this->resources[ $id ]['embed_js'], | |
| 642 | 455 | ); |
| 643 | 456 | |
| 644 | - // If debugging is enabled, add the post ID to the script. | |
| 645 | - if ( $settings->debug_enabled() ) { | |
| 646 | - $script['data-kit-source-post-id'] = $post_id; | |
| 647 | - } | |
| 648 | - | |
| 649 | - // Add the script to the scripts array. | |
| 650 | - $scripts[] = $script; | |
| 651 | - | |
| 652 | 457 | return $scripts; |
| 653 | 458 | |
| 654 | 459 | } |
| 655 | 460 | ); |
| @@ -678,13 +483,8 @@ | ||
| 678 | 483 | 'async' => true, |
| 679 | 484 | 'data-uid' => $this->resources[ $id ]['uid'], |
| 680 | 485 | 'src' => $this->resources[ $id ]['embed_js'], |
| 681 | 486 | ); |
| 682 | - | |
| 683 | - // If debugging is enabled, add the post ID to the script. | |
| 684 | - if ( $settings->debug_enabled() ) { | |
| 685 | - $script['data-kit-source-post-id'] = $post_id; | |
| 686 | - } | |
| 687 | 487 | |
| 688 | 488 | /** |
| 689 | 489 | * Filter the form <script> key/value pairs immediately before the script is output. |
| 690 | 490 | * |