| @@ -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 | } |
| @@ -92,8 +94,174 @@ | ||
| 92 | 94 | |
| 93 | 95 | } |
| 94 | 96 | |
| 95 | 97 | /** |
| 98 | + * Determines if the given Form ID is a legacy Form or Landing Page. | |
| 99 | + * | |
| 100 | + * @since 2.5.0 | |
| 101 | + * | |
| 102 | + * @param int $id Form or Landing Page ID. | |
| 103 | + */ | |
| 104 | + public function is_legacy( $id ) { | |
| 105 | + | |
| 106 | + // Get Form. | |
| 107 | + $form = $this->get_by_id( (int) $id ); | |
| 108 | + | |
| 109 | + // Return false if no Form exists. | |
| 110 | + if ( ! $form ) { | |
| 111 | + return false; | |
| 112 | + } | |
| 113 | + | |
| 114 | + // If the `format` key exists, this is not a legacy Form. | |
| 115 | + if ( array_key_exists( 'format', $form ) ) { | |
| 116 | + return false; | |
| 117 | + } | |
| 118 | + | |
| 119 | + return true; | |
| 120 | + | |
| 121 | + } | |
| 122 | + | |
| 123 | + /** | |
| 124 | + * Returns a <select> field populated with all forms, based on the given parameters. | |
| 125 | + * | |
| 126 | + * @since 2.3.9 | |
| 127 | + * | |
| 128 | + * @param string $name Name. | |
| 129 | + * @param string $id ID. | |
| 130 | + * @param bool|array $css_classes <select> CSS class(es). | |
| 131 | + * @param string $selected_option <option> value to mark as selected. | |
| 132 | + * @param bool|array $prepend_options <option> elements to prepend before resources. | |
| 133 | + * @param bool|array $attributes <select> attributes. | |
| 134 | + * @param bool|string|array $description Description. | |
| 135 | + * @return string HTML Select Field | |
| 136 | + */ | |
| 137 | + public function get_select_field_all( $name, $id, $css_classes, $selected_option, $prepend_options = false, $attributes = false, $description = false ) { | |
| 138 | + | |
| 139 | + return $this->get_select_field( | |
| 140 | + $this->get(), | |
| 141 | + $name, | |
| 142 | + $id, | |
| 143 | + $css_classes, | |
| 144 | + $selected_option, | |
| 145 | + $prepend_options, | |
| 146 | + $attributes, | |
| 147 | + $description | |
| 148 | + ); | |
| 149 | + | |
| 150 | + } | |
| 151 | + | |
| 152 | + /** | |
| 153 | + * Returns a <select> field populated with all non-inline forms, based on the given parameters. | |
| 154 | + * | |
| 155 | + * @since 2.3.9 | |
| 156 | + * | |
| 157 | + * @param string $name Name. | |
| 158 | + * @param string $id ID. | |
| 159 | + * @param bool|array $css_classes <select> CSS class(es). | |
| 160 | + * @param string $selected_option <option> value to mark as selected. | |
| 161 | + * @param bool|array $prepend_options <option> elements to prepend before resources. | |
| 162 | + * @param bool|array $attributes <select> attributes. | |
| 163 | + * @param bool|string|array $description Description. | |
| 164 | + * @return string HTML Select Field | |
| 165 | + */ | |
| 166 | + public function get_select_field_non_inline( $name, $id, $css_classes, $selected_option, $prepend_options = false, $attributes = false, $description = false ) { | |
| 167 | + | |
| 168 | + return $this->get_select_field( | |
| 169 | + $this->get_non_inline(), | |
| 170 | + $name, | |
| 171 | + $id, | |
| 172 | + $css_classes, | |
| 173 | + $selected_option, | |
| 174 | + $prepend_options, | |
| 175 | + $attributes, | |
| 176 | + $description | |
| 177 | + ); | |
| 178 | + | |
| 179 | + } | |
| 180 | + | |
| 181 | + /** | |
| 182 | + * Returns a <select> field populated with the resources, based on the given parameters. | |
| 183 | + * | |
| 184 | + * @since 2.3.9 | |
| 185 | + * | |
| 186 | + * @param array $forms Forms. | |
| 187 | + * @param string $name Name. | |
| 188 | + * @param string $id ID. | |
| 189 | + * @param bool|array $css_classes <select> CSS class(es). | |
| 190 | + * @param string $selected_option <option> value to mark as selected. | |
| 191 | + * @param bool|array $prepend_options <option> elements to prepend before resources. | |
| 192 | + * @param bool|array $attributes <select> attributes. | |
| 193 | + * @param bool|string|array $description Description. | |
| 194 | + * @return string HTML Select Field | |
| 195 | + */ | |
| 196 | + private function get_select_field( $forms, $name, $id, $css_classes, $selected_option, $prepend_options = false, $attributes = false, $description = false ) { | |
| 197 | + | |
| 198 | + $html = sprintf( | |
| 199 | + '<select name="%s" id="%s" class="%s"', | |
| 200 | + esc_attr( $name ), | |
| 201 | + esc_attr( $id ), | |
| 202 | + esc_attr( ( is_array( $css_classes ) ? implode( ' ', $css_classes ) : '' ) ) | |
| 203 | + ); | |
| 204 | + | |
| 205 | + // Append any attributes. | |
| 206 | + if ( $attributes ) { | |
| 207 | + foreach ( $attributes as $key => $value ) { | |
| 208 | + $html .= sprintf( | |
| 209 | + ' %s="%s"', | |
| 210 | + esc_attr( $key ), | |
| 211 | + esc_attr( $value ) | |
| 212 | + ); | |
| 213 | + } | |
| 214 | + } | |
| 215 | + | |
| 216 | + // Close select tag. | |
| 217 | + $html .= '>'; | |
| 218 | + | |
| 219 | + // If any prepended options exist, add them now. | |
| 220 | + if ( $prepend_options ) { | |
| 221 | + foreach ( $prepend_options as $value => $label ) { | |
| 222 | + $html .= sprintf( | |
| 223 | + '<option value="%s" data-preserve-on-refresh="1"%s>%s</option>', | |
| 224 | + esc_attr( $value ), | |
| 225 | + selected( $selected_option, $value, false ), | |
| 226 | + esc_attr( $label ) | |
| 227 | + ); | |
| 228 | + } | |
| 229 | + } | |
| 230 | + | |
| 231 | + // Iterate through resources, if they exist, building <option> elements. | |
| 232 | + if ( $forms ) { | |
| 233 | + foreach ( $forms as $form ) { | |
| 234 | + // Legacy forms don't include a `format` key, so define them as inline. | |
| 235 | + $html .= sprintf( | |
| 236 | + '<option value="%s"%s>%s [%s]</option>', | |
| 237 | + esc_attr( $form['id'] ), | |
| 238 | + selected( $selected_option, $form['id'], false ), | |
| 239 | + esc_attr( $form['name'] ), | |
| 240 | + ( ! empty( $form['format'] ) ? esc_attr( $form['format'] ) : 'inline' ) | |
| 241 | + ); | |
| 242 | + } | |
| 243 | + } | |
| 244 | + | |
| 245 | + // Close select. | |
| 246 | + $html .= '</select>'; | |
| 247 | + | |
| 248 | + // If no description is provided, return the select field now. | |
| 249 | + if ( ! $description ) { | |
| 250 | + return $html; | |
| 251 | + } | |
| 252 | + | |
| 253 | + // Append description before returning field. | |
| 254 | + if ( ! is_array( $description ) ) { | |
| 255 | + return $html . '<p class="description">' . $description . '</p>'; | |
| 256 | + } | |
| 257 | + | |
| 258 | + // Return description lines in a paragraph, using breaklines for each description entry in the array. | |
| 259 | + return $html . '<p class="description">' . implode( '<br />', $description ) . '</p>'; | |
| 260 | + | |
| 261 | + } | |
| 262 | + | |
| 263 | + /** | |
| 96 | 264 | * Returns the HTML/JS markup for the given Form ID. |
| 97 | 265 | * |
| 98 | 266 | * Legacy Forms will return HTML. |
| 99 | 267 | * Current Forms will return a <script> embed string. |
| @@ -130,21 +298,30 @@ | ||
| 130 | 298 | if ( ! isset( $this->resources[ $id ]['uid'] ) ) { |
| 131 | 299 | // Initialize Settings. |
| 132 | 300 | $settings = new ConvertKit_Settings(); |
| 133 | 301 | |
| 134 | - // Bail if no API Key is specified in the Plugin Settings. | |
| 135 | - if ( ! $settings->has_api_key() ) { | |
| 302 | + // Bail if no Access Token is specified in the Plugin Settings. | |
| 303 | + if ( ! $settings->has_access_token() ) { | |
| 136 | 304 | return new WP_Error( |
| 137 | 305 | 'convertkit_resource_forms_get_html', |
| 138 | - __( 'ConvertKit Legacy Form could not be fetched as no API Key specified in Plugin Settings', 'convertkit' ) | |
| 306 | + __( 'ConvertKit Legacy Form could not be fetched as no Access Token specified in Plugin Settings', 'convertkit' ) | |
| 139 | 307 | ); |
| 140 | 308 | } |
| 141 | 309 | |
| 142 | 310 | // Initialize the API. |
| 143 | - $api = new ConvertKit_API( $settings->get_api_key(), $settings->get_api_secret(), $settings->debug_enabled(), 'output_form' ); | |
| 311 | + $api = new ConvertKit_API_V4( | |
| 312 | + CONVERTKIT_OAUTH_CLIENT_ID, | |
| 313 | + CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI, | |
| 314 | + $settings->get_access_token(), | |
| 315 | + $settings->get_refresh_token(), | |
| 316 | + $settings->debug_enabled(), | |
| 317 | + 'output_form' | |
| 318 | + ); | |
| 144 | 319 | |
| 145 | 320 | // Return Legacy Form HTML. |
| 146 | - return $api->get_form_html( $id ); | |
| 321 | + // We now call get_html() with the `embed_url` property, instead of get_form_html() with the `id` property, | |
| 322 | + // because `embed_url` includes the API Key. | |
| 323 | + return $api->get_html( $this->resources[ $id ]['embed_url'] ); | |
| 147 | 324 | } |
| 148 | 325 | |
| 149 | 326 | // If the form's format is not an inline form, add the inline script before the closing </body> tag. |
| 150 | 327 | // This prevents a modal form's overlay being constrained by the WordPress Theme's styles, |
| @@ -165,14 +342,68 @@ | ||
| 165 | 342 | |
| 166 | 343 | } |
| 167 | 344 | ); |
| 168 | 345 | |
| 346 | + // Sanity check we're not in the WordPress Admin interface. | |
| 347 | + // Some third party REST API Plugins seem to load frontend Posts, which would result in a wp_die() as | |
| 348 | + // the output Plugin class (rightly) isn't initialized in the backend. | |
| 349 | + if ( is_admin() ) { | |
| 350 | + return ''; | |
| 351 | + } | |
| 352 | + | |
| 353 | + // Don't output the global non-inline form, if defined, because | |
| 354 | + // a non-inline form was specified at either Post/Page default level, Post/Page level | |
| 355 | + // or Post Category level. | |
| 356 | + // This prevents multiple non-inline forms loading. | |
| 357 | + remove_action( 'wp_footer', array( WP_ConvertKit()->get_class( 'output' ), 'output_global_non_inline_form' ), 1 ); | |
| 358 | + | |
| 169 | 359 | // Don't return a script for output, as it'll be output in the site's footer. |
| 170 | 360 | return ''; |
| 171 | 361 | } |
| 172 | 362 | |
| 173 | 363 | // 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>'; | |
| 364 | + | |
| 365 | + // Define script key-value pairs. | |
| 366 | + $script = array( | |
| 367 | + 'async' => true, | |
| 368 | + 'data-uid' => $this->resources[ $id ]['uid'], | |
| 369 | + 'src' => $this->resources[ $id ]['embed_js'], | |
| 370 | + ); | |
| 371 | + | |
| 372 | + /** | |
| 373 | + * Filter the form <script> key/value pairs immediately before the script is output. | |
| 374 | + * | |
| 375 | + * @since 2.4.5 | |
| 376 | + * | |
| 377 | + * @param array $script Form script key/value pairs to output as <script> tag. | |
| 378 | + */ | |
| 379 | + $script = apply_filters( 'convertkit_resource_forms_output_script', $script ); | |
| 380 | + | |
| 381 | + // Build script output. | |
| 382 | + $output = '<script'; | |
| 383 | + foreach ( $script as $attribute => $value ) { | |
| 384 | + // If the value is true, just output the attribute. | |
| 385 | + if ( $value === true ) { | |
| 386 | + $output .= ' ' . esc_attr( $attribute ); | |
| 387 | + continue; | |
| 388 | + } | |
| 389 | + | |
| 390 | + // Sanitize attribute and value. | |
| 391 | + $attribute = esc_attr( $attribute ); | |
| 392 | + $value = ( $attribute === 'src' ? esc_url( $value ) : esc_attr( $value ) ); | |
| 393 | + | |
| 394 | + // Output the attribute and value. | |
| 395 | + $output .= ' ' . $attribute; | |
| 396 | + | |
| 397 | + // Output the value, if it's not a blank string. | |
| 398 | + if ( strlen( $value ) > 0 ) { | |
| 399 | + $output .= '="' . $value . '"'; | |
| 400 | + } | |
| 401 | + } | |
| 402 | + $output .= '></script>'; | |
| 403 | + | |
| 404 | + // Return script output. | |
| 405 | + return $output; | |
| 175 | 406 | |
| 176 | 407 | } |
| 177 | 408 | |
| 178 | 409 | } |