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

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