PluginProbe
GetResponse Forms by Optin Cat / 1.4.0
GetResponse Forms by Optin Cat v1.4.0
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.4.0, at includes/eoi-shortcode.php

452 lines 13.7 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 ( 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 if ( array_key_exists( $form_id, $this->prerequisites ) ) {
74 return $this->prerequisites[ $form_id ];
75 }
76
77 $head = '';
78
79 $fca_eoi_meta = get_post_meta( $form_id, 'fca_eoi', true );
80 if ( ! $fca_eoi_meta ) {
81 return $head;
82 }
83
84 $layout_id = $fca_eoi_meta['layout'];
85 $layout = new EasyOptInsLayout( $layout_id );
86 $scss_path = $layout->path_to_resource( 'layout', 'scss' );
87
88 $scss = $layout->new_scss_compiler();
89
90 if ( file_exists( $scss_path ) ) {
91 $head .=
92 '<style>' .
93 '.fca_eoi_form p { width: auto; }' . $scss->compile(
94 sprintf( '$ltr: %s;', is_rtl() ? 'false' : 'true' ) .
95 '#fca_eoi_form_' . $form_id . '{' .
96 'input{ max-width: 9999px; }' .
97 file_get_contents( $scss_path ) .
98 '}'
99 ) .
100 '</style>';
101 }
102
103 // Add per form CSS
104 if ( ! EasyOptInsLayout::uses_new_css() ) {
105 $head .= '<style>.fca_eoi_form{ margin: auto; }</style>';
106 }
107
108 $css_for_scss = '';
109 if ( ! empty( $fca_eoi_meta[ $layout_id ] ) ) {
110 $head .= '<style>';
111 $css_for_scss .= "#fca_eoi_form_$form_id {";
112 foreach ( $fca_eoi_meta[ $layout_id ] as $selector => $declarations ) {
113 $css_for_scss .= "$selector{";
114 foreach ( $declarations as $property => $value ) {
115 if ( strlen( $value ) ) {
116 $css_for_scss .= "$property:$value !important;";
117 }
118 }
119 $css_for_scss .= '}';
120 }
121 $css_for_scss .= '}';
122 $head .= $scss->compile( $css_for_scss ) . '</style>';
123 }
124
125 if ( $layout->layout_type != 'lightbox' ) {
126 $head .= '<script>' . EasyOptInsActivity::get_instance()->get_tracking_code( $form_id ) . '</script>';
127 }
128
129 $this->prerequisites[ $form_id ] = $head;
130 return $head;
131 }
132
133 /**
134 * @param int $form_id
135 *
136 * @return string
137 */
138 private function get_prerequisites_for_form( $form_id ) {
139 $prerequisites = $this->add_prerequisites_for_form( $form_id );
140 $this->prerequisites[ $form_id ] = '';
141
142 if ( ! empty( $prerequisites ) ) {
143 $this->enqueue_assets();
144 return $prerequisites;
145 }
146
147 return '';
148 }
149
150 public function button_head() {
151 ?>
152
153 <style>
154 #fca-eoi-media-button {
155 background: url(<?php echo $this->settings['plugin_url'] . '/icon.png' ?>) 0 -1px no-repeat;
156 background-size: 16px 16px;
157 }
158 </style>
159
160 <?php
161 }
162
163 public function button() {
164 if (current_user_can( 'delete_pages')){
165
166 $button_title = __( 'Optin Cat' );
167
168 if ( version_compare( $GLOBALS['wp_version'], '3.5', '<' ) ) {
169 echo '<a href="#TB_inline?width=640&inlineId=fca-eoi-shortcode-thickbox" class="thickbox" title="' . $button_title . '">' . $button_title . '</a>';
170 } else {
171 $img = '<span class="wp-media-buttons-icon" id="fca-eoi-media-button"></span>';
172 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>';
173 }
174 }
175 }
176
177 public function button_footer() {
178 $options = array();
179
180 foreach ( get_posts( array( 'post_type' => 'easy-opt-ins', 'post_status' => 'publish', 'posts_per_page' => -1 ) ) as $post ) {
181 $form_id = $post->ID;
182 $layout = get_post_meta( $form_id, 'fca_eoi_layout', true );
183
184 if ( ! empty( $layout ) && strpos( $layout, 'postbox_' ) === 0 ) {
185 $options[ $form_id ] = empty( $post->post_title ) ? '(no title)' : $post->post_title;
186 }
187 }
188
189 ?>
190
191 <script type="text/javascript">
192 jQuery( function( $ ) {
193 $( '#fca-eoi-shortcode-insert' ).on( 'click', function() {
194 var id = $( '#fca-eoi-shortcode' ).val();
195
196 if ( '' === id ) {
197 alert( <?php echo json_encode( __( 'You must choose a form' ) ) ?> );
198 return;
199 }
200
201 window.send_to_editor( '[<?php echo $this->settings[ 'shortcode' ] ?> id="' + id + '"]' );
202 } );
203 } );
204 </script>
205 <div id="fca-eoi-shortcode-thickbox" style="display: none;">
206 <div class="wrap" style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;">
207 <p><?php _e('Use the form below to insert an Optin Cat shortcode .' ) ?></p>
208 <div>
209 <select id="fca-eoi-shortcode">
210 <option value=""><?php _e( 'Please select...' ) ?></option>
211 <?php foreach ( $options as $form_id => $title ) { ?>
212 <option value="<?php echo (int) $form_id ?>"><?php echo esc_html( $title ) ?></option>
213 <?php } ?>
214 </select>
215 </div>
216 <p class="submit">
217 <input type="button" id="fca-eoi-shortcode-insert" class="button-primary" value="<?php _e( 'Insert' ) ?>">
218 <a id="fca-eoi-shortcode-cancel" class="button-secondary" onclick="tb_remove();" title="<?php _e( 'Cancel' ) ?>"><?php _e( 'Cancel' ) ?></a>
219 </p>
220 </div>
221 </div>
222
223 <?php
224 }
225
226 public function enqueue_assets() {
227
228 if ( $this->assets_enqueued ) {
229 return;
230 }
231
232 $this->assets_enqueued = true;
233
234 $protocol = is_ssl() ? 'https' : 'http';
235
236 // Get lightboxes
237 $lightboxes = get_posts( array(
238 'post_type' => 'easy-opt-ins',
239 'posts_per_page' => -1,
240 'orderby' => 'ID',
241 'meta_key' => 'fca_eoi_layout',
242 'meta_value' => 'lightbox_',
243 'meta_compare' => 'like',
244 ) );
245
246 // Get postboxes
247 $postboxes = get_posts( array(
248 'post_type' => 'easy-opt-ins',
249 'posts_per_page' => -1,
250 'orderby' => 'ID',
251 'meta_key' => 'fca_eoi_layout',
252 'meta_value' => 'postbox_',
253 'meta_compare' => 'like',
254 ) );
255
256 // Get postboxes
257 $widgets = get_posts( array(
258 'post_type' => 'easy-opt-ins',
259 'posts_per_page' => -1,
260 'orderby' => 'ID',
261 'meta_key' => 'fca_eoi_layout',
262 'meta_value' => 'layout_',
263 'meta_compare' => 'like',
264 ) );
265
266 // Exit function if not optin form exist
267 if ( ! $lightboxes && ! $postboxes && ! $widgets ) {
268 return;
269 }
270
271 wp_enqueue_script( 'jquery' );
272
273 wp_enqueue_style( 'fca_eoi', $this->settings[ 'plugin_url' ].'/assets/style' . ( EasyOptInsLayout::uses_new_css() ? '-new' : '' ) . '.css' );
274 wp_enqueue_script( 'fca_eoi', $this->settings[ 'plugin_url' ].'/assets/script.js' );
275
276 wp_enqueue_style( 'fontawesome', $protocol . '://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.1.0/css/font-awesome.min.css' );
277
278 wp_enqueue_script( 'tooltipster', $this->settings[ 'plugin_url' ] . '/assets/vendor/tooltipster/jquery.tooltipster.min.js' );
279 wp_enqueue_style( 'tooltipster', $this->settings[ 'plugin_url' ] . '/assets/vendor/tooltipster/tooltipster.min.css' );
280
281 wp_enqueue_script( 'featherlight', $this->settings[ 'plugin_url' ] . '/assets/vendor/featherlight/release/featherlight.min.js' );
282 wp_enqueue_style( 'featherlight', $this->settings[ 'plugin_url' ] . '/assets/vendor/featherlight/release/featherlight.min.css' );
283
284 wp_localize_script(
285 'fca_eoi'
286 , 'fca_eoi'
287 , array_merge( $this->settings['error_text'], array(
288 'ajax_url' => admin_url( 'admin-ajax.php' )
289 ) )
290 );
291 }
292
293 public function shortcode_content( $atts ) {
294
295 $form_id = $atts['id'];
296
297 /**
298 * Check that we have a valid post ID
299 */
300 if( empty ( $form_id ) ) {
301 return 'Missing form ID';
302 }
303 if( ! $post = get_post( $form_id ) ) {
304 return 'Wrong form ID';
305 }
306
307 $fca_eoi_meta = get_post_meta( $form_id, 'fca_eoi', true );
308 if( ! $fca_eoi_meta ) {
309 return 'Form doesn\'t exist';
310 }
311
312 // Get template
313 $layout_id = $fca_eoi_meta[ 'layout' ];
314
315 $layout = new EasyOptInsLayout( $layout_id );
316 $layout_type = $layout->layout_type;
317 $html_path = $layout->path_to_resource( 'layout', 'html' );
318 $html_wrap_path = $layout->path_to_html_wrapper();
319
320 if ( ! file_exists( $html_path ) ) {
321 return '';
322 }
323
324 if ( EasyOptInsLayout::uses_new_css() ) {
325 $template = str_replace(
326 '{{{layout}}}',
327 file_get_contents( $html_path ),
328 file_get_contents( $html_wrap_path )
329 );
330 } else {
331 $template = file_get_contents( $html_path );
332 }
333
334 $form_wrapper = '';
335 $form_wrapper_end = '';
336
337 if ( EasyOptInsLayout::uses_new_css() && $layout->layout_type != 'lightbox' ) {
338 $form_wrapper =
339 '<div class="' .
340 'fca_eoi_form_wrapper ' .
341 $layout->layout_class . '_wrapper ' .
342 'fca_eoi_layout_' . $layout->layout_number . '_wrapper' .
343 '">';
344 $form_wrapper_end = '</div>';
345 }
346
347 // Fill template with our formatting stuff
348 $template = str_replace(
349 array(
350 '<form>',
351 '{{{description_copy}}}',
352 '{{{headline_copy}}}',
353 '{{{name_field}}}',
354 '{{{email_field}}}',
355 '{{{submit_button}}}',
356 '{{{privacy_copy}}}',
357 '{{{fatcatapps_link}}}',
358 '</form>',
359 ),
360 array(
361 sprintf(
362 $form_wrapper .
363 '<div id="fca_eoi_form_%s" class="fca_eoi_form_content">' .
364 '<form method="post" action="#" class="fca_eoi_form %s %s" ' .
365 'data-fca_eoi_list_id="%s" data-fca_eoi_thank_you_page="%s" novalidate' .
366 '>' .
367 '<input type="hidden" name="fca_eoi_form_id" value="%s" />'
368 , $form_id
369 , EasyOptInsLayout::uses_new_css()
370 ? 'fca_eoi_layout_' . $layout->layout_number
371 : 'fca_eoi_' . $layout_type
372 , EasyOptInsLayout::uses_new_css()
373 ? $layout->layout_class
374 : 'fca_eoi_' . $layout_id
375 , K::get_var( 'list_id', $fca_eoi_meta )
376 , get_permalink( K::get_var( 'thank_you_page', $fca_eoi_meta ) )
377 ? get_permalink( K::get_var( 'thank_you_page', $fca_eoi_meta ) )
378 : ''
379 , $post->ID
380 ),
381 '<div>{{{description_copy}}}</div>',
382 EasyOptInsLayout::uses_new_css()
383 ? '<div>{{{headline_copy}}}</div>'
384 : '<span>{{{headline_copy}}}</span>',
385 EasyOptInsLayout::uses_new_css()
386 ? '<input class="fca_eoi_form_input_element" type="text" name="name" placeholder="{{{name_placeholder}}}">'
387 : '<input type="text" name="name" placeholder="{{{name_placeholder}}}" />',
388 EasyOptInsLayout::uses_new_css()
389 ? '<input class="fca_eoi_form_input_element" type="email" name="email" placeholder="{{{email_placeholder}}}">'
390 : '<input type="email" name="email" placeholder="{{{email_placeholder}}}" />',
391 EasyOptInsLayout::uses_new_css()
392 ? '<input class="fca_eoi_form_button_element" type="submit" value="{{{button_copy}}}">'
393 : '<input type="submit" value="{{{button_copy}}}" />',
394 EasyOptInsLayout::uses_new_css()
395 ? '<div>{{{privacy_copy}}}</div>'
396 : '<span >{{{privacy_copy}}}</span>',
397 EasyOptInsLayout::uses_new_css()
398 ? '{{#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}}'
399 : '{{#show_fatcatapps_link}}<p class="fca_eoi_' . $layout_id . '_fatcatapps_link_wrapper"><a href="http://fatcatapps.com/eoi" target="_blank">Powered by Optin Cat</a></p>{{/show_fatcatapps_link}}',
400 '<input type="hidden" name="id" value="' . $form_id . '"><input type="hidden" name="fca_eoi" value="1"></form></div>' . $form_wrapper_end,
401 ),
402 $template
403 );
404
405 $mustache = new Mustache_Engine;
406 $output = $mustache->render(
407 $template,
408 array(
409 'headline_copy' => $fca_eoi_meta[ 'headline_copy' ],
410 'description_copy' => $fca_eoi_meta[ 'description_copy' ],
411 'privacy_copy' => $fca_eoi_meta[ 'privacy_copy' ],
412 'name_placeholder' => $fca_eoi_meta[ 'name_placeholder' ],
413 'email_placeholder' => $fca_eoi_meta[ 'email_placeholder' ],
414 'button_copy' => $fca_eoi_meta[ 'button_copy' ],
415 'show_name_field' => K::get_var( 'show_name_field', $fca_eoi_meta ),
416 'show_fatcatapps_link' => K::get_var( 'show_fatcatapps_link', $fca_eoi_meta ),
417 )
418 );
419
420 // add the fca_eoi_alter_form
421 $output = apply_filters( 'fca_eoi_alter_form'
422 , $output
423 , $fca_eoi_meta
424 );
425
426 $error_text = '';
427
428 foreach ( $fca_eoi_meta as $key => $value ) {
429 if ( strpos( $key, 'error_text_' ) === 0 ) {
430 $text = substr( $key, 11 );
431 $error_text .= 'fca_eoi[' . json_encode($text) . '] = ' . json_encode($value) . ';';
432 }
433 }
434
435 if ( ! empty( $error_text ) ) {
436 $output .=
437 '<script>' .
438 'if ( typeof fca_eoi === "undefined" ) {' .
439 'fca_eoi = {};' .
440 '}' .
441 $error_text .
442 '</script>';
443 }
444
445 $prerequisites = $this->get_prerequisites_for_form( $form_id );
446 $prerequisites = str_replace('<style', '<style scoped', $prerequisites);
447
448 // Return form with debugging information if applicable
449 return $prerequisites . $output . ( FCA_EOI_DEBUG ? @d( $fca_eoi_meta, $template ) : '' );
450 }
451 }
452