| @@ -24,15 +24,20 @@ | ||
| 24 | 24 | * @since 1.9.8.5 |
| 25 | 25 | */ |
| 26 | 26 | public function __construct() { |
| 27 | 27 | |
| 28 | + // Preview inline forms. | |
| 28 | 29 | add_filter( 'convertkit_output_append_form_to_content_form_id', array( $this, 'preview_form' ), 99999 ); |
| 30 | + | |
| 31 | + // Preview non-inline forms. | |
| 32 | + add_filter( 'template_redirect', array( $this, 'preview_non_inline_form' ) ); | |
| 33 | + | |
| 34 | + // Append edit link to inline forms. | |
| 29 | 35 | add_filter( 'convertkit_block_form_render', array( $this, 'maybe_append_edit_form_link_to_form_block' ), 10, 3 ); |
| 30 | 36 | add_filter( 'convertkit_frontend_append_form', array( $this, 'maybe_append_edit_form_link_to_form' ), 10, 4 ); |
| 31 | 37 | |
| 32 | 38 | } |
| 33 | 39 | |
| 34 | - | |
| 35 | 40 | /** |
| 36 | 41 | * Changes the form to display for the given Post ID if the request is |
| 37 | 42 | * from a logged in user who has clicked a preview link in the Plugin Setup Wizard. |
| 38 | 43 | * |
| @@ -58,9 +63,9 @@ | ||
| 58 | 63 | return $form_id; |
| 59 | 64 | } |
| 60 | 65 | |
| 61 | 66 | // Determine the form to preview. |
| 62 | - $preview_form_id = (int) ( isset( $_REQUEST['convertkit_form_id'] ) ? sanitize_text_field( $_REQUEST['convertkit_form_id'] ) : 0 ); | |
| 67 | + $preview_form_id = ( isset( $_REQUEST['convertkit_form_id'] ) ? absint( wp_unslash( $_REQUEST['convertkit_form_id'] ) ) : 0 ); | |
| 63 | 68 | |
| 64 | 69 | // Return. |
| 65 | 70 | return $preview_form_id; |
| 66 | 71 | |
| @@ -66,8 +71,73 @@ | ||
| 66 | 71 | |
| 67 | 72 | } |
| 68 | 73 | |
| 69 | 74 | /** |
| 75 | + * Adds non-inline form(s) for display if the request is from a logged in user who has clicked a preview link | |
| 76 | + * and is viewing the home page. | |
| 77 | + * | |
| 78 | + * @since 2.3.3 | |
| 79 | + */ | |
| 80 | + public function preview_non_inline_form() { | |
| 81 | + | |
| 82 | + // Bail if not on the home page. | |
| 83 | + if ( ! is_home() ) { | |
| 84 | + return; | |
| 85 | + } | |
| 86 | + | |
| 87 | + // Bail if the user isn't logged in. | |
| 88 | + if ( ! is_user_logged_in() ) { | |
| 89 | + return; | |
| 90 | + } | |
| 91 | + | |
| 92 | + // Bail if no nonce field exists. | |
| 93 | + if ( ! isset( $_REQUEST['convertkit-preview-form-nonce'] ) ) { | |
| 94 | + return; | |
| 95 | + } | |
| 96 | + | |
| 97 | + // Bail if the nonce verification fails. | |
| 98 | + if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['convertkit-preview-form-nonce'] ) ), 'convertkit-preview-form' ) ) { | |
| 99 | + return; | |
| 100 | + } | |
| 101 | + | |
| 102 | + // Bail if no form(s) to preview. | |
| 103 | + if ( ! isset( $_REQUEST['convertkit_form_id'] ) ) { | |
| 104 | + return; | |
| 105 | + } | |
| 106 | + | |
| 107 | + // Determine form ID(s) to preview. | |
| 108 | + $convertkit_forms = new ConvertKit_Resource_Forms(); | |
| 109 | + $preview_form_ids = explode( ',', sanitize_text_field( wp_unslash( $_REQUEST['convertkit_form_id'] ) ) ); | |
| 110 | + | |
| 111 | + foreach ( $preview_form_ids as $preview_form_id ) { | |
| 112 | + // Get form. | |
| 113 | + $form = $convertkit_forms->get_by_id( (int) $preview_form_id ); | |
| 114 | + | |
| 115 | + // Bail if the Form doesn't exist (this shouldn't happen, but you never know). | |
| 116 | + if ( ! $form ) { | |
| 117 | + continue; | |
| 118 | + } | |
| 119 | + | |
| 120 | + // Add the form to the scripts array so it is included in the preview. | |
| 121 | + add_filter( | |
| 122 | + 'convertkit_output_scripts_footer', | |
| 123 | + function ( $scripts ) use ( $form ) { | |
| 124 | + | |
| 125 | + $scripts[] = array( | |
| 126 | + 'async' => true, | |
| 127 | + 'data-uid' => $form['uid'], | |
| 128 | + 'src' => $form['embed_js'], | |
| 129 | + ); | |
| 130 | + | |
| 131 | + return $scripts; | |
| 132 | + | |
| 133 | + } | |
| 134 | + ); | |
| 135 | + } | |
| 136 | + | |
| 137 | + } | |
| 138 | + | |
| 139 | + /** | |
| 70 | 140 | * Returns the URL for the most recent published Post based on the supplied Post Type, |
| 71 | 141 | * with a preview form nonce included in the URL. |
| 72 | 142 | * |
| 73 | 143 | * @since 1.9.8.5 |
| @@ -95,8 +165,27 @@ | ||
| 95 | 165 | |
| 96 | 166 | } |
| 97 | 167 | |
| 98 | 168 | /** |
| 169 | + * Returns the URL for the home page, with a preview form nonce included in the URL. | |
| 170 | + * | |
| 171 | + * @since 2.3.3 | |
| 172 | + * | |
| 173 | + * @return string | |
| 174 | + */ | |
| 175 | + public function get_preview_form_home_url() { | |
| 176 | + | |
| 177 | + // Return preview URL. | |
| 178 | + return add_query_arg( | |
| 179 | + array( | |
| 180 | + 'convertkit-preview-form-nonce' => $this->get_preview_form_nonce(), | |
| 181 | + ), | |
| 182 | + get_home_url() | |
| 183 | + ); | |
| 184 | + | |
| 185 | + } | |
| 186 | + | |
| 187 | + /** | |
| 99 | 188 | * Appends an "Edit form in ConvertKit" link to the given ConvertKit Form block, |
| 100 | 189 | * if the request is to preview a Page and the logged in WordPress user can |
| 101 | 190 | * edit the Page. |
| 102 | 191 | * |
| @@ -184,9 +273,9 @@ | ||
| 184 | 273 | 'utm_term' => get_locale(), |
| 185 | 274 | 'utm_content' => 'convertkit', |
| 186 | 275 | ), |
| 187 | 276 | sprintf( |
| 188 | - 'https://app.convertkit.com/landing_pages/%s/edit/', | |
| 277 | + 'https://app.kit.com/landing_pages/%s/edit/', | |
| 189 | 278 | esc_attr( (string) $form_id ) |
| 190 | 279 | ) |
| 191 | 280 | ); |
| 192 | 281 | } else { |
| @@ -196,9 +285,9 @@ | ||
| 196 | 285 | 'utm_term' => get_locale(), |
| 197 | 286 | 'utm_content' => 'convertkit', |
| 198 | 287 | ), |
| 199 | 288 | sprintf( |
| 200 | - 'https://app.convertkit.com/forms/designers/%s/edit/', | |
| 289 | + 'https://app.kit.com/forms/designers/%s/edit/', | |
| 201 | 290 | esc_attr( (string) $form_id ) |
| 202 | 291 | ) |
| 203 | 292 | ); |
| 204 | 293 | } |
| @@ -206,9 +295,9 @@ | ||
| 206 | 295 | // Append a link to edit the Form on ConvertKit. |
| 207 | 296 | $form_html .= sprintf( |
| 208 | 297 | '<div style="margin:0;padding:5px;text-align:right;font-size:13px;"><a href="%s" target="_blank">%s</a></div>', |
| 209 | 298 | esc_url( $link ), |
| 210 | - esc_html__( 'Edit form in ConvertKit', 'convertkit' ) | |
| 299 | + esc_html__( 'Edit form in Kit', 'convertkit' ) | |
| 211 | 300 | ); |
| 212 | 301 | |
| 213 | 302 | return $form_html; |
| 214 | 303 | |