| 1 |
<?php |
| 2 |
/** |
| 3 |
* Universal Shortcode |
| 4 |
*/ |
| 5 |
|
| 6 |
// Exit if accessed directly |
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
class WINP_SnippetShortcodeJs extends WINP_SnippetShortcode { |
| 12 |
|
| 13 |
public $shortcode_name = 'wbcr_js_snippet'; |
| 14 |
|
| 15 |
/** |
| 16 |
* Content render |
| 17 |
* |
| 18 |
* @param array $attr |
| 19 |
* @param string $content |
| 20 |
* @param string $tag |
| 21 |
*/ |
| 22 |
public function html( $attr, $content, $tag ) { |
| 23 |
$id = $this->get_snippet_id( $attr, WINP_SNIPPET_TYPE_JS ); |
| 24 |
|
| 25 |
if ( ! $id && ( current_user_can( 'manage_options' ) || ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ) ) { |
| 26 |
/* translators: %s: Shortcode tag name */ |
| 27 |
echo '<span style="color:red">' . sprintf( esc_html__( '[%s]: PHP snippets error (not passed the snippet ID)', 'insert-php' ), esc_html( $tag ) ) . '</span>'; |
| 28 |
|
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
$snippet = get_post( $id ); |
| 33 |
$snippet_meta = get_post_meta( $id, '' ); |
| 34 |
|
| 35 |
if ( ! $snippet || empty( $snippet_meta ) ) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
$attrs = $this->filter_attributes( $attr, $id ); |
| 40 |
|
| 41 |
// Let users pass arbitrary variables, through shortcode attributes. |
| 42 |
// @since 2.4.0 |
| 43 |
$vars = ''; |
| 44 |
foreach ( $attrs as $var => $value ) { |
| 45 |
$vars .= PHP_EOL . "var {$var} = \"{$value}\";"; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
$is_activate = $this->get_snippet_activate( $snippet_meta ); |
| 50 |
$snippet_content = $this->get_snippet_content( $snippet, $snippet_meta, $id ); |
| 51 |
$snippet_scope = $this->get_snippet_scope( $snippet_meta ); |
| 52 |
$is_condition = WINP_Plugin::app()->get_execute_object()->checkCondition( $id ); |
| 53 |
|
| 54 |
if ( ! $is_activate || empty( $snippet_content ) || $snippet_scope != 'shortcode' || ! $is_condition ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
// Track shortcode execution. |
| 59 |
WINP_Plugin::app()->get_execute_object()->track_shortcode_snippet( $id ); |
| 60 |
|
| 61 |
echo "<script type='text/javascript'>{$vars}</script>"; // print attributes |
| 62 |
echo WINP_Execute_Snippet::getJsCssSnippetData( $id ); |
| 63 |
} |
| 64 |
} |
| 65 |
|