| @@ -92,8 +92,148 @@ | ||
| 92 | 92 | |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | /** |
| 96 | + * Returns a <select> field populated with all forms, based on the given parameters. | |
| 97 | + * | |
| 98 | + * @since 2.3.9 | |
| 99 | + * | |
| 100 | + * @param string $name Name. | |
| 101 | + * @param string $id ID. | |
| 102 | + * @param bool|array $css_classes <select> CSS class(es). | |
| 103 | + * @param string $selected_option <option> value to mark as selected. | |
| 104 | + * @param bool|array $prepend_options <option> elements to prepend before resources. | |
| 105 | + * @param bool|array $attributes <select> attributes. | |
| 106 | + * @param bool|string|array $description Description. | |
| 107 | + * @return string HTML Select Field | |
| 108 | + */ | |
| 109 | + public function get_select_field_all( $name, $id, $css_classes, $selected_option, $prepend_options = false, $attributes = false, $description = false ) { | |
| 110 | + | |
| 111 | + return $this->get_select_field( | |
| 112 | + $this->get(), | |
| 113 | + $name, | |
| 114 | + $id, | |
| 115 | + $css_classes, | |
| 116 | + $selected_option, | |
| 117 | + $prepend_options, | |
| 118 | + $attributes, | |
| 119 | + $description | |
| 120 | + ); | |
| 121 | + | |
| 122 | + } | |
| 123 | + | |
| 124 | + /** | |
| 125 | + * Returns a <select> field populated with all non-inline forms, based on the given parameters. | |
| 126 | + * | |
| 127 | + * @since 2.3.9 | |
| 128 | + * | |
| 129 | + * @param string $name Name. | |
| 130 | + * @param string $id ID. | |
| 131 | + * @param bool|array $css_classes <select> CSS class(es). | |
| 132 | + * @param string $selected_option <option> value to mark as selected. | |
| 133 | + * @param bool|array $prepend_options <option> elements to prepend before resources. | |
| 134 | + * @param bool|array $attributes <select> attributes. | |
| 135 | + * @param bool|string|array $description Description. | |
| 136 | + * @return string HTML Select Field | |
| 137 | + */ | |
| 138 | + public function get_select_field_non_inline( $name, $id, $css_classes, $selected_option, $prepend_options = false, $attributes = false, $description = false ) { | |
| 139 | + | |
| 140 | + return $this->get_select_field( | |
| 141 | + $this->get_non_inline(), | |
| 142 | + $name, | |
| 143 | + $id, | |
| 144 | + $css_classes, | |
| 145 | + $selected_option, | |
| 146 | + $prepend_options, | |
| 147 | + $attributes, | |
| 148 | + $description | |
| 149 | + ); | |
| 150 | + | |
| 151 | + } | |
| 152 | + | |
| 153 | + /** | |
| 154 | + * Returns a <select> field populated with the resources, based on the given parameters. | |
| 155 | + * | |
| 156 | + * @since 2.3.9 | |
| 157 | + * | |
| 158 | + * @param array $forms Forms. | |
| 159 | + * @param string $name Name. | |
| 160 | + * @param string $id ID. | |
| 161 | + * @param bool|array $css_classes <select> CSS class(es). | |
| 162 | + * @param string $selected_option <option> value to mark as selected. | |
| 163 | + * @param bool|array $prepend_options <option> elements to prepend before resources. | |
| 164 | + * @param bool|array $attributes <select> attributes. | |
| 165 | + * @param bool|string|array $description Description. | |
| 166 | + * @return string HTML Select Field | |
| 167 | + */ | |
| 168 | + private function get_select_field( $forms, $name, $id, $css_classes, $selected_option, $prepend_options = false, $attributes = false, $description = false ) { | |
| 169 | + | |
| 170 | + $html = sprintf( | |
| 171 | + '<select name="%s" id="%s" class="%s"', | |
| 172 | + esc_attr( $name ), | |
| 173 | + esc_attr( $id ), | |
| 174 | + esc_attr( ( is_array( $css_classes ) ? implode( ' ', $css_classes ) : '' ) ) | |
| 175 | + ); | |
| 176 | + | |
| 177 | + // Append any attributes. | |
| 178 | + if ( $attributes ) { | |
| 179 | + foreach ( $attributes as $key => $value ) { | |
| 180 | + $html .= sprintf( | |
| 181 | + ' %s="%s"', | |
| 182 | + esc_attr( $key ), | |
| 183 | + esc_attr( $value ) | |
| 184 | + ); | |
| 185 | + } | |
| 186 | + } | |
| 187 | + | |
| 188 | + // Close select tag. | |
| 189 | + $html .= '>'; | |
| 190 | + | |
| 191 | + // If any prepended options exist, add them now. | |
| 192 | + if ( $prepend_options ) { | |
| 193 | + foreach ( $prepend_options as $value => $label ) { | |
| 194 | + $html .= sprintf( | |
| 195 | + '<option value="%s" data-preserve-on-refresh="1"%s>%s</option>', | |
| 196 | + esc_attr( $value ), | |
| 197 | + selected( $selected_option, $value, false ), | |
| 198 | + esc_attr( $label ) | |
| 199 | + ); | |
| 200 | + } | |
| 201 | + } | |
| 202 | + | |
| 203 | + // Iterate through resources, if they exist, building <option> elements. | |
| 204 | + if ( $forms ) { | |
| 205 | + foreach ( $forms as $form ) { | |
| 206 | + // Legacy forms don't include a `format` key, so define them as inline. | |
| 207 | + $html .= sprintf( | |
| 208 | + '<option value="%s"%s>%s [%s]</option>', | |
| 209 | + esc_attr( $form['id'] ), | |
| 210 | + selected( $selected_option, $form['id'], false ), | |
| 211 | + esc_attr( $form['name'] ), | |
| 212 | + ( ! empty( $form['format'] ) ? esc_attr( $form['format'] ) : 'inline' ) | |
| 213 | + ); | |
| 214 | + } | |
| 215 | + } | |
| 216 | + | |
| 217 | + // Close select. | |
| 218 | + $html .= '</select>'; | |
| 219 | + | |
| 220 | + // If no description is provided, return the select field now. | |
| 221 | + if ( ! $description ) { | |
| 222 | + return $html; | |
| 223 | + } | |
| 224 | + | |
| 225 | + // Append description before returning field. | |
| 226 | + if ( ! is_array( $description ) ) { | |
| 227 | + return $html . '<p class="description">' . $description . '</p>'; | |
| 228 | + } | |
| 229 | + | |
| 230 | + // Return description lines in a paragraph, using breaklines for each description entry in the array. | |
| 231 | + return $html . '<p class="description">' . implode( '<br />', $description ) . '</p>'; | |
| 232 | + | |
| 233 | + } | |
| 234 | + | |
| 235 | + /** | |
| 96 | 236 | * Returns the HTML/JS markup for the given Form ID. |
| 97 | 237 | * |
| 98 | 238 | * Legacy Forms will return HTML. |
| 99 | 239 | * Current Forms will return a <script> embed string. |
| @@ -165,14 +305,68 @@ | ||
| 165 | 305 | |
| 166 | 306 | } |
| 167 | 307 | ); |
| 168 | 308 | |
| 309 | + // Sanity check we're not in the WordPress Admin interface. | |
| 310 | + // Some third party REST API Plugins seem to load frontend Posts, which would result in a wp_die() as | |
| 311 | + // the output Plugin class (rightly) isn't initialized in the backend. | |
| 312 | + if ( is_admin() ) { | |
| 313 | + return ''; | |
| 314 | + } | |
| 315 | + | |
| 316 | + // Don't output the global non-inline form, if defined, because | |
| 317 | + // a non-inline form was specified at either Post/Page default level, Post/Page level | |
| 318 | + // or Post Category level. | |
| 319 | + // This prevents multiple non-inline forms loading. | |
| 320 | + remove_action( 'wp_footer', array( WP_ConvertKit()->get_class( 'output' ), 'output_global_non_inline_form' ), 1 ); | |
| 321 | + | |
| 169 | 322 | // Don't return a script for output, as it'll be output in the site's footer. |
| 170 | 323 | return ''; |
| 171 | 324 | } |
| 172 | 325 | |
| 173 | 326 | // 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>'; | |
| 327 | + | |
| 328 | + // Define script key-value pairs. | |
| 329 | + $script = array( | |
| 330 | + 'async' => true, | |
| 331 | + 'data-uid' => $this->resources[ $id ]['uid'], | |
| 332 | + 'src' => $this->resources[ $id ]['embed_js'], | |
| 333 | + ); | |
| 334 | + | |
| 335 | + /** | |
| 336 | + * Filter the form <script> key/value pairs immediately before the script is output. | |
| 337 | + * | |
| 338 | + * @since 2.4.5 | |
| 339 | + * | |
| 340 | + * @param array $script Form script key/value pairs to output as <script> tag. | |
| 341 | + */ | |
| 342 | + $script = apply_filters( 'convertkit_resource_forms_output_script', $script ); | |
| 343 | + | |
| 344 | + // Build script output. | |
| 345 | + $output = '<script'; | |
| 346 | + foreach ( $script as $attribute => $value ) { | |
| 347 | + // If the value is true, just output the attribute. | |
| 348 | + if ( $value === true ) { | |
| 349 | + $output .= ' ' . esc_attr( $attribute ); | |
| 350 | + continue; | |
| 351 | + } | |
| 352 | + | |
| 353 | + // Sanitize attribute and value. | |
| 354 | + $attribute = esc_attr( $attribute ); | |
| 355 | + $value = ( $attribute === 'src' ? esc_url( $value ) : esc_attr( $value ) ); | |
| 356 | + | |
| 357 | + // Output the attribute and value. | |
| 358 | + $output .= ' ' . $attribute; | |
| 359 | + | |
| 360 | + // Output the value, if it's not a blank string. | |
| 361 | + if ( strlen( $value ) > 0 ) { | |
| 362 | + $output .= '="' . $value . '"'; | |
| 363 | + } | |
| 364 | + } | |
| 365 | + $output .= '></script>'; | |
| 366 | + | |
| 367 | + // Return script output. | |
| 368 | + return $output; | |
| 175 | 369 | |
| 176 | 370 | } |
| 177 | 371 | |
| 178 | 372 | } |