PluginProbe ʕ •ᴥ•ʔ
AlphaListing / 4.5.0
AlphaListing v4.5.0
4.5.0 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 2 weeks ago Extension.php 2 weeks ago PostsQuery.php 2 weeks ago Query.php 2 weeks ago TermsQuery.php 2 weeks ago
Extension.php
165 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 * Unhook all our filters and actions.
95 *
96 * @since 1.0.0
97 */
98 final public function cleanup() {
99 foreach ( array_keys( $this->hooks ) as $type ) {
100 while ( null !== ( $hook = array_shift( $this->hooks[ $type ] ) ) ) { // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
101 call_user_func_array( "remove_$type", $hook );
102 }
103 }
104 }
105
106 /**
107 * Handle the shortcode.
108 *
109 * @since 4.0.0
110 * @return void
111 */
112 public function handler() {}
113
114 /**
115 * Add the attribute.
116 *
117 * @since 4.0.0
118 * @param array $attributes The attributes.
119 * @return array The attributes.
120 */
121 public function add_attribute( $attributes ) {
122 $attributes[ $this->attribute_name ] = $this->default_value;
123 return $attributes;
124 }
125
126 /**
127 * Sanitize the shortcode attribute.
128 *
129 * @param mixed $value The value of the shortcode attribute.
130 * @param array $attributes The complete set of shortcode attributes.
131 * @return mixed The sanitized value.
132 */
133 public function sanitize_attribute( $value, array $attributes ) {
134 return $value;
135 }
136
137 /**
138 * Update the query with this extension's additional configuration.
139 *
140 * @param mixed $query The query.
141 * @param string $display The display/query type.
142 * @param string $key The name of the attribute.
143 * @param mixed $value The shortcode attribute value.
144 * @param array $attributes The complete set of shortcode attributes.
145 * @return mixed The updated query.
146 */
147 public function shortcode_query( $query, string $display, string $key, $value, array $attributes ) {
148 return $query;
149 }
150
151 /**
152 * Update the query with this extension's additional configuration.
153 *
154 * @param mixed $query The query.
155 * @param string $display The display/query type.
156 * @param string $key The name of the attribute.
157 * @param mixed $value The shortcode attribute value.
158 * @param array $attributes The complete set of shortcode attributes.
159 * @return mixed The updated query.
160 */
161 public function shortcode_query_for_display_and_attribute( $query, string $display, string $key, $value, array $attributes ) {
162 return $query;
163 }
164 }
165