| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
class WINP_Snippet { |
| 8 |
|
| 9 |
protected $id; |
| 10 |
|
| 11 |
protected $type; |
| 12 |
|
| 13 |
public function __construct( WP_Post $snippet ) { |
| 14 |
$this->id = $snippet->ID; |
| 15 |
|
| 16 |
// todo: to refactor |
| 17 |
$this->type = WINP_Helper::get_snippet_type( $this->id ); |
| 18 |
} |
| 19 |
|
| 20 |
public static function get_snippet( $post_id ) { |
| 21 |
$post_id = (int) $post_id; |
| 22 |
|
| 23 |
if ( ! $post_id ) { |
| 24 |
$snippet = get_post( $post_id ); |
| 25 |
|
| 26 |
if ( $snippet ) { |
| 27 |
return new self( $snippet ); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
return null; |
| 32 |
} |
| 33 |
|
| 34 |
public function is_allowed() { |
| 35 |
// If the user has prohibited the insertion of unfiltered HTML, |
| 36 |
// we prohibit the execution of snippets. |
| 37 |
if ( ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML ) |
| 38 |
&& ! in_array( |
| 39 |
$this->type, |
| 40 |
[ |
| 41 |
WINP_SNIPPET_TYPE_TEXT, |
| 42 |
WINP_SNIPPET_TYPE_AD, |
| 43 |
WINP_SNIPPET_TYPE_CSS, |
| 44 |
] |
| 45 |
) ) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
return true; |
| 50 |
} |
| 51 |
|
| 52 |
public function get_meta( $key ) { |
| 53 |
return get_post_meta( $this->id, 'wbcr_inp_' . $key, true ); |
| 54 |
} |
| 55 |
|
| 56 |
public function get_scope() { |
| 57 |
return $this->get_meta( 'snippet_scope' ); |
| 58 |
} |
| 59 |
|
| 60 |
public function get_content() { |
| 61 |
return $this->get_meta( 'snippet_code' ); |
| 62 |
} |
| 63 |
|
| 64 |
public function is_active() { |
| 65 |
// WPML Compatibility |
| 66 |
if ( defined( 'WPML_PLUGIN_FILE' ) ) { |
| 67 |
$wpml_langs = $this->get_meta( 'snippet_wpml_lang' ); |
| 68 |
|
| 69 |
if ( $wpml_langs !== '' && defined( 'ICL_LANGUAGE_CODE' ) ) { |
| 70 |
if ( ! in_array( ICL_LANGUAGE_CODE, explode( ',', $wpml_langs ) ) ) { |
| 71 |
return false; |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
// todo: протестировать |
| 77 |
return $this->get_meta( 'snippet_activate' ); |
| 78 |
} |
| 79 |
} |
| 80 |
|