class-wprun-base.php
367 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class WPRun_Base_1x0x0 |
| 4 | * |
| 5 | * Base abstract class can be extended for easy WP Plugin and Theme development. |
| 6 | * All subclasses are singletons and can be instantiated with the static |
| 7 | * "create()" factory method. |
| 8 | * |
| 9 | * @package WPRun |
| 10 | * @category WordPress Library |
| 11 | * @version 1.0.0 |
| 12 | * @author WebFactory Ltd |
| 13 | * @link https://www.webfactoryltd.com/ |
| 14 | */ |
| 15 | abstract class WPRun_Base_1x0x0 |
| 16 | { |
| 17 | |
| 18 | const RETURN_VOID = '__VOID__'; |
| 19 | |
| 20 | /** |
| 21 | * Page hook |
| 22 | * Page hook can be set by subclasses, in that case filter and action methods |
| 23 | * will only be set if page hook is the current screen id |
| 24 | * @var string |
| 25 | */ |
| 26 | protected $page_hook = null; |
| 27 | |
| 28 | /** |
| 29 | * Automatically set action and filter methods |
| 30 | * @var boolean |
| 31 | */ |
| 32 | protected $autoset_hook_methods = true; |
| 33 | |
| 34 | /** |
| 35 | * @var string |
| 36 | */ |
| 37 | protected $action_prefix = 'action_'; |
| 38 | |
| 39 | /** |
| 40 | * @var string |
| 41 | */ |
| 42 | protected $filter_prefix = 'filter_'; |
| 43 | |
| 44 | /** |
| 45 | * Only for internal use (to recognize a callback call) |
| 46 | * @var string |
| 47 | */ |
| 48 | private $internal_callback_prefix = '_cb_'; |
| 49 | |
| 50 | /** |
| 51 | * List of (singleton) instances |
| 52 | * Only for internal use |
| 53 | * @var array |
| 54 | */ |
| 55 | private static $instances = array(); |
| 56 | |
| 57 | /** |
| 58 | * @var array |
| 59 | */ |
| 60 | private $arguments = array(); |
| 61 | |
| 62 | /** |
| 63 | * Factory method |
| 64 | * @param mixed $param1 Optional, will be passed on to the constructor and init() method |
| 65 | * @param mixed $paramN Optional, will be passed on to the constructor and init() method |
| 66 | * @return WPRun_Base_1x0x0 |
| 67 | * @triggers E_USER_NOTICE Class already created |
| 68 | */ |
| 69 | final public static function create() |
| 70 | { |
| 71 | $class_name = get_called_class(); |
| 72 | $arguments = func_get_args(); |
| 73 | |
| 74 | // check if instance of this class already exists |
| 75 | if ( key_exists( $class_name, self::$instances ) ) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | // pass all arguments to constructor |
| 80 | $instance = new $class_name( $arguments ); |
| 81 | |
| 82 | return $instance; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Constructor |
| 87 | * @triggers E_USER_NOTICE |
| 88 | */ |
| 89 | private function __construct( array $arguments ) |
| 90 | { |
| 91 | $class_name = get_called_class(); |
| 92 | self::$instances[ $class_name ] = $this; |
| 93 | |
| 94 | $this->arguments = $arguments; |
| 95 | |
| 96 | // call init method |
| 97 | $method_name = 'init'; |
| 98 | |
| 99 | if ( method_exists( $this, $method_name ) ) { |
| 100 | $method_reflection = new ReflectionMethod( get_called_class(), $method_name ); |
| 101 | |
| 102 | if ( $method_reflection->isProtected() ) { |
| 103 | call_user_func_array( array( $this, $method_name ), $this->arguments ); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // automatically set methods as callback for WP hooks |
| 108 | if ( true === $this->autoset_hook_methods ) { |
| 109 | $this->set_hook_methods(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @return WPRun_Base_1x0x0 |
| 115 | * @triggers E_USER_NOTICE Instance not yet created |
| 116 | */ |
| 117 | final public static function get_instance() |
| 118 | { |
| 119 | $class_name = get_called_class(); |
| 120 | |
| 121 | if ( ! isset( self::$instances[ $class_name ] ) ) { |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | return self::$instances[ $class_name ]; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Get argument passed on to the constructor |
| 130 | * @param integer $index Optional, when null return all arguments |
| 131 | * @return mixed|null |
| 132 | */ |
| 133 | final protected function get_argument( $index = null ) |
| 134 | { |
| 135 | // return all arguments when no index given |
| 136 | if ( null === $index ) { |
| 137 | return $this->arguments; |
| 138 | } |
| 139 | |
| 140 | if ( !isset( $this->arguments[ $index ] ) ) { |
| 141 | return null; |
| 142 | } |
| 143 | |
| 144 | return $this->arguments[ $index ]; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * @param string $template_file_path |
| 149 | * @param array $vars Optional |
| 150 | * @triggers E_USER_NOTICE Template file not readable |
| 151 | */ |
| 152 | final public static function show_template( $template_file_path, array $vars = array() ) |
| 153 | { |
| 154 | if ( is_readable( $template_file_path ) && 0 === strpos($template_file_path, WPEL_Plugin::get_plugin_dir())) { |
| 155 | // show file |
| 156 | include $template_file_path; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * @param string $template_file_path |
| 162 | * @param array $vars Optional |
| 163 | * @triggers E_USER_NOTICE Template file not readable |
| 164 | */ |
| 165 | final public static function render_template( $template_file_path, array $vars = array() ) |
| 166 | { |
| 167 | // start output buffer |
| 168 | ob_start(); |
| 169 | |
| 170 | // output template |
| 171 | self::show_template( $template_file_path, $vars ); |
| 172 | |
| 173 | // get the view content |
| 174 | $content = ob_get_contents(); |
| 175 | |
| 176 | // clean output buffer |
| 177 | ob_end_clean(); |
| 178 | |
| 179 | return $content; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Get a callable to a method in current instance, when called will be |
| 184 | * caught by __callStatic(), were the magic happens |
| 185 | * @param string $method_name |
| 186 | * @return callable |
| 187 | */ |
| 188 | final protected function get_callback( $method_name ) |
| 189 | { |
| 190 | return array( get_called_class(), $this->internal_callback_prefix . $method_name ); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * @param string $method_name |
| 195 | * @param array $arguments |
| 196 | * @return mixed|void |
| 197 | * @triggers E_USER_NOTICE Method name not exists/callable |
| 198 | */ |
| 199 | public function __call( $method_name, $arguments ) |
| 200 | { |
| 201 | $return_value = self::magic_call( $method_name, $arguments ); |
| 202 | |
| 203 | if ( self::RETURN_VOID === $return_value ) { |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | return $return_value; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * @param string $method_name |
| 212 | * @param array $arguments |
| 213 | * @return mixed|void |
| 214 | * @triggers E_USER_NOTICE Method name not exists/callable |
| 215 | */ |
| 216 | public static function __callStatic( $method_name, $arguments ) |
| 217 | { |
| 218 | $return_value = self::magic_call( $method_name, $arguments ); |
| 219 | |
| 220 | if ( self::RETURN_VOID === $return_value ) { |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | return $return_value; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * @param string $method_name |
| 229 | * @param array $arguments |
| 230 | * @return mixed|void |
| 231 | */ |
| 232 | final protected static function magic_call( $method_name, $arguments ) |
| 233 | { |
| 234 | $class_name = get_called_class(); |
| 235 | $instance = self::$instances[ $class_name ]; |
| 236 | |
| 237 | // catch callbacks set by get_callback() method |
| 238 | // this way callbacks can also be implemented as protected |
| 239 | $given_callback_name = self::fetch_name_containing_prefix( $instance->internal_callback_prefix, $method_name ); |
| 240 | |
| 241 | // normal callback |
| 242 | if ( null !== $given_callback_name ) { |
| 243 | $real_args = $arguments; |
| 244 | |
| 245 | $given_method_name = $given_callback_name; |
| 246 | |
| 247 | $callable = array( $instance, $given_method_name ); |
| 248 | |
| 249 | if ( is_callable( $callable ) ) { |
| 250 | return call_user_func_array( $callable, $real_args ); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | return self::RETURN_VOID; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Check and auto-initialize methods for hooks |
| 259 | */ |
| 260 | final protected function set_hook_methods() |
| 261 | { |
| 262 | $methods = get_class_methods( $this ); |
| 263 | |
| 264 | foreach ( $methods as $method_name ) { |
| 265 | $action_name = self::fetch_name_containing_prefix( $this->action_prefix, $method_name ); |
| 266 | if ( null !== $action_name ) { |
| 267 | $this->add_to_hook( 'action', $action_name, $method_name ); |
| 268 | continue; |
| 269 | } |
| 270 | |
| 271 | $filter_name = self::fetch_name_containing_prefix( $this->filter_prefix, $method_name ); |
| 272 | if ( null !== $filter_name ) { |
| 273 | $this->add_to_hook( 'filter', $filter_name, $method_name ); |
| 274 | continue; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * @param string $hook_type "action" or "filter" |
| 281 | * @param string $hook_name |
| 282 | * @param string $method_name |
| 283 | * @triggers E_USER_NOTICE |
| 284 | */ |
| 285 | private function add_to_hook( $hook_type, $hook_name, $method_name ) |
| 286 | { |
| 287 | // fetch priority outof method name |
| 288 | $split_method_Name = explode( '_', $method_name ); |
| 289 | $last = end( $split_method_Name ); |
| 290 | |
| 291 | if ( is_numeric( $last ) ) { |
| 292 | $priority = (int) $last; |
| 293 | $wp_hook_name = str_replace( '_' . $last, '', $hook_name ); |
| 294 | } else { |
| 295 | $priority = 10; |
| 296 | $wp_hook_name = $hook_name; |
| 297 | } |
| 298 | |
| 299 | // get the method's number of params |
| 300 | $method_reflection = new ReflectionMethod( get_called_class(), $method_name ); |
| 301 | $accepted_args = $method_reflection->getNumberOfParameters(); |
| 302 | |
| 303 | // set internal wp hook action or filter callback |
| 304 | $method_callback = $this->get_callback( $method_name ); |
| 305 | $check_call_hook = $this->get_callback( 'check_call_hook' ); |
| 306 | |
| 307 | $callback = function () use ( $method_callback, $check_call_hook ) { |
| 308 | $call_hook = call_user_func( $check_call_hook ); |
| 309 | |
| 310 | if ( false === $call_hook ) { |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | return call_user_func_array( $method_callback, func_get_args() ); |
| 315 | }; |
| 316 | |
| 317 | if ( 'action' === $hook_type ) { |
| 318 | add_action( $wp_hook_name, $callback, $priority, $accepted_args ); |
| 319 | } elseif ('filter' === $hook_type) { |
| 320 | add_filter( $wp_hook_name, $callback, $priority, $accepted_args ); |
| 321 | } else { |
| 322 | return false; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Check if an action or filter hook should be called (correct page hook) |
| 328 | * @return boolean |
| 329 | */ |
| 330 | final protected function check_call_hook() |
| 331 | { |
| 332 | if ( null === $this->page_hook ) { |
| 333 | return true; |
| 334 | } |
| 335 | |
| 336 | if ( is_network_admin() ) { |
| 337 | $page_hook = $this->page_hook .'-network'; |
| 338 | } else { |
| 339 | $page_hook = $this->page_hook; |
| 340 | } |
| 341 | |
| 342 | if ( get_current_screen()->id === $page_hook ) { |
| 343 | return true; |
| 344 | } |
| 345 | |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * @param string $prefix |
| 351 | * @param string $name |
| 352 | * @return string|null |
| 353 | */ |
| 354 | private static function fetch_name_containing_prefix( $prefix, $name ) |
| 355 | { |
| 356 | $prefix_length = strlen( $prefix ); |
| 357 | |
| 358 | if ( $prefix !== substr( $name, 0, $prefix_length) ) { |
| 359 | return null; |
| 360 | } |
| 361 | |
| 362 | $fetchedName = substr( $name, $prefix_length ); |
| 363 | return $fetchedName; |
| 364 | } |
| 365 | |
| 366 | } |
| 367 |