| @@ -11,9 +11,9 @@ | ||
| 11 | 11 | * ConvertKit Forms data stored locally from the API. |
| 12 | 12 | * |
| 13 | 13 | * @since 1.9.6 |
| 14 | 14 | */ |
| 15 | -class ConvertKit_Resource_Forms extends ConvertKit_Resource { | |
| 15 | +class ConvertKit_Resource_Forms extends ConvertKit_Resource_V4 { | |
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | 18 | * Holds the Settings Key that stores site wide ConvertKit settings |
| 19 | 19 | * |
| @@ -36,14 +36,16 @@ | ||
| 36 | 36 | * @param bool|string $context Context. |
| 37 | 37 | */ |
| 38 | 38 | public function __construct( $context = false ) { |
| 39 | 39 | |
| 40 | - // Initialize the API if the API Key and Secret have been defined in the Plugin Settings. | |
| 40 | + // Initialize the API if the Access Token has been defined in the Plugin Settings. | |
| 41 | 41 | $settings = new ConvertKit_Settings(); |
| 42 | - if ( $settings->has_api_key_and_secret() ) { | |
| 43 | - $this->api = new ConvertKit_API( | |
| 44 | - $settings->get_api_key(), | |
| 45 | - $settings->get_api_secret(), | |
| 42 | + if ( $settings->has_access_and_refresh_token() ) { | |
| 43 | + $this->api = new ConvertKit_API_V4( | |
| 44 | + CONVERTKIT_OAUTH_CLIENT_ID, | |
| 45 | + CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI, | |
| 46 | + $settings->get_access_token(), | |
| 47 | + $settings->get_refresh_token(), | |
| 46 | 48 | $settings->debug_enabled(), |
| 47 | 49 | $context |
| 48 | 50 | ); |
| 49 | 51 | } |
| @@ -53,8 +55,42 @@ | ||
| 53 | 55 | |
| 54 | 56 | } |
| 55 | 57 | |
| 56 | 58 | /** |
| 59 | + * Returns all inline forms based on the sort order. | |
| 60 | + * | |
| 61 | + * @since 2.7.3 | |
| 62 | + * | |
| 63 | + * @return bool|array | |
| 64 | + */ | |
| 65 | + public function get_inline() { | |
| 66 | + | |
| 67 | + // If the ConvertKit WordPress Libraries are < 1.3.6 (e.g. loaded by an outdated | |
| 68 | + // addon), or a WordPress site updates this Plugin before other ConvertKit Plugins, | |
| 69 | + // get_by() won't be available and will cause an E_ERROR, crashing the site. | |
| 70 | + // @see https://wordpress.org/support/topic/error-1795/. | |
| 71 | + if ( ! method_exists( $this, 'get_by' ) ) { // @phpstan-ignore-line Older WordPress Libraries won't have this function. | |
| 72 | + return false; | |
| 73 | + } | |
| 74 | + | |
| 75 | + return $this->get_by( 'format', array( 'inline' ) ); | |
| 76 | + | |
| 77 | + } | |
| 78 | + | |
| 79 | + /** | |
| 80 | + * Returns whether any inline forms exist in the options table. | |
| 81 | + * | |
| 82 | + * @since 2.7.3 | |
| 83 | + * | |
| 84 | + * @return bool | |
| 85 | + */ | |
| 86 | + public function inline_exist() { | |
| 87 | + | |
| 88 | + return (bool) $this->get_inline(); | |
| 89 | + | |
| 90 | + } | |
| 91 | + | |
| 92 | + /** | |
| 57 | 93 | * Returns all non-inline forms based on the sort order. |
| 58 | 94 | * |
| 59 | 95 | * @since 2.2.4 |
| 60 | 96 | * |
| @@ -65,9 +101,9 @@ | ||
| 65 | 101 | // If the ConvertKit WordPress Libraries are < 1.3.6 (e.g. loaded by an outdated |
| 66 | 102 | // addon), or a WordPress site updates this Plugin before other ConvertKit Plugins, |
| 67 | 103 | // get_by() won't be available and will cause an E_ERROR, crashing the site. |
| 68 | 104 | // @see https://wordpress.org/support/topic/error-1795/. |
| 69 | - if ( ! method_exists( $this, 'get_by' ) ) { | |
| 105 | + if ( ! method_exists( $this, 'get_by' ) ) { // @phpstan-ignore-line Older WordPress Libraries won't have this function. | |
| 70 | 106 | return false; |
| 71 | 107 | } |
| 72 | 108 | |
| 73 | 109 | return $this->get_by( 'format', array( 'modal', 'slide in', 'sticky bar' ) ); |
| @@ -73,9 +109,8 @@ | ||
| 73 | 109 | return $this->get_by( 'format', array( 'modal', 'slide in', 'sticky bar' ) ); |
| 74 | 110 | |
| 75 | 111 | } |
| 76 | 112 | |
| 77 | - | |
| 78 | 113 | /** |
| 79 | 114 | * Returns whether any non-inline forms exist in the options table. |
| 80 | 115 | * |
| 81 | 116 | * @since 2.2.4 |
| @@ -92,8 +127,257 @@ | ||
| 92 | 127 | |
| 93 | 128 | } |
| 94 | 129 | |
| 95 | 130 | /** |
| 131 | + * Determines if the given Form ID is a legacy Form or Landing Page. | |
| 132 | + * | |
| 133 | + * @since 2.5.0 | |
| 134 | + * | |
| 135 | + * @param int $id Form or Landing Page ID. | |
| 136 | + */ | |
| 137 | + public function is_legacy( $id ) { | |
| 138 | + | |
| 139 | + // Get Form. | |
| 140 | + $form = $this->get_by_id( (int) $id ); | |
| 141 | + | |
| 142 | + // Return false if no Form exists. | |
| 143 | + if ( ! $form ) { | |
| 144 | + return false; | |
| 145 | + } | |
| 146 | + | |
| 147 | + // If the `format` key exists, this is not a legacy Form. | |
| 148 | + if ( array_key_exists( 'format', $form ) ) { | |
| 149 | + return false; | |
| 150 | + } | |
| 151 | + | |
| 152 | + return true; | |
| 153 | + | |
| 154 | + } | |
| 155 | + | |
| 156 | + /** | |
| 157 | + * Returns a <select> field populated with all forms, based on the given parameters. | |
| 158 | + * | |
| 159 | + * @since 2.3.9 | |
| 160 | + * | |
| 161 | + * @param string $name Name. | |
| 162 | + * @param string $id ID. | |
| 163 | + * @param bool|array $css_classes <select> CSS class(es). | |
| 164 | + * @param string $selected_option <option> value to mark as selected. | |
| 165 | + * @param bool|array $prepend_options <option> elements to prepend before resources. | |
| 166 | + * @param bool|array $attributes <select> attributes. | |
| 167 | + * @param bool|string|array $description Description. | |
| 168 | + * @return string HTML Select Field | |
| 169 | + */ | |
| 170 | + public function get_select_field_all( $name, $id, $css_classes, $selected_option, $prepend_options = false, $attributes = false, $description = false ) { | |
| 171 | + | |
| 172 | + return $this->get_select_field( | |
| 173 | + $this->get(), | |
| 174 | + $name, | |
| 175 | + $id, | |
| 176 | + $css_classes, | |
| 177 | + $selected_option, | |
| 178 | + $prepend_options, | |
| 179 | + $attributes, | |
| 180 | + $description | |
| 181 | + ); | |
| 182 | + | |
| 183 | + } | |
| 184 | + | |
| 185 | + /** | |
| 186 | + * Returns a <select> field populated with all non-inline forms, based on the given parameters. | |
| 187 | + * | |
| 188 | + * @since 2.3.9 | |
| 189 | + * | |
| 190 | + * @param string $name Name. | |
| 191 | + * @param string $id ID. | |
| 192 | + * @param bool|array $css_classes <select> CSS class(es). | |
| 193 | + * @param array $selected_options <option> values to mark as selected. | |
| 194 | + * @param bool|array $prepend_options <option> elements to prepend before resources. | |
| 195 | + * @param bool|array $attributes <select> attributes. | |
| 196 | + * @param bool|string|array $description Description. | |
| 197 | + * @return string HTML Select Field | |
| 198 | + */ | |
| 199 | + public function get_select_field_non_inline( $name, $id, $css_classes, $selected_options, $prepend_options = false, $attributes = false, $description = false ) { | |
| 200 | + | |
| 201 | + return $this->get_multi_select_field( | |
| 202 | + $this->get_non_inline(), | |
| 203 | + $name, | |
| 204 | + $id, | |
| 205 | + $css_classes, | |
| 206 | + $selected_options, | |
| 207 | + $prepend_options, | |
| 208 | + $attributes, | |
| 209 | + $description | |
| 210 | + ); | |
| 211 | + | |
| 212 | + } | |
| 213 | + | |
| 214 | + /** | |
| 215 | + * Returns a <select> field populated with the resources, based on the given parameters. | |
| 216 | + * | |
| 217 | + * @since 2.3.9 | |
| 218 | + * | |
| 219 | + * @param array $forms Forms. | |
| 220 | + * @param string $name Name. | |
| 221 | + * @param string $id ID. | |
| 222 | + * @param bool|array $css_classes <select> CSS class(es). | |
| 223 | + * @param string $selected_option <option> value to mark as selected. | |
| 224 | + * @param bool|array $prepend_options <option> elements to prepend before resources. | |
| 225 | + * @param bool|array $attributes <select> attributes. | |
| 226 | + * @param bool|string|array $description Description. | |
| 227 | + * @return string HTML Select Field | |
| 228 | + */ | |
| 229 | + private function get_select_field( $forms, $name, $id, $css_classes, $selected_option, $prepend_options = false, $attributes = false, $description = false ) { | |
| 230 | + | |
| 231 | + $html = sprintf( | |
| 232 | + '<select name="%s" id="%s" class="%s"', | |
| 233 | + esc_attr( $name ), | |
| 234 | + esc_attr( $id ), | |
| 235 | + esc_attr( ( is_array( $css_classes ) ? implode( ' ', $css_classes ) : '' ) ) | |
| 236 | + ); | |
| 237 | + | |
| 238 | + // Append any attributes. | |
| 239 | + if ( $attributes ) { | |
| 240 | + foreach ( $attributes as $key => $value ) { | |
| 241 | + $html .= sprintf( | |
| 242 | + ' %s="%s"', | |
| 243 | + esc_attr( $key ), | |
| 244 | + esc_attr( $value ) | |
| 245 | + ); | |
| 246 | + } | |
| 247 | + } | |
| 248 | + | |
| 249 | + // Close select tag. | |
| 250 | + $html .= '>'; | |
| 251 | + | |
| 252 | + // If any prepended options exist, add them now. | |
| 253 | + if ( $prepend_options ) { | |
| 254 | + foreach ( $prepend_options as $value => $label ) { | |
| 255 | + $html .= sprintf( | |
| 256 | + '<option value="%s" data-preserve-on-refresh="1"%s>%s</option>', | |
| 257 | + esc_attr( $value ), | |
| 258 | + selected( $selected_option, $value, false ), | |
| 259 | + esc_attr( $label ) | |
| 260 | + ); | |
| 261 | + } | |
| 262 | + } | |
| 263 | + | |
| 264 | + // Iterate through resources, if they exist, building <option> elements. | |
| 265 | + if ( $forms ) { | |
| 266 | + foreach ( $forms as $form ) { | |
| 267 | + // Legacy forms don't include a `format` key, so define them as inline. | |
| 268 | + $html .= sprintf( | |
| 269 | + '<option value="%s"%s>%s [%s]</option>', | |
| 270 | + esc_attr( $form['id'] ), | |
| 271 | + selected( $selected_option, $form['id'], false ), | |
| 272 | + esc_attr( $form['name'] ), | |
| 273 | + ( ! empty( $form['format'] ) ? esc_attr( $form['format'] ) : 'inline' ) | |
| 274 | + ); | |
| 275 | + } | |
| 276 | + } | |
| 277 | + | |
| 278 | + // Close select. | |
| 279 | + $html .= '</select>'; | |
| 280 | + | |
| 281 | + // If no description is provided, return the select field now. | |
| 282 | + if ( ! $description ) { | |
| 283 | + return $html; | |
| 284 | + } | |
| 285 | + | |
| 286 | + // Append description before returning field. | |
| 287 | + if ( ! is_array( $description ) ) { | |
| 288 | + return $html . '<p class="description">' . $description . '</p>'; | |
| 289 | + } | |
| 290 | + | |
| 291 | + // Return description lines in a paragraph, using breaklines for each description entry in the array. | |
| 292 | + return $html . '<p class="description">' . implode( '<br />', $description ) . '</p>'; | |
| 293 | + | |
| 294 | + } | |
| 295 | + | |
| 296 | + /** | |
| 297 | + * Returns a <select> field populated with the resources, based on the given parameters, | |
| 298 | + * that supports multiple selection. | |
| 299 | + * | |
| 300 | + * @since 2.6.9 | |
| 301 | + * | |
| 302 | + * @param array $forms Forms. | |
| 303 | + * @param string $name Name. | |
| 304 | + * @param string $id ID. | |
| 305 | + * @param bool|array $css_classes <select> CSS class(es). | |
| 306 | + * @param array $selected_options <option> values to mark as selected. | |
| 307 | + * @param bool|array $prepend_options <option> elements to prepend before resources. | |
| 308 | + * @param bool|array $attributes <select> attributes. | |
| 309 | + * @param bool|string|array $description Description. | |
| 310 | + * @return string HTML Select Field | |
| 311 | + */ | |
| 312 | + private function get_multi_select_field( $forms, $name, $id, $css_classes, $selected_options = array(), $prepend_options = false, $attributes = false, $description = false ) { | |
| 313 | + | |
| 314 | + $html = sprintf( | |
| 315 | + '<select name="%s[]" id="%s" class="%s" multiple', | |
| 316 | + esc_attr( $name ), | |
| 317 | + esc_attr( $id ), | |
| 318 | + esc_attr( ( is_array( $css_classes ) ? implode( ' ', $css_classes ) : '' ) ) | |
| 319 | + ); | |
| 320 | + | |
| 321 | + // Append any attributes. | |
| 322 | + if ( $attributes ) { | |
| 323 | + foreach ( $attributes as $key => $value ) { | |
| 324 | + $html .= sprintf( | |
| 325 | + ' %s="%s"', | |
| 326 | + esc_attr( $key ), | |
| 327 | + esc_attr( $value ) | |
| 328 | + ); | |
| 329 | + } | |
| 330 | + } | |
| 331 | + | |
| 332 | + // Close select tag. | |
| 333 | + $html .= '>'; | |
| 334 | + | |
| 335 | + // If any prepended options exist, add them now. | |
| 336 | + if ( $prepend_options ) { | |
| 337 | + foreach ( $prepend_options as $value => $label ) { | |
| 338 | + $html .= sprintf( | |
| 339 | + '<option value="%s" data-preserve-on-refresh="1"%s>%s</option>', | |
| 340 | + esc_attr( $value ), | |
| 341 | + ( in_array( $value, $selected_options, true ) ? ' selected' : '' ), | |
| 342 | + esc_attr( $label ) | |
| 343 | + ); | |
| 344 | + } | |
| 345 | + } | |
| 346 | + | |
| 347 | + // Iterate through resources, if they exist, building <option> elements. | |
| 348 | + if ( $forms ) { | |
| 349 | + foreach ( $forms as $form ) { | |
| 350 | + // Legacy forms don't include a `format` key, so define them as inline. | |
| 351 | + $html .= sprintf( | |
| 352 | + '<option value="%s"%s>%s [%s]</option>', | |
| 353 | + esc_attr( $form['id'] ), | |
| 354 | + ( in_array( $form['id'], $selected_options, true ) ? ' selected' : '' ), | |
| 355 | + esc_attr( $form['name'] ), | |
| 356 | + ( ! empty( $form['format'] ) ? esc_attr( $form['format'] ) : 'inline' ) | |
| 357 | + ); | |
| 358 | + } | |
| 359 | + } | |
| 360 | + | |
| 361 | + // Close select. | |
| 362 | + $html .= '</select>'; | |
| 363 | + | |
| 364 | + // If no description is provided, return the select field now. | |
| 365 | + if ( ! $description ) { | |
| 366 | + return $html; | |
| 367 | + } | |
| 368 | + | |
| 369 | + // Append description before returning field. | |
| 370 | + if ( ! is_array( $description ) ) { | |
| 371 | + return $html . '<p class="description">' . $description . '</p>'; | |
| 372 | + } | |
| 373 | + | |
| 374 | + // Return description lines in a paragraph, using breaklines for each description entry in the array. | |
| 375 | + return $html . '<p class="description">' . implode( '<br />', $description ) . '</p>'; | |
| 376 | + | |
| 377 | + } | |
| 378 | + | |
| 379 | + /** | |
| 96 | 380 | * Returns the HTML/JS markup for the given Form ID. |
| 97 | 381 | * |
| 98 | 382 | * Legacy Forms will return HTML. |
| 99 | 383 | * Current Forms will return a <script> embed string. |
| @@ -118,9 +402,9 @@ | ||
| 118 | 402 | return new WP_Error( |
| 119 | 403 | 'convertkit_resource_forms_get_html', |
| 120 | 404 | sprintf( |
| 121 | 405 | /* translators: ConvertKit Form ID */ |
| 122 | - __( 'ConvertKit Form ID %s does not exist on ConvertKit.', 'convertkit' ), | |
| 406 | + __( 'Kit Form ID %s does not exist on Kit.', 'convertkit' ), | |
| 123 | 407 | $id |
| 124 | 408 | ) |
| 125 | 409 | ); |
| 126 | 410 | } |
| @@ -125,26 +409,35 @@ | ||
| 125 | 409 | ); |
| 126 | 410 | } |
| 127 | 411 | |
| 128 | 412 | // If no uid is present in the Form API data, this is a legacy form that's served by directly fetching the HTML |
| 129 | - // from forms.convertkit.com. | |
| 413 | + // from forms.kit.com. | |
| 130 | 414 | if ( ! isset( $this->resources[ $id ]['uid'] ) ) { |
| 131 | 415 | // Initialize Settings. |
| 132 | 416 | $settings = new ConvertKit_Settings(); |
| 133 | 417 | |
| 134 | - // Bail if no API Key is specified in the Plugin Settings. | |
| 135 | - if ( ! $settings->has_api_key() ) { | |
| 418 | + // Bail if no Access Token is specified in the Plugin Settings. | |
| 419 | + if ( ! $settings->has_access_token() ) { | |
| 136 | 420 | return new WP_Error( |
| 137 | 421 | 'convertkit_resource_forms_get_html', |
| 138 | - __( 'ConvertKit Legacy Form could not be fetched as no API Key specified in Plugin Settings', 'convertkit' ) | |
| 422 | + __( 'Kit Legacy Form could not be fetched as no Access Token specified in Plugin Settings', 'convertkit' ) | |
| 139 | 423 | ); |
| 140 | 424 | } |
| 141 | 425 | |
| 142 | 426 | // Initialize the API. |
| 143 | - $api = new ConvertKit_API( $settings->get_api_key(), $settings->get_api_secret(), $settings->debug_enabled(), 'output_form' ); | |
| 427 | + $api = new ConvertKit_API_V4( | |
| 428 | + CONVERTKIT_OAUTH_CLIENT_ID, | |
| 429 | + CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI, | |
| 430 | + $settings->get_access_token(), | |
| 431 | + $settings->get_refresh_token(), | |
| 432 | + $settings->debug_enabled(), | |
| 433 | + 'output_form' | |
| 434 | + ); | |
| 144 | 435 | |
| 145 | 436 | // Return Legacy Form HTML. |
| 146 | - return $api->get_form_html( $id ); | |
| 437 | + // We now call get_html() with the `embed_url` property, instead of get_form_html() with the `id` property, | |
| 438 | + // because `embed_url` includes the API Key. | |
| 439 | + return $api->get_html( $this->resources[ $id ]['embed_url'] ); | |
| 147 | 440 | } |
| 148 | 441 | |
| 149 | 442 | // If the form's format is not an inline form, add the inline script before the closing </body> tag. |
| 150 | 443 | // This prevents a modal form's overlay being constrained by the WordPress Theme's styles, |
| @@ -165,14 +458,68 @@ | ||
| 165 | 458 | |
| 166 | 459 | } |
| 167 | 460 | ); |
| 168 | 461 | |
| 462 | + // Sanity check we're not in the WordPress Admin interface. | |
| 463 | + // Some third party REST API Plugins seem to load frontend Posts, which would result in a wp_die() as | |
| 464 | + // the output Plugin class (rightly) isn't initialized in the backend. | |
| 465 | + if ( is_admin() ) { | |
| 466 | + return ''; | |
| 467 | + } | |
| 468 | + | |
| 469 | + // Don't output the global non-inline form, if defined, because | |
| 470 | + // a non-inline form was specified at either Post/Page default level, Post/Page level | |
| 471 | + // or Post Category level. | |
| 472 | + // This prevents multiple non-inline forms loading. | |
| 473 | + remove_action( 'wp_footer', array( WP_ConvertKit()->get_class( 'output' ), 'output_global_non_inline_form' ), 1 ); | |
| 474 | + | |
| 169 | 475 | // Don't return a script for output, as it'll be output in the site's footer. |
| 170 | 476 | return ''; |
| 171 | 477 | } |
| 172 | 478 | |
| 173 | 479 | // If here, return Form <script> embed now, as we want the inline form to display at this specific point of the content. |
| 174 | - return '<script async data-uid="' . esc_attr( $this->resources[ $id ]['uid'] ) . '" src="' . esc_url( $this->resources[ $id ]['embed_js'] ) . '"></script>'; | |
| 480 | + | |
| 481 | + // Define script key-value pairs. | |
| 482 | + $script = array( | |
| 483 | + 'async' => true, | |
| 484 | + 'data-uid' => $this->resources[ $id ]['uid'], | |
| 485 | + 'src' => $this->resources[ $id ]['embed_js'], | |
| 486 | + ); | |
| 487 | + | |
| 488 | + /** | |
| 489 | + * Filter the form <script> key/value pairs immediately before the script is output. | |
| 490 | + * | |
| 491 | + * @since 2.4.5 | |
| 492 | + * | |
| 493 | + * @param array $script Form script key/value pairs to output as <script> tag. | |
| 494 | + */ | |
| 495 | + $script = apply_filters( 'convertkit_resource_forms_output_script', $script ); | |
| 496 | + | |
| 497 | + // Build script output. | |
| 498 | + $output = '<script'; | |
| 499 | + foreach ( $script as $attribute => $value ) { | |
| 500 | + // If the value is true, just output the attribute. | |
| 501 | + if ( $value === true ) { | |
| 502 | + $output .= ' ' . esc_attr( $attribute ); | |
| 503 | + continue; | |
| 504 | + } | |
| 505 | + | |
| 506 | + // Sanitize attribute and value. | |
| 507 | + $attribute = esc_attr( $attribute ); | |
| 508 | + $value = ( $attribute === 'src' ? esc_url( $value ) : esc_attr( $value ) ); | |
| 509 | + | |
| 510 | + // Output the attribute and value. | |
| 511 | + $output .= ' ' . $attribute; | |
| 512 | + | |
| 513 | + // Output the value, if it's not a blank string. | |
| 514 | + if ( strlen( $value ) > 0 ) { | |
| 515 | + $output .= '="' . $value . '"'; | |
| 516 | + } | |
| 517 | + } | |
| 518 | + $output .= '></script>'; | |
| 519 | + | |
| 520 | + // Return script output. | |
| 521 | + return $output; | |
| 175 | 522 | |
| 176 | 523 | } |
| 177 | 524 | |
| 178 | 525 | } |