- Timestamp:
- 12/06/2026 04:19:19 AM (less than one hour ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
marqueex/trunk/includes/Integrations/Shortcode/ShortcodeIntegration.php
r3588111 r3847136 5 5 use Wpxero\Marqueex\Traits\Singleton; 6 6 use Wpxero\Marqueex\Core\Settings; 7 use Wpxero\Marqueex\Core\BlockRender; 7 8 8 9 if (!defined('ABSPATH')) { … … 44 45 45 46 /** 46 * Render Marquee Slider Shortcode 47 * [marqueex_slider] — a Text Marquee. 48 * 49 * The shortcodes used to print bare legacy markup that no stylesheet or 50 * script targets, so they rendered as static text. Each one now renders the 51 * matching block, which brings its own styles, view script, reduced-motion 52 * handling and accessible duplicate track. 53 * 54 * @param array|string $atts 55 * @param string|null $content 56 * @return string 47 57 */ 48 58 public function render_slider_shortcode($atts, $content = null) { 49 59 $atts = shortcode_atts([ 50 'speed' => '30',51 'direction' => 'left',60 'speed' => '30', 61 'direction' => 'left', 52 62 'pause_on_hover' => 'true', 53 'class' => '', 54 'id' => '', 63 'gap' => '', 64 'class' => '', 65 'id' => '', 55 66 ], $atts, 'marqueex_slider'); 56 67 57 $unique_id = 'marqueex-slider-' . uniqid(); 58 $class = !empty($atts['class']) ? ' ' . esc_attr($atts['class']) : ''; 59 $id = !empty($atts['id']) ? ' id="' . esc_attr($atts['id']) . '"' : ''; 60 61 $output = sprintf( 62 '<div%s class="marqueex-slider%s" data-speed="%s" data-direction="%s" data-hover-paused-enabled="%s">%s</div>', 63 $id, 64 $class, 65 esc_attr($atts['speed']), 66 esc_attr($atts['direction']), 67 esc_attr($atts['pause_on_hover']), 68 do_shortcode($content) 69 ); 70 71 return $output; 72 } 73 74 /** 75 * Render News Ticker Shortcode 68 return $this->render_as_block('marqueex/text-marquee', 'text_marquee', $atts, $content); 69 } 70 71 /** 72 * [marqueex_ticker] — a News Ticker with a label. 73 * 74 * @param array|string $atts 75 * @param string|null $content 76 * @return string 76 77 */ 77 78 public function render_ticker_shortcode($atts, $content = null) { 78 79 $atts = shortcode_atts([ 79 'speed' => '30',80 'direction' => 'left',80 'speed' => '30', 81 'direction' => 'left', 81 82 'pause_on_hover' => 'true', 82 'label' => 'BREAKING', 83 'show_label' => 'true', 84 'class' => '', 85 'id' => '', 83 'label' => 'BREAKING', 84 'show_label' => 'true', 85 'gap' => '', 86 'class' => '', 87 'id' => '', 86 88 ], $atts, 'marqueex_ticker'); 87 89 88 $unique_id = 'marqueex-ticker-' . uniqid(); 89 $class = !empty($atts['class']) ? ' ' . esc_attr($atts['class']) : ''; 90 $id = !empty($atts['id']) ? ' id="' . esc_attr($atts['id']) . '"' : ''; 91 92 $output = '<div class="marqueex-news-ticker-wrapper">'; 93 94 if ($atts['show_label'] === 'true') { 95 $output .= sprintf( 96 '<div class="marqueex-ticker-label">%s</div>', 97 esc_html($atts['label']) 98 ); 99 } 100 101 $output .= sprintf( 102 '<div%s class="marqueex-news-ticker%s" data-speed="%s" data-direction="%s" data-hover-paused-enabled="%s">%s</div>', 103 $id, 104 $class, 105 esc_attr($atts['speed']), 106 esc_attr($atts['direction']), 107 esc_attr($atts['pause_on_hover']), 108 do_shortcode($content) 109 ); 110 111 $output .= '</div>'; 112 113 return $output; 114 } 115 116 /** 117 * Render Infinite Slider Shortcode 90 return $this->render_as_block('marqueex/news-ticker', 'news_ticker', $atts, $content); 91 } 92 93 /** 94 * [marqueex_infinite] — a continuous strip of items with a configurable gap. 95 * 96 * Rendered as a Text Marquee: the Infinite Slider block is built from inner 97 * blocks, which a shortcode's text content cannot express. 98 * 99 * @param array|string $atts 100 * @param string|null $content 101 * @return string 118 102 */ 119 103 public function render_infinite_shortcode($atts, $content = null) { 120 104 $atts = shortcode_atts([ 121 'speed' => '30',122 'direction' => 'left',105 'speed' => '30', 106 'direction' => 'left', 123 107 'pause_on_hover' => 'true', 124 'gap' => '20',125 'class' => '',126 'id' => '',108 'gap' => '20', 109 'class' => '', 110 'id' => '', 127 111 ], $atts, 'marqueex_infinite'); 128 112 129 $unique_id = 'marqueex-infinite-' . uniqid(); 130 $class = !empty($atts['class']) ? ' ' . esc_attr($atts['class']) : ''; 131 $id = !empty($atts['id']) ? ' id="' . esc_attr($atts['id']) . '"' : ''; 132 133 $output = sprintf( 134 '<div%s class="marqueex-infinite-slider%s" data-speed="%s" data-direction="%s" data-hover-paused-enabled="%s" data-gap="%s">%s</div>', 135 $id, 136 $class, 137 esc_attr($atts['speed']), 138 esc_attr($atts['direction']), 139 esc_attr($atts['pause_on_hover']), 140 esc_attr($atts['gap']), 141 do_shortcode($content) 113 return $this->render_as_block('marqueex/text-marquee', 'infinite_slider', $atts, $content); 114 } 115 116 /** 117 * Render a shortcode through a registered MarqueeX block. 118 * 119 * @param string $block_name Block to render. 120 * @param string $element Key under builder_support.shortcode.elements. 121 * @param array $atts Normalised shortcode attributes. 122 * @param string|null $content Enclosed content; items are separated by "|". 123 * @return string 124 */ 125 private function render_as_block($block_name, $element, array $atts, $content) { 126 if (!Settings::get('builder_support.shortcode.elements.' . $element, true)) { 127 return ''; 128 } 129 130 $items = $this->parse_items((string) $content); 131 if (empty($items)) { 132 return ''; 133 } 134 135 // The block may be switched off for the block editor; BlockRender 136 // registers it on demand. Plain text only if it is not built at all. 137 if (!BlockRender::ensure_registered($block_name)) { 138 return $this->wrap($atts, '<div class="marqueex-shortcode-fallback">' . esc_html(implode(' · ', wp_list_pluck($items, 'text'))) . '</div>'); 139 } 140 141 $direction = strtolower((string) $atts['direction']); 142 $options = [ 143 'speed' => max(10, min(100, absint($atts['speed']))), 144 'reverse' => in_array($direction, ['right', 'down'], true), 145 'pauseOnHover' => $this->truthy($atts['pause_on_hover']), 146 ]; 147 148 if ('' !== $atts['gap']) { 149 // The shortcode takes pixels; the blocks space items in rem. 150 $options['itemSpacing'] = round(max(0, (float) $atts['gap']) / 16, 3); 151 } 152 153 $attrs = [ 154 'contentType' => 'custom', 155 'items' => $items, 156 'options' => $options, 157 ]; 158 159 if ('marqueex/text-marquee' === $block_name) { 160 $attrs['options']['vertical'] = in_array($direction, ['up', 'down'], true); 161 } 162 163 if ('marqueex/news-ticker' === $block_name) { 164 $show = $this->truthy($atts['show_label']); 165 $attrs['label'] = [ 166 'show' => $show, 167 'text' => sanitize_text_field($atts['label']), 168 'tag' => 'span', 169 ]; 170 } 171 172 return $this->wrap($atts, BlockRender::render($block_name, $attrs)); 173 } 174 175 /** 176 * Split enclosed content into marquee items. 177 * 178 * Items are separated by "|". An item that is a single link keeps its URL. 179 * 180 * @param string $content 181 * @return array<int, array{text: string, url: string, isExternal: bool, nofollow: bool}> 182 */ 183 private function parse_items($content) { 184 $content = do_shortcode($content); 185 $items = []; 186 187 foreach (explode('|', $content) as $piece) { 188 $url = ''; 189 if (preg_match('/<a\s[^>]*href=(["\'])(.*?)\1/i', $piece, $match)) { 190 $url = esc_url_raw($match[2]); 191 } 192 193 $text = trim(preg_replace('/\s+/u', ' ', wp_strip_all_tags($piece))); 194 if ('' === $text) { 195 continue; 196 } 197 198 $items[] = [ 199 'text' => $text, 200 'url' => $url, 201 'isExternal' => false, 202 'nofollow' => false, 203 ]; 204 } 205 206 return $items; 207 } 208 209 /** 210 * Keep the shortcode's own class/id attributes on a wrapper. 211 * 212 * @param array $atts 213 * @param string $html 214 * @return string 215 */ 216 private function wrap(array $atts, $html) { 217 if ('' === $atts['class'] && '' === $atts['id']) { 218 return $html; 219 } 220 221 $classes = array_filter(array_map('sanitize_html_class', preg_split('/\s+/', (string) $atts['class']))); 222 223 return sprintf( 224 '<div%s%s>%s</div>', 225 '' !== $atts['id'] ? ' id="' . esc_attr($atts['id']) . '"' : '', 226 $classes ? ' class="' . esc_attr(implode(' ', $classes)) . '"' : '', 227 $html 142 228 ); 143 144 return $output; 229 } 230 231 /** 232 * @param mixed $value Shortcode attribute value. 233 * @return bool 234 */ 235 private function truthy($value) { 236 return in_array(strtolower(trim((string) $value)), ['1', 'true', 'yes', 'on'], true); 145 237 } 146 238 … … 155 247 return; 156 248 } 249 250 // The help opens in a jQuery UI dialog; WordPress doesn't load it by default. 251 // Footer scripts and late styles have not printed yet at admin_footer. 252 wp_enqueue_script('jquery-ui-dialog'); 253 wp_enqueue_style('wp-jquery-ui-dialog'); 157 254 158 255 ?> … … 173 270 <ul> 174 271 <li><strong>speed:</strong> <?php esc_html_e('Animation speed (10-100)', 'marqueex'); ?></li> 175 <li><strong>direction:</strong> <?php esc_html_e('left , right, up, down', 'marqueex'); ?></li>272 <li><strong>direction:</strong> <?php esc_html_e('left or right (up and down for marqueex_slider)', 'marqueex'); ?></li> 176 273 <li><strong>pause_on_hover:</strong> <?php esc_html_e('true or false', 'marqueex'); ?></li> 177 274 <li><strong>label:</strong> <?php esc_html_e('Label text for ticker (default: BREAKING)', 'marqueex'); ?></li> 178 275 <li><strong>show_label:</strong> <?php esc_html_e('Show/hide label (true or false)', 'marqueex'); ?></li> 179 276 <li><strong>gap:</strong> <?php esc_html_e('Gap between items in pixels', 'marqueex'); ?></li> 277 <li><strong>|</strong> <?php esc_html_e('Separate items in the content with a vertical bar. A link around an item is kept.', 'marqueex'); ?></li> 180 278 <li><strong>class:</strong> <?php esc_html_e('Additional CSS classes', 'marqueex'); ?></li> 181 279 <li><strong>id:</strong> <?php esc_html_e('Custom ID attribute', 'marqueex'); ?></li>
Note:
See TracChangeset
for help on using the changeset viewer.