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