Manager.php
2 weeks ago
Shortcode_Abstract.php
2 weeks ago
Shortcode_Interface.php
2 weeks ago
Utils.php
2 weeks ago
Manager.php
183 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Shortcodes manager for Tribe plugins. |
| 4 | * |
| 5 | * @package Tribe\Shortcode |
| 6 | * @since 4.12.0 |
| 7 | */ |
| 8 | |
| 9 | namespace Tribe\Shortcode; |
| 10 | |
| 11 | /** |
| 12 | * Class Shortcode Manager. |
| 13 | * |
| 14 | * @since 4.12.0 |
| 15 | * |
| 16 | * @package Tribe\Shortcode |
| 17 | */ |
| 18 | class Manager { |
| 19 | |
| 20 | /** |
| 21 | * Current shortcodes. |
| 22 | * |
| 23 | * @since 4.12.9 |
| 24 | * |
| 25 | * @var array $current_shortcode An array containing the current shortcodes being executed. |
| 26 | */ |
| 27 | public $current_shortcode = []; |
| 28 | |
| 29 | /** |
| 30 | * Get the list of shortcodes available for handling. |
| 31 | * |
| 32 | * @since 4.12.0 |
| 33 | * |
| 34 | * @return array An associative array of shortcodes in the shape `[ <slug> => <class> ]` |
| 35 | */ |
| 36 | public function get_registered_shortcodes() { |
| 37 | $shortcodes = []; |
| 38 | |
| 39 | /** |
| 40 | * Allow the registering of shortcodes into the our Tribe plugins. |
| 41 | * |
| 42 | * @since 4.12.0 |
| 43 | * |
| 44 | * @var array An associative array of shortcodes in the shape `[ <slug> => <class> ]`. |
| 45 | */ |
| 46 | $shortcodes = apply_filters( 'tribe_shortcodes', $shortcodes ); |
| 47 | |
| 48 | return $shortcodes; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Verifies if a given shortcode slug is registered for handling. |
| 53 | * |
| 54 | * @since 4.12.0 |
| 55 | * |
| 56 | * @param string $slug Which slug we are checking if is registered. |
| 57 | * |
| 58 | * @return bool Whether a shortcode is registered or not. |
| 59 | */ |
| 60 | public function is_shortcode_registered( $slug ) { |
| 61 | $registered_shortcodes = $this->get_registered_shortcodes(); |
| 62 | return isset( $registered_shortcodes[ $slug ] ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Verifies if a given shortcode class name is registered for handling. |
| 67 | * |
| 68 | * @since 4.12.0 |
| 69 | * |
| 70 | * @param string $class_name Which class name we are checking if is registered. |
| 71 | * |
| 72 | * @return bool Whether a shortcode is registered, by class. |
| 73 | */ |
| 74 | public function is_shortcode_registered_by_class( $class_name ) { |
| 75 | $registered_shortcodes = $this->get_registered_shortcodes(); |
| 76 | return in_array( $class_name, $registered_shortcodes ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Add new shortcodes handler to catch the correct strings. |
| 81 | * |
| 82 | * @since 4.12.0 |
| 83 | */ |
| 84 | public function add_shortcodes() { |
| 85 | $registered_shortcodes = $this->get_registered_shortcodes(); |
| 86 | |
| 87 | // Add to WordPress all of the registered Shortcodes |
| 88 | foreach ( $registered_shortcodes as $shortcode => $class_name ) { |
| 89 | add_shortcode( $shortcode, [ $this, 'render_shortcode' ] ); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Makes sure we are correctly handling the Shortcodes we manage. |
| 95 | * |
| 96 | * @since 4.12.0 |
| 97 | * |
| 98 | * @param array $arguments Set of arguments passed to the Shortcode at hand. |
| 99 | * @param string $content Contents passed to the shortcode, inside of the open and close brackets. |
| 100 | * @param string $shortcode Which shortcode tag are we handling here. |
| 101 | * |
| 102 | * @return string The rendered shortcode HTML. |
| 103 | */ |
| 104 | public function render_shortcode( $arguments, $content, $shortcode ) { |
| 105 | $registered_shortcodes = $this->get_registered_shortcodes(); |
| 106 | |
| 107 | // Bail when we try to handle an unregistered shortcode (shouldn't happen) |
| 108 | if ( ! $this->is_shortcode_registered( $shortcode ) ) { |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | /** @var Shortcode_Interface $instance */ |
| 113 | $instance = new $registered_shortcodes[ $shortcode ]; |
| 114 | $instance->setup( $arguments, $content ); |
| 115 | |
| 116 | return $instance->get_html(); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Filter `pre_do_shortcode_tag` to add the current shortcode. |
| 121 | * |
| 122 | * @since 4.12.9 |
| 123 | * |
| 124 | * @param bool|string $return Short-circuit return value. Either false or the value to replace the shortcode with. |
| 125 | * @param string $tag Shortcode name. |
| 126 | * @param array $attr Shortcode attributes array, |
| 127 | * @param array $m Regular expression match array. |
| 128 | * |
| 129 | * @return bool|string Short-circuit return value. |
| 130 | */ |
| 131 | public function filter_pre_do_shortcode_tag( $return, $tag, $attr, $m ) { |
| 132 | if ( ! $this->is_shortcode_registered( $tag ) ) { |
| 133 | return $return; |
| 134 | } |
| 135 | |
| 136 | // Add to the doing shortcode. |
| 137 | $this->current_shortcode[] = $tag; |
| 138 | |
| 139 | return $return; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Filter `do_shortcode_tag` to remove the shortcode from the `$tribe_current_shortcode` list. |
| 144 | * |
| 145 | * @since 4.12.9 |
| 146 | * |
| 147 | * @param string $output Shortcode output. |
| 148 | * @param string $tag Shortcode name. |
| 149 | * @param array|string $attr Shortcode attributes array or empty string. |
| 150 | * @param array $m Regular expression match array. |
| 151 | * |
| 152 | * @return string Shortcode output. |
| 153 | */ |
| 154 | public function filter_do_shortcode_tag( $output, $tag, $attr, $m ) { |
| 155 | if ( ! $this->is_shortcode_registered( $tag ) ) { |
| 156 | return $output; |
| 157 | } |
| 158 | |
| 159 | if ( isset( $this->current_shortcode[ $tag ] ) ) { |
| 160 | unset( $this->current_shortcode[ $tag ] ); |
| 161 | } |
| 162 | |
| 163 | return $output; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Check if a shortcode is being done. |
| 168 | * |
| 169 | * @since 4.12.9 |
| 170 | * |
| 171 | * @param null|string $tag The shortcode tag name, or null to check if doing any shortcode. |
| 172 | * |
| 173 | * @return bool If the shortcode is being done or not. |
| 174 | */ |
| 175 | public function is_doing_shortcode( $tag = null ) { |
| 176 | if ( null === $tag ) { |
| 177 | return ! empty( $this->current_shortcode ); |
| 178 | } |
| 179 | |
| 180 | return in_array( $tag, $this->current_shortcode, true ); |
| 181 | } |
| 182 | } |
| 183 |