PluginProbe ʕ •ᴥ•ʔ
AlphaListing / 4.3.4
AlphaListing v4.3.4
trunk 4.3.4 4.3.5 4.3.6 4.3.7 4.4.0
alphalisting / src / Shortcode / Extension.php
alphalisting / src / Shortcode Last commit date
QueryParts 1 year ago Extension.php 1 year ago PostsQuery.php 1 year ago Query.php 1 year ago TermsQuery.php 1 year ago
Extension.php
186 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 array_filter(
107 $this->filters[ $type ],
108 function( $item ) use ( $hook ) {
109 return $item === $hook;
110 }
111 );
112 }
113
114 /**
115 * Unhook all our filters and actions.
116 *
117 * @since 1.0.0
118 */
119 final public function cleanup() {
120 foreach ( array_keys( $this->hooks ) as $type ) {
121 while ( $hook = array_shift( $this->hooks[ $type ] ) ) { // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
122 call_user_func_array( "remove_$type", $hook );
123 }
124 }
125 }
126
127 /**
128 * Handle the shortcode.
129 *
130 * @since 4.0.0
131 * @return void
132 */
133 public function handler() {}
134
135 /**
136 * Add the attribute.
137 *
138 * @since 4.0.0
139 * @param array $attributes The attributes.
140 * @return array The attributes.
141 */
142 public function add_attribute( $attributes ) {
143 $attributes[ $this->attribute_name ] = $this->default_value;
144 return $attributes;
145 }
146
147 /**
148 * Sanitize the shortcode attribute.
149 *
150 * @param mixed $value The value of the shortcode attribute.
151 * @param array $attributes The complete set of shortcode attributes.
152 * @return mixed The sanitized value.
153 */
154 public function sanitize_attribute( $value, array $attributes ) {
155 return $value;
156 }
157
158 /**
159 * Update the query with this extension's additional configuration.
160 *
161 * @param mixed $query The query.
162 * @param string $display The display/query type.
163 * @param string $key The name of the attribute.
164 * @param mixed $value The shortcode attribute value.
165 * @param array $attributes The complete set of shortcode attributes.
166 * @return mixed The updated query.
167 */
168 public function shortcode_query( $query, string $display, string $key, $value, array $attributes ) {
169 return $query;
170 }
171
172 /**
173 * Update the query with this extension's additional configuration.
174 *
175 * @param mixed $query The query.
176 * @param string $display The display/query type.
177 * @param string $key The name of the attribute.
178 * @param mixed $value The shortcode attribute value.
179 * @param array $attributes The complete set of shortcode attributes.
180 * @return mixed The updated query.
181 */
182 public function shortcode_query_for_display_and_attribute( $query, string $display, string $key, $value, array $attributes ) {
183 return $query;
184 }
185 }
186