class-advanced-variables.php
2 years ago
class-author-variables.php
3 years ago
class-base.php
1 year ago
class-basic-variables.php
2 years ago
class-cache.php
6 years ago
class-manager.php
1 year ago
class-post-variables.php
1 year ago
class-replacer.php
1 year ago
class-term-variables.php
2 years ago
class-variable.php
2 years ago
class-variable.php
219 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Variable model. |
| 4 | * |
| 5 | * Replace '%variables%' in strings based on context. |
| 6 | * |
| 7 | * @since 1.0.33 |
| 8 | * @package RankMath |
| 9 | * @subpackage RankMath\Replace_Variables |
| 10 | * @author Rank Math <support@rankmath.com> |
| 11 | */ |
| 12 | |
| 13 | namespace RankMath\Replace_Variables; |
| 14 | |
| 15 | defined( 'ABSPATH' ) || exit; |
| 16 | |
| 17 | /** |
| 18 | * Variable class. |
| 19 | */ |
| 20 | class Variable { |
| 21 | |
| 22 | /** |
| 23 | * Required properties. |
| 24 | * |
| 25 | * @var array |
| 26 | */ |
| 27 | private static $required = [ 'name', 'description', 'variable' ]; |
| 28 | |
| 29 | /** |
| 30 | * The unique id. |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | protected $id; |
| 35 | |
| 36 | /** |
| 37 | * The name of the variabe. |
| 38 | * |
| 39 | * @var string |
| 40 | */ |
| 41 | protected $name; |
| 42 | |
| 43 | /** |
| 44 | * The description of the variable. |
| 45 | * |
| 46 | * @var string |
| 47 | */ |
| 48 | protected $description; |
| 49 | |
| 50 | /** |
| 51 | * The variable to use. |
| 52 | * |
| 53 | * @var string |
| 54 | */ |
| 55 | protected $variable; |
| 56 | |
| 57 | /** |
| 58 | * The example for the variable. |
| 59 | * |
| 60 | * @var string |
| 61 | */ |
| 62 | protected $example; |
| 63 | |
| 64 | /** |
| 65 | * The callback to get the replacement value. |
| 66 | * |
| 67 | * @var mixed |
| 68 | */ |
| 69 | protected $callback; |
| 70 | |
| 71 | /** |
| 72 | * The variable is cacheable or not. |
| 73 | * |
| 74 | * @var bool |
| 75 | */ |
| 76 | protected $cacheable = true; |
| 77 | |
| 78 | /** |
| 79 | * Create variable from array. |
| 80 | * |
| 81 | * @throws \InvalidArgumentException If `$id` is empty. |
| 82 | * |
| 83 | * @param string $id Unique id of variable. |
| 84 | * @param array $args Array of values. |
| 85 | * |
| 86 | * @return Variable |
| 87 | */ |
| 88 | public static function from( $id, $args ) { |
| 89 | if ( empty( $id ) ) { |
| 90 | throw new \InvalidArgumentException( esc_html__( 'The $id variable is required.', 'rank-math' ) ); |
| 91 | } |
| 92 | |
| 93 | $variable = new Variable(); |
| 94 | $variable->id = $id; |
| 95 | $variable->example = isset( $args['example'] ) ? $args['example'] : esc_html__( 'Example', 'rank-math' ); |
| 96 | |
| 97 | foreach ( self::$required as $key ) { |
| 98 | if ( ! isset( $args[ $key ] ) ) { |
| 99 | /* translators: variable name */ |
| 100 | throw new \InvalidArgumentException( sprintf( esc_html__( 'The $%1$s is required for variable %2$s.', 'rank-math' ), esc_html( $key ), esc_html( $id ) ) ); |
| 101 | } |
| 102 | |
| 103 | $variable->$key = $args[ $key ]; |
| 104 | } |
| 105 | |
| 106 | if ( isset( $args['nocache'] ) && $args['nocache'] ) { |
| 107 | $variable->cacheable = false; |
| 108 | } |
| 109 | |
| 110 | return $variable; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Returns the id. |
| 115 | * |
| 116 | * @return string |
| 117 | */ |
| 118 | public function get_id() { |
| 119 | return $this->id; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Returns the name. |
| 124 | * |
| 125 | * @return string |
| 126 | */ |
| 127 | public function get_name() { |
| 128 | return $this->name; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Returns the description. |
| 133 | * |
| 134 | * @return string |
| 135 | */ |
| 136 | public function get_description() { |
| 137 | return $this->description; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Returns the variable. |
| 142 | * |
| 143 | * @return string |
| 144 | */ |
| 145 | public function get_variable() { |
| 146 | return $this->variable; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Returns the example. |
| 151 | * |
| 152 | * @return string |
| 153 | */ |
| 154 | public function get_example() { |
| 155 | return $this->example; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Set example. |
| 160 | * |
| 161 | * @param string $example New example. |
| 162 | */ |
| 163 | public function set_example( $example ) { |
| 164 | $this->example = $example; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Set callback. |
| 169 | * |
| 170 | * @param mixed $callback New callback. |
| 171 | */ |
| 172 | public function set_callback( $callback ) { |
| 173 | $this->callback = $callback; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Run callback. |
| 178 | * |
| 179 | * @param array $var_args Array of arguments passed with variable. |
| 180 | * @param array $args The object some of the replacement values might come from, |
| 181 | * could be a post, taxonomy or term. |
| 182 | * |
| 183 | * @return mixed |
| 184 | */ |
| 185 | public function run_callback( $var_args, $args = [] ) { |
| 186 | rank_math()->variables->set_arguments( $args ); |
| 187 | |
| 188 | $value = ! empty( $this->callback ) ? call_user_func( $this->callback, $var_args, $args ) : |
| 189 | apply_filters( 'rank_math/vars/' . $this->get_id(), $var_args, $this ); |
| 190 | |
| 191 | rank_math()->variables->reset_arguments(); |
| 192 | |
| 193 | return $value; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Convert object to array. |
| 198 | * |
| 199 | * @return array Object as array. |
| 200 | */ |
| 201 | public function to_array() { |
| 202 | $arr = []; |
| 203 | foreach ( [ 'name', 'description', 'variable', 'example' ] as $key ) { |
| 204 | $arr[ $key ] = $this->$key; |
| 205 | } |
| 206 | |
| 207 | return $arr; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Returns the variable is cacheable or not. |
| 212 | * |
| 213 | * @return bool |
| 214 | */ |
| 215 | public function is_cacheable() { |
| 216 | return $this->cacheable; |
| 217 | } |
| 218 | } |
| 219 |