| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * A base shortcode for all lockers | |
| 3 | + * A base shortcode for all snippets | |
| 4 | 4 | * |
| 5 | 5 | * @since 1.0.0 |
| 6 | 6 | */ |
| 7 | 7 | |
| @@ -9,33 +9,94 @@ | ||
| 9 | 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | 10 | exit; |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | -class WINP_SnippetShortcode extends Wbcr_FactoryShortcodes335_Shortcode { | |
| 13 | +/** | |
| 14 | + * Base shortcode class for all snippet shortcodes | |
| 15 | + */ | |
| 16 | +class WINP_SnippetShortcode { | |
| 14 | 17 | |
| 15 | - public $manager; | |
| 18 | + /** | |
| 19 | + * Plugin instance | |
| 20 | + * | |
| 21 | + * @var WINP_Plugin | |
| 22 | + */ | |
| 23 | + public $plugin; | |
| 16 | 24 | |
| 25 | + /** | |
| 26 | + * Shortcode name(s) | |
| 27 | + * | |
| 28 | + * @var string|array<string> | |
| 29 | + */ | |
| 17 | 30 | public $shortcode_name = 'wbcr_php_snippet'; |
| 18 | 31 | |
| 19 | 32 | /** |
| 20 | - * Includes assets | |
| 33 | + * Includes assets in header | |
| 34 | + * | |
| 21 | 35 | * @var bool |
| 22 | 36 | */ |
| 23 | 37 | public $assets_in_header = true; |
| 24 | 38 | |
| 39 | + /** | |
| 40 | + * Constructor | |
| 41 | + * | |
| 42 | + * @param WINP_Plugin $plugin Plugin instance. | |
| 43 | + */ | |
| 25 | 44 | public function __construct( $plugin ) { |
| 26 | - parent::__construct( $plugin ); | |
| 45 | + $this->plugin = $plugin; | |
| 46 | + | |
| 47 | + // Ensure shortcode_name is an array. | |
| 48 | + if ( ! is_array( $this->shortcode_name ) ) { | |
| 49 | + $this->shortcode_name = [ $this->shortcode_name ]; | |
| 50 | + } | |
| 51 | + | |
| 52 | + // Register shortcode(s) with WordPress. | |
| 53 | + foreach ( $this->shortcode_name as $name ) { | |
| 54 | + if ( ! empty( $name ) ) { | |
| 55 | + add_shortcode( $name, [ $this, 'render' ] ); | |
| 56 | + } | |
| 57 | + } | |
| 58 | + | |
| 59 | + // Enqueue assets in header if needed. | |
| 60 | + if ( $this->assets_in_header ) { | |
| 61 | + add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_assets' ] ); | |
| 62 | + } | |
| 27 | 63 | } |
| 28 | 64 | |
| 29 | 65 | /** |
| 66 | + * Enqueue assets if needed. | |
| 67 | + * | |
| 68 | + * @return void | |
| 69 | + */ | |
| 70 | + public function enqueue_assets() { | |
| 71 | + // Override in child classes if needed. | |
| 72 | + } | |
| 73 | + | |
| 74 | + /** | |
| 75 | + * Shortcode render callback. | |
| 76 | + * | |
| 77 | + * @param array<string, mixed> $attr Shortcode attributes. | |
| 78 | + * @param string|null $content Shortcode content. | |
| 79 | + * @param string $tag Shortcode tag. | |
| 80 | + * | |
| 81 | + * @return string | |
| 82 | + */ | |
| 83 | + public function render( $attr, $content, $tag ) { | |
| 84 | + ob_start(); | |
| 85 | + $this->html( $attr, $content ?? '', $tag ); | |
| 86 | + $html = ob_get_clean(); | |
| 87 | + return false !== $html ? $html : ''; | |
| 88 | + } | |
| 89 | + | |
| 90 | + /** | |
| 30 | 91 | * Filter attributes |
| 31 | 92 | * |
| 32 | - * @param $attr | |
| 33 | - * @param $post_id | |
| 93 | + * @param array<string, mixed> $attr Shortcode attributes. | |
| 94 | + * @param int $post_id Post ID. | |
| 34 | 95 | * |
| 35 | - * @return mixed | |
| 96 | + * @return array<string, mixed> | |
| 36 | 97 | */ |
| 37 | - public function filterAttributes( $attr, $post_id ) { | |
| 98 | + public function filter_attributes( $attr, $post_id ) { | |
| 38 | 99 | if ( ! empty( $attr ) ) { |
| 39 | 100 | $available_tags = WINP_Helper::getMetaOption( $post_id, 'snippet_tags', null ); |
| 40 | 101 | |
| 41 | 102 | if ( ! empty( $available_tags ) ) { |
| @@ -43,9 +104,9 @@ | ||
| 43 | 104 | $available_tags = array_map( 'trim', $available_tags ); |
| 44 | 105 | } |
| 45 | 106 | |
| 46 | 107 | foreach ( $attr as $name => $value ) { |
| 47 | - $is_allow_attr = in_array( $name, array( 'id', 'title' ) ); | |
| 108 | + $is_allow_attr = in_array( $name, [ 'id', 'title' ] ); | |
| 48 | 109 | $validate_name = preg_match( '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $name ); |
| 49 | 110 | |
| 50 | 111 | if ( ! $is_allow_attr && ( ( ! empty( $available_tags ) && ! in_array( $name, $available_tags ) ) || ! $validate_name ) ) { |
| 51 | 112 | unset( $attr[ $name ] ); |
| @@ -82,17 +143,17 @@ | ||
| 82 | 143 | |
| 83 | 144 | /** |
| 84 | 145 | * Get snippet id |
| 85 | 146 | * |
| 86 | - * @param $attr | |
| 87 | - * @param $type | |
| 147 | + * @param array<string, mixed> $attr Shortcode attributes. | |
| 148 | + * @param string $type Snippet type. | |
| 88 | 149 | * |
| 89 | 150 | * @return int|null |
| 90 | 151 | */ |
| 91 | - public function getSnippetId( $attr, $type ) { | |
| 152 | + public function get_snippet_id( $attr, $type ) { | |
| 92 | 153 | $id = isset( $attr['id'] ) ? (int) $attr['id'] : null; |
| 93 | 154 | |
| 94 | - if ( $id && $type != WINP_Helper::get_snippet_type( $id ) ) { | |
| 155 | + if ( $id && WINP_Helper::get_snippet_type( $id ) !== $type ) { | |
| 95 | 156 | $id = 0; |
| 96 | 157 | } |
| 97 | 158 | |
| 98 | 159 | return $id; |
| @@ -100,16 +161,16 @@ | ||
| 100 | 161 | |
| 101 | 162 | /** |
| 102 | 163 | * Get snippet activate |
| 103 | 164 | * |
| 104 | - * @param $snippet_meta | |
| 165 | + * @param array<string, mixed> $snippet_meta Snippet metadata. | |
| 105 | 166 | * |
| 106 | 167 | * @return bool |
| 107 | 168 | */ |
| 108 | - public function getSnippetActivate( $snippet_meta ) { | |
| 109 | - // WPML Compatibility | |
| 169 | + public function get_snippet_activate( $snippet_meta ) { | |
| 170 | + // WPML Compatibility. | |
| 110 | 171 | if ( defined( 'WPML_PLUGIN_FILE' ) ) { |
| 111 | - $wpml_langs = isset( $snippet_meta[ $this->plugin->getPrefix() . 'snippet_wpml_lang' ][0] ) ? $snippet_meta[ $this->plugin->getPrefix() . 'snippet_wpml_lang' ][0] : ''; | |
| 172 | + $wpml_langs = isset( $snippet_meta['wbcr_inp_snippet_wpml_lang'][0] ) ? $snippet_meta['wbcr_inp_snippet_wpml_lang'][0] : ''; | |
| 112 | 173 | if ( $wpml_langs !== '' && defined( 'ICL_LANGUAGE_CODE' ) ) { |
| 113 | 174 | if ( ! in_array( ICL_LANGUAGE_CODE, explode( ',', $wpml_langs ) ) ) { |
| 114 | 175 | return false; |
| 115 | 176 | } |
| @@ -115,48 +176,49 @@ | ||
| 115 | 176 | } |
| 116 | 177 | } |
| 117 | 178 | } |
| 118 | 179 | |
| 119 | - return isset( $snippet_meta[ $this->plugin->getPrefix() . 'snippet_activate' ] ) && $snippet_meta[ $this->plugin->getPrefix() . 'snippet_activate' ][0]; | |
| 180 | + return isset( $snippet_meta['wbcr_inp_snippet_activate'] ) && $snippet_meta['wbcr_inp_snippet_activate'][0]; | |
| 120 | 181 | } |
| 121 | 182 | |
| 122 | 183 | /** |
| 123 | 184 | * Get snippet scope |
| 124 | 185 | * |
| 125 | - * @param $snippet_meta | |
| 186 | + * @param array<string, mixed> $snippet_meta Snippet metadata. | |
| 126 | 187 | * |
| 127 | - * @return null | |
| 188 | + * @return string|null | |
| 128 | 189 | */ |
| 129 | - public function getSnippetScope( $snippet_meta ) { | |
| 130 | - return isset( $snippet_meta[ $this->plugin->getPrefix() . 'snippet_scope' ] ) ? $snippet_meta[ $this->plugin->getPrefix() . 'snippet_scope' ][0] : null; | |
| 190 | + public function get_snippet_scope( $snippet_meta ) { | |
| 191 | + return isset( $snippet_meta['wbcr_inp_snippet_scope'] ) ? $snippet_meta['wbcr_inp_snippet_scope'][0] : null; | |
| 131 | 192 | } |
| 132 | 193 | |
| 133 | 194 | /** |
| 134 | 195 | * Get snippet content |
| 135 | 196 | * |
| 136 | - * @param WP_Post $snippet | |
| 137 | - * @param array $snippet_meta | |
| 138 | - * @param int $id | |
| 197 | + * @param WP_Post $snippet Snippet post object. | |
| 198 | + * @param array<string, mixed> $snippet_meta Snippet metadata. | |
| 199 | + * @param int $id Snippet ID. | |
| 139 | 200 | * |
| 140 | - * @return null|string | |
| 201 | + * @return string|null | |
| 141 | 202 | */ |
| 142 | - public function getSnippetContent( $snippet, $snippet_meta, $id ) { | |
| 203 | + public function get_snippet_content( $snippet, $snippet_meta, $id ) { | |
| 143 | 204 | $snippet_code = WINP_Helper::get_snippet_code( $snippet ); |
| 144 | 205 | |
| 145 | - if ( WINP_Plugin::app()->getOption( 'execute_shortcode' ) ) { | |
| 206 | + if ( get_option( 'wbcr_inp_execute_shortcode' ) ) { | |
| 146 | 207 | $snippet_code = do_shortcode( $snippet_code ); |
| 147 | 208 | } |
| 148 | 209 | |
| 149 | - return WINP_Plugin::app()->getExecuteObject()->prepareCode( $snippet_code, $id ); | |
| 210 | + return WINP_Plugin::app()->get_execute_object()->prepareCode( $snippet_code, $id ); | |
| 150 | 211 | } |
| 151 | 212 | |
| 152 | 213 | /** |
| 153 | 214 | * Content render |
| 154 | 215 | * |
| 155 | - * @param array $attr | |
| 156 | - * @param string $content | |
| 157 | - * @param string $tag | |
| 216 | + * @param array<string, mixed> $attr Shortcode attributes. | |
| 217 | + * @param string $content Shortcode content. | |
| 218 | + * @param string $tag Shortcode tag. | |
| 219 | + * | |
| 220 | + * @return void | |
| 158 | 221 | */ |
| 159 | 222 | public function html( $attr, $content, $tag ) { |
| 160 | - | |
| 161 | 223 | } |
| 162 | -} | |
| 224 | +} | |