PluginProbe
GetResponse Forms by Optin Cat / 1.5.4
GetResponse Forms by Optin Cat v1.5.4
1.3.4 1.3.5 1.3.6 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.1 1.6.2 1.6.3 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 2.0.0 All 36 releases
getresponse / includes / eoi-shortcode.php

eoi-shortcode.php in GetResponse Forms by Optin Cat 1.5.4, at includes/eoi-shortcode.php

439 lines 13.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class EasyOptInsShortcodes {
4
5 var $settings;
6 var $prerequisites = array();
7 var $assets_enqueued = false;
8
9 public function __construct( $settings = array() ) {
10 global $pagenow, $typenow;
11
12 $this->settings = $settings;
13
14 // Add shortcode
15 add_shortcode( $this->settings[ 'shortcode' ], array( $this, 'shortcode_content' ) );
16
17 // Add shortcode aliases
18 foreach ( $settings[ 'shortcode_aliases' ] as $shortcode) {
19 add_shortcode( $shortcode, array( $this, 'shortcode_content' ) );
20 }
21
22 // Add shortcode generator button
23 if ( FCA_EOI_EDITION != 'email_popup' && in_array( $pagenow, array( 'post.php', 'page.php', 'post-new.php', 'post-edit.php' ) ) && $typenow != 'download' ) {
24 add_action( 'admin_head', array( $this, 'button_head' ) );
25 add_action( 'media_buttons', array( $this, 'button' ), 1000 );
26 add_action( 'admin_footer', array( $this, 'button_footer' ) );
27 }
28
29 if ( ! is_admin() ) {
30 add_action( 'wp', array( $this, 'parse_shortcodes' ), 11 );
31 }
32 }
33
34 public function wp_head() {
35 foreach ( $this->prerequisites as $form_id => $head ) {
36 echo $head;
37 $this->prerequisites[ $form_id ] = '';
38 }
39 }
40
41 public function parse_shortcodes() {
42 global $post;
43
44 if ( empty( $post ) ) {
45 return;
46 }
47
48 if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $post->post_content, $matches )
49 && array_key_exists( 2, $matches )
50 && array_key_exists( 3, $matches )
51 && in_array( $this->settings['shortcode'], $matches[2] )
52 ) {
53 foreach ( $matches[3] as $params ) {
54 $params = shortcode_parse_atts( $params );
55 if ( ! empty( $params['id'] ) ) {
56 $this->add_prerequisites_for_form( (int) $params['id'] );
57 }
58 }
59 }
60
61 if ( ! empty( $this->prerequisites ) ) {
62 $this->enqueue_assets();
63 add_action( 'wp_head', array( $this, 'wp_head' ) );
64 }
65 }
66
67 /**
68 * @param int $form_id
69 *
70 * @return string
71 */
72 private function add_prerequisites_for_form( $form_id ) {
73 $head = get_post_meta( $form_id, 'fca_eoi_head', true );
74 if ( !empty ( $head ) ) {
75 return $head;
76 } else {
77
78 if ( array_key_exists( $form_id, $this->prerequisites ) ) {
79 return $this->prerequisites[ $form_id ];
80 }
81
82 $head = '';
83
84 $fca_eoi_meta = get_post_meta( $form_id, 'fca_eoi', true );
85 if ( ! $fca_eoi_meta ) {
86 return $head;
87 }
88
89 $layout_id = $fca_eoi_meta['layout'];
90 $layout = new EasyOptInsLayout( $layout_id );
91 $scss_path = $layout->path_to_resource( 'layout', 'scss' );
92
93 $scss = $layout->new_scss_compiler();
94
95 if ( file_exists( $scss_path ) ) {
96 $head .=
97 '<style>' .
98 '.fca_eoi_form p { width: auto; }' . $scss->compile(
99 sprintf( '$ltr: %s;', is_rtl() ? 'false' : 'true' ) .
100 '#fca_eoi_form_' . $form_id . '{' .
101 'input{ max-width: 9999px; }' .
102 file_get_contents( $scss_path ) .
103 '}'
104 ) .
105 '</style>';
106 }
107
108 // Add per form CSS
109 $head .= '<style>.fca_eoi_form{ margin: auto; }</style>';
110
111 $css_for_scss = '';
112 if ( ! empty( $fca_eoi_meta[ $layout_id ] ) ) {
113 $head .= '<style>';
114 $css_for_scss .= "#fca_eoi_form_$form_id {";
115 foreach ( $fca_eoi_meta[ $layout_id ] as $selector => $declarations ) {
116 $css_for_scss .= "$selector{";
117 foreach ( $declarations as $property => $value ) {
118 if ( strlen( $value ) ) {
119 $css_for_scss .= "$property:$value !important;";
120 }
121 }
122 $css_for_scss .= '}';
123 }
124 $css_for_scss .= '}';
125 $head .= $scss->compile( $css_for_scss ) . '</style>';
126 }
127
128 if ( $layout->layout_type != 'lightbox' ) {
129 $head .= '<script>' . EasyOptInsActivity::get_instance()->get_tracking_code( $form_id ) . '</script>';
130 }
131
132 $this->prerequisites[ $form_id ] = $head;
133
134 delete_post_meta( $form_id, 'fca_eoi_head' );
135 add_post_meta( $form_id, 'fca_eoi_head', $head );
136
137
138 return $head;
139
140 }
141
142 }
143
144 /**
145 * @param int $form_id
146 *
147 * @return string
148 */
149 private function get_prerequisites_for_form( $form_id ) {
150 $prerequisites = $this->add_prerequisites_for_form( $form_id );
151
152 $this->prerequisites[ $form_id ] = '';
153
154 if ( ! empty( $prerequisites ) ) {
155 $this->enqueue_assets();
156 return $prerequisites;
157 }
158
159 return '';
160 }
161
162 public function button_head() {
163 ?>
164
165 <style>
166 #fca-eoi-media-button {
167 background: url(<?php echo FCA_EOI_PLUGIN_URL . '/icon.png' ?>) 0 -1px no-repeat;
168 background-size: 16px 16px;
169 }
170 </style>
171
172 <?php
173 }
174
175 public function button() {
176 if (current_user_can( 'delete_pages')){
177
178 $button_title = __( 'Optin Cat' );
179
180 if ( version_compare( $GLOBALS['wp_version'], '3.5', '<' ) ) {
181 echo '<a href="#TB_inline?width=640&inlineId=fca-eoi-shortcode-thickbox" class="thickbox" title="' . $button_title . '">' . $button_title . '</a>';
182 } else {
183 $img = '<span class="wp-media-buttons-icon" id="fca-eoi-media-button"></span>';
184 echo '<a href="#TB_inline?width=640&inlineId=fca-eoi-shortcode-thickbox" class="thickbox button" title="' . $button_title . '" style="padding-left: .4em;">' . $img . $button_title . '</a>';
185 }
186 }
187 }
188
189 public function button_footer() {
190 $options = array();
191
192 foreach ( get_posts( array( 'post_type' => 'easy-opt-ins', 'post_status' => 'publish', 'posts_per_page' => -1 ) ) as $post ) {
193 $form_id = $post->ID;
194 $layout = get_post_meta( $form_id, 'fca_eoi_layout', true );
195
196 if ( ! empty( $layout ) && strpos( $layout, 'postbox_' ) === 0 ) {
197 $options[ $form_id ] = empty( $post->post_title ) ? '(no title)' : $post->post_title;
198 }
199 }
200
201 ?>
202
203 <script type="text/javascript">
204 jQuery( function( $ ) {
205 $( '#fca-eoi-shortcode-insert' ).on( 'click', function() {
206 var id = $( '#fca-eoi-shortcode' ).val();
207
208 if ( '' === id ) {
209 alert( <?php echo json_encode( __( 'You must choose a form' ) ) ?> );
210 return;
211 }
212
213 window.send_to_editor( '[<?php echo $this->settings[ 'shortcode' ] ?> id="' + id + '"]' );
214 } );
215 } );
216 </script>
217 <div id="fca-eoi-shortcode-thickbox" style="display: none;">
218 <div class="wrap" style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;">
219 <p><?php _e('Use the form below to insert an Optin Cat shortcode .' ) ?></p>
220 <div>
221 <select id="fca-eoi-shortcode">
222 <option value=""><?php _e( 'Please select...' ) ?></option>
223 <?php foreach ( $options as $form_id => $title ) { ?>
224 <option value="<?php echo (int) $form_id ?>"><?php echo esc_html( $title ) ?></option>
225 <?php } ?>
226 </select>
227 </div>
228 <p class="submit">
229 <input type="button" id="fca-eoi-shortcode-insert" class="button-primary" value="<?php _e( 'Insert' ) ?>">
230 <a id="fca-eoi-shortcode-cancel" class="button-secondary" onclick="tb_remove();" title="<?php _e( 'Cancel' ) ?>"><?php _e( 'Cancel' ) ?></a>
231 </p>
232 </div>
233 </div>
234
235 <?php
236 }
237
238 public function enqueue_assets() {
239
240 if ( $this->assets_enqueued ) {
241 return;
242 }
243
244 $this->assets_enqueued = true;
245
246 $protocol = is_ssl() ? 'https' : 'http';
247
248 // Get lightboxes
249 $lightboxes = get_posts( array(
250 'post_type' => 'easy-opt-ins',
251 'posts_per_page' => -1,
252 'orderby' => 'ID',
253 'meta_key' => 'fca_eoi_layout',
254 'meta_value' => 'lightbox_',
255 'meta_compare' => 'like',
256 ) );
257
258 // Get postboxes
259 $postboxes = get_posts( array(
260 'post_type' => 'easy-opt-ins',
261 'posts_per_page' => -1,
262 'orderby' => 'ID',
263 'meta_key' => 'fca_eoi_layout',
264 'meta_value' => 'postbox_',
265 'meta_compare' => 'like',
266 ) );
267
268 // Get postboxes
269 $widgets = get_posts( array(
270 'post_type' => 'easy-opt-ins',
271 'posts_per_page' => -1,
272 'orderby' => 'ID',
273 'meta_key' => 'fca_eoi_layout',
274 'meta_value' => 'layout_',
275 'meta_compare' => 'like',
276 ) );
277
278 // Exit function if not optin form exist
279 if ( ! $lightboxes && ! $postboxes && ! $widgets ) {
280 return;
281 }
282
283 wp_enqueue_script( 'jquery' );
284
285 wp_enqueue_style( 'fca_eoi', FCA_EOI_PLUGIN_URL . '/assets/style-new.css' );
286 wp_enqueue_script( 'fca_eoi_script_js', FCA_EOI_PLUGIN_URL . '/assets/script.js' );
287
288 wp_enqueue_style( 'fontawesome', $protocol . '://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.1.0/css/font-awesome.min.css' );
289
290 wp_enqueue_script( 'tooltipster', FCA_EOI_PLUGIN_URL . '/assets/vendor/tooltipster/jquery.tooltipster.min.js' );
291 wp_enqueue_style( 'tooltipster', FCA_EOI_PLUGIN_URL . '/assets/vendor/tooltipster/tooltipster.min.css' );
292
293 wp_enqueue_script( 'featherlight', FCA_EOI_PLUGIN_URL . '/assets/vendor/featherlight/release/featherlight.min.js' );
294 wp_enqueue_style( 'featherlight', FCA_EOI_PLUGIN_URL . '/assets/vendor/featherlight/release/featherlight.min.css' );
295
296 //PASS VARIABLES TO JAVASCRIPT
297 wp_localize_script( 'fca_eoi_script_js', 'fca_eoi', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
298 }
299
300 public function shortcode_content( $atts ) {
301
302 $form_id = $atts['id'];
303
304 /**
305 * Check that we have a valid post ID
306 */
307 if( empty ( $form_id ) ) {
308 return 'Missing form ID';
309 }
310 if( ! $post = get_post( $form_id ) ) {
311 return 'Wrong form ID';
312 }
313
314 $fca_eoi_meta = get_post_meta( $form_id, 'fca_eoi', true );
315 if( ! $fca_eoi_meta ) {
316 return 'Form doesn\'t exist';
317 }
318
319 // Get template
320 $layout_id = $fca_eoi_meta[ 'layout' ];
321
322 $layout = new EasyOptInsLayout( $layout_id );
323 $layout_type = $layout->layout_type;
324 $html_path = $layout->path_to_resource( 'layout', 'html' );
325 $html_wrap_path = $layout->path_to_html_wrapper();
326
327 if ( ! file_exists( $html_path ) ) {
328 return '';
329 }
330
331
332 $template = str_replace(
333 '{{{layout}}}',
334 file_get_contents( $html_path ),
335 file_get_contents( $html_wrap_path )
336 );
337
338
339 $form_wrapper = '';
340 $form_wrapper_end = '';
341
342 if ( $layout->layout_type != 'lightbox' ) {
343 $form_wrapper =
344 '<div class="' .
345 'fca_eoi_form_wrapper ' .
346 $layout->layout_class . '_wrapper ' .
347 'fca_eoi_layout_' . $layout->layout_number . '_wrapper' .
348 '">';
349 $form_wrapper_end = '</div>';
350 }
351
352 $errorTexts = fca_eoi_get_error_texts($form_id);
353
354 //determine if ajax or thank you page redirect
355 $thanks_mode = fca_eoi_get_thanks_mode($form_id);
356 if ($thanks_mode == 'ajax') {
357 $thanks_page = K::get_var( 'thankyou_ajax', $fca_eoi_meta, '' );
358 } else {
359 $thanks_page = get_permalink( K::get_var( 'thank_you_page', $fca_eoi_meta ) );
360 }
361
362 if (empty ($thanks_page)) {
363 $thanks_mode = 'redirect';
364 $thanks_page = get_site_url();
365 }
366
367 // Fill template with our formatting stuff
368 $template = str_replace(
369 array(
370 '<form>',
371 '{{{description_copy}}}',
372 '{{{headline_copy}}}',
373 '{{{name_field}}}',
374 '{{{email_field}}}',
375 '{{{submit_button}}}',
376 '{{{privacy_copy}}}',
377 '{{{fatcatapps_link}}}',
378 '</form>',
379 ),
380 array(
381 sprintf(
382 $form_wrapper .
383 '<div id="fca_eoi_form_%s" class="fca_eoi_form_content">' .
384 '<form method="post" action="#" class="fca_eoi_form %s %s" ' .
385 'data-fca_eoi_list_id="%s" data-fca_eoi_thank_you_page="%s" data-fca_eoi_thank_you_mode="%s" novalidate' .
386 '>' .
387 '<input type="hidden" id="fca_eoi_form_id" name="fca_eoi_form_id" value="%s" />',
388 $form_id,
389 'fca_eoi_layout_' . $layout->layout_number,
390 $layout->layout_class,
391 K::get_var( 'list_id', $fca_eoi_meta ),
392 $thanks_page,
393 $thanks_mode,
394 $post->ID
395 ),
396 '<div>{{{description_copy}}}</div>',
397 '<div>{{{headline_copy}}}</div>',
398 '<input class="fca_eoi_form_input_element" type="text" name="name" placeholder="{{{name_placeholder}}}">',
399 '<input class="fca_eoi_form_input_element" type="email" name="email" placeholder="{{{email_placeholder}}}">',
400 '<div class="fca_eoi_spiner_div"><span class="fca_eoi_loading_spinner"></span></div><input class="fca_eoi_form_button_element" type="submit" value="{{{button_copy}}}">',
401 '<div>{{{privacy_copy}}}</div>',
402 '{{#show_fatcatapps_link}}<div class="fca_eoi_layout_fatcatapps_link_wrapper fca_eoi_form_text_element"><a href="http://fatcatapps.com/eoi" target="_blank">Powered by Optin Cat</a></div>{{/show_fatcatapps_link}}',
403 '<input type="hidden" name="id" value="' . $form_id . '"><input type="hidden" name="fca_eoi" value="1">
404 <input type="hidden" name="fca_eoi_error_texts_email" class="fca_eoi_error_texts_email" value="' . htmlspecialchars ($errorTexts['invalid_email']) . '">
405 <input type="hidden" name="fca_eoi_error_texts_required" class="fca_eoi_error_texts_required" value="' . htmlspecialchars ($errorTexts['field_required']) . '">' . wp_nonce_field( 'fca_eoi_submit_form', 'fca_eoi_nonce', TRUE, FALSE ) . '</form></div>' . $form_wrapper_end,
406 ),
407 $template
408 );
409
410 $mustache = new Mustache_Engine;
411 $output = $mustache->render(
412 $template,
413 array(
414 'headline_copy' => $fca_eoi_meta[ 'headline_copy' ],
415 'description_copy' => $fca_eoi_meta[ 'description_copy' ],
416 'privacy_copy' => $fca_eoi_meta[ 'privacy_copy' ],
417 'name_placeholder' => $fca_eoi_meta[ 'name_placeholder' ],
418 'email_placeholder' => $fca_eoi_meta[ 'email_placeholder' ],
419 'button_copy' => $fca_eoi_meta[ 'button_copy' ],
420 'show_name_field' => K::get_var( 'show_name_field', $fca_eoi_meta ),
421 'show_fatcatapps_link' => K::get_var( 'show_fatcatapps_link', $fca_eoi_meta ),
422 )
423 );
424
425 // add the fca_eoi_alter_form
426 $output = apply_filters( 'fca_eoi_alter_form'
427 , $output
428 , $fca_eoi_meta
429 );
430
431
432 $prerequisites = $this->get_prerequisites_for_form( $form_id );
433 $prerequisites = str_replace('<style', '<style scoped', $prerequisites);
434
435 // Return form with debugging information if applicable
436 return $prerequisites . $output . ( FCA_EOI_DEBUG ? @d( $fca_eoi_meta, $template ) : '' );
437 }
438 }
439