QueryParts
9 months ago
Extension.php
9 months ago
PostsQuery.php
9 months ago
Query.php
9 months ago
TermsQuery.php
9 months ago
Extension.php
190 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Shortcode Extension class |
| 4 | * |
| 5 | * @package alphalisting |
| 6 | */ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | namespace eslin87\AlphaListing\Shortcode; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; |
| 14 | } |
| 15 | |
| 16 | use \eslin87\AlphaListing\Singleton; |
| 17 | |
| 18 | /** |
| 19 | * Shortcode Extension |
| 20 | */ |
| 21 | abstract class Extension extends Singleton implements \eslin87\AlphaListing\Extension { |
| 22 | /** |
| 23 | * The attribute for this shortcode extension. |
| 24 | * |
| 25 | * @since 4.0.0 |
| 26 | * @var string |
| 27 | */ |
| 28 | public $attribute_name = ''; |
| 29 | |
| 30 | /** |
| 31 | * The default value for the attribute. |
| 32 | * |
| 33 | * @since 4.0.0 |
| 34 | * @var string |
| 35 | */ |
| 36 | public $default_value = ''; |
| 37 | |
| 38 | /** |
| 39 | * The types of listing this shortcode extension may be used with. |
| 40 | * |
| 41 | * @since 4.0.0 |
| 42 | * @var array |
| 43 | */ |
| 44 | public $display_types = array(); |
| 45 | |
| 46 | /** |
| 47 | * Our hooks |
| 48 | * |
| 49 | * @since 4.0.0 |
| 50 | * @var array |
| 51 | */ |
| 52 | protected $hooks = array( |
| 53 | 'action' => array(), |
| 54 | 'filter' => array(), |
| 55 | ); |
| 56 | |
| 57 | /** |
| 58 | * Initialize the extension. |
| 59 | * |
| 60 | * @since 4.0.0 |
| 61 | * @return void |
| 62 | */ |
| 63 | final public function initialize() { |
| 64 | add_action( 'alphalisting_shortcode_start', array( $this, 'handler' ), 10, 1 ); |
| 65 | add_action( 'alphalisting_shortcode_end', array( $this, 'cleanup' ), 10, 1 ); |
| 66 | |
| 67 | if ( ! empty( $this->attribute_name ) ) { |
| 68 | add_filter( 'alphalisting_get_shortcode_attributes', array( $this, 'add_attribute' ) ); |
| 69 | add_filter( "alphalisting_sanitize_shortcode_attribute__{$this->attribute_name}", array( $this, 'sanitize_attribute' ), 10, 2 ); |
| 70 | add_filter( "alphalisting_shortcode_query_for_attribute__{$this->attribute_name}", array( $this, 'shortcode_query' ), 10, 5 ); |
| 71 | foreach ( $this->display_types as $display ) { |
| 72 | add_filter( "alphalisting_shortcode_query_for_display__{$display}__and_attribute__{$this->attribute_name}", array( $this, 'shortcode_query_for_display_and_attribute' ), 10, 5 ); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Add a hook. |
| 79 | * |
| 80 | * @since 1.0.0 |
| 81 | * @param string $type The hook type (filter or action). |
| 82 | * @param string $name The hook name. |
| 83 | * @param callable $function The function to call. |
| 84 | * @param int $order The order to call this function. |
| 85 | * @param int $arguments The number of arguments the function expects. |
| 86 | */ |
| 87 | final protected function add_hook( string $type, string $name, callable $function, int $order = 10, int $arguments = 1 ) { |
| 88 | $hook = array( $name, $function, $order, $arguments ); |
| 89 | call_user_func_array( "add_$type", $hook ); |
| 90 | $this->hooks[ $type ][] = $hook; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Remove a hook. |
| 95 | * |
| 96 | * @since 1.0.0 |
| 97 | * @param string $type The hook type (filter or action). |
| 98 | * @param string $name The hook name. |
| 99 | * @param callable $function The function to call. |
| 100 | * @param int $order The order to call this function. |
| 101 | * @param int $arguments The number of arguments the function expects. |
| 102 | */ |
| 103 | final protected function remove_hook( string $type, string $name, callable $function, int $order = 10, int $arguments = 1 ) { |
| 104 | $hook = array( $name, $function, $order, $arguments ); |
| 105 | call_user_func_array( "remove_$type", $hook ); |
| 106 | if ( isset( $this->hooks[ $type ] ) ) { |
| 107 | $this->hooks[ $type ] = array_values( |
| 108 | array_filter( |
| 109 | $this->hooks[ $type ], |
| 110 | function( $item ) use ( $hook ) { |
| 111 | return $item !== $hook; |
| 112 | } |
| 113 | ) |
| 114 | ); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Unhook all our filters and actions. |
| 120 | * |
| 121 | * @since 1.0.0 |
| 122 | */ |
| 123 | final public function cleanup() { |
| 124 | foreach ( array_keys( $this->hooks ) as $type ) { |
| 125 | while ( $hook = array_shift( $this->hooks[ $type ] ) ) { // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition |
| 126 | call_user_func_array( "remove_$type", $hook ); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Handle the shortcode. |
| 133 | * |
| 134 | * @since 4.0.0 |
| 135 | * @return void |
| 136 | */ |
| 137 | public function handler() {} |
| 138 | |
| 139 | /** |
| 140 | * Add the attribute. |
| 141 | * |
| 142 | * @since 4.0.0 |
| 143 | * @param array $attributes The attributes. |
| 144 | * @return array The attributes. |
| 145 | */ |
| 146 | public function add_attribute( $attributes ) { |
| 147 | $attributes[ $this->attribute_name ] = $this->default_value; |
| 148 | return $attributes; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Sanitize the shortcode attribute. |
| 153 | * |
| 154 | * @param mixed $value The value of the shortcode attribute. |
| 155 | * @param array $attributes The complete set of shortcode attributes. |
| 156 | * @return mixed The sanitized value. |
| 157 | */ |
| 158 | public function sanitize_attribute( $value, array $attributes ) { |
| 159 | return $value; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Update the query with this extension's additional configuration. |
| 164 | * |
| 165 | * @param mixed $query The query. |
| 166 | * @param string $display The display/query type. |
| 167 | * @param string $key The name of the attribute. |
| 168 | * @param mixed $value The shortcode attribute value. |
| 169 | * @param array $attributes The complete set of shortcode attributes. |
| 170 | * @return mixed The updated query. |
| 171 | */ |
| 172 | public function shortcode_query( $query, string $display, string $key, $value, array $attributes ) { |
| 173 | return $query; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Update the query with this extension's additional configuration. |
| 178 | * |
| 179 | * @param mixed $query The query. |
| 180 | * @param string $display The display/query type. |
| 181 | * @param string $key The name of the attribute. |
| 182 | * @param mixed $value The shortcode attribute value. |
| 183 | * @param array $attributes The complete set of shortcode attributes. |
| 184 | * @return mixed The updated query. |
| 185 | */ |
| 186 | public function shortcode_query_for_display_and_attribute( $query, string $display, string $key, $value, array $attributes ) { |
| 187 | return $query; |
| 188 | } |
| 189 | } |
| 190 |