code-manager
Last commit date
Code_Manager
6 days ago
admin
6 days ago
assets
6 days ago
includes
6 days ago
public
6 days ago
vendor
6 days ago
CM_Tools.php
6 days ago
LICENSE.txt
6 days ago
changelog.md
6 days ago
code-manager-config.php
6 days ago
code-manager.php
6 days ago
readme.txt
6 days ago
CM_Tools.php
452 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Code Manager tools |
| 4 | * |
| 5 | * @package . |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Class CM_Tools |
| 10 | * |
| 11 | * This class is available from the Code Manager editor. The purpose is to help developers to easily access objects |
| 12 | * from an action hook or filter. |
| 13 | * |
| 14 | * Usage: |
| 15 | * $cm = new CM_Tools( $self ); |
| 16 | * |
| 17 | * $constants = $cm->get_constants(); |
| 18 | * $constant_exists = $cm->has_constant('CONSTANT_NAME'); |
| 19 | * $constant_value = $cm->get_constant_value('CONSTANT_NAME'); |
| 20 | * |
| 21 | * $properties = $cm->get_properties(); |
| 22 | * $property_exists = $cm->has_property('property_name'); |
| 23 | * $property_value = $cm->get_property_value('property_name'); |
| 24 | * $cm->set_property_value('property_name', 'new_property_value'); |
| 25 | * |
| 26 | * $methods = $cm->get_methods(); |
| 27 | * $method_exists = $cm->has_method('method_name'); |
| 28 | * $cm->invoke_method('method_name', 'argument_value')); |
| 29 | * |
| 30 | * $menu_slug = CM_Tools::get_menu_slug(); |
| 31 | * $argument_value = CM_Tools::get_argument('table_name'); |
| 32 | * $full_path = CM_Tools::get_full_path(); |
| 33 | * $admin_page = CM_Tools::get_page(); |
| 34 | * |
| 35 | * CM_Tools::log($obj); |
| 36 | * |
| 37 | * @author Peter Schulz |
| 38 | * @since 1.0.0 |
| 39 | */ |
| 40 | class CM_Tools { |
| 41 | |
| 42 | /** |
| 43 | * CM tool object |
| 44 | * |
| 45 | * @var null |
| 46 | */ |
| 47 | private $obj = null; |
| 48 | |
| 49 | /** |
| 50 | * Reflection object |
| 51 | * |
| 52 | * @var ReflectionClass|null |
| 53 | */ |
| 54 | private $reflection = null; |
| 55 | |
| 56 | /** |
| 57 | * Constructor |
| 58 | * |
| 59 | * @param array $obj Arguments. |
| 60 | */ |
| 61 | public function __construct( $obj ) { |
| 62 | try { |
| 63 | $this->obj = $obj; |
| 64 | $this->reflection = new ReflectionClass( $obj ); |
| 65 | } catch ( ReflectionException $exception ) { |
| 66 | var_dump( 'CODE MANAGER ERROR: Class not found' ); |
| 67 | } catch ( Exception $exception ) { |
| 68 | var_dump( 'CODE MANAGER ERROR: Could not instantiate CM_Tools' ); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns string representation of reflection object |
| 74 | * |
| 75 | * @return string |
| 76 | */ |
| 77 | public function toString() { |
| 78 | return $this->reflection->__toString(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get constants of reflection object |
| 83 | * |
| 84 | * @return array|null |
| 85 | */ |
| 86 | public function get_constants() { |
| 87 | return null !== $this->reflection ? $this->reflection->getConstants() : null; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Does reflection object have constants? |
| 92 | * |
| 93 | * @param string $constant_name Constant name. |
| 94 | * @return bool|null |
| 95 | */ |
| 96 | public function has_constant( $constant_name ) { |
| 97 | return null !== $this->reflection ? $this->reflection->hasConstant( $constant_name ) : null; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Get constant value of reflection object |
| 102 | * |
| 103 | * @param string $constant_name Constant name. |
| 104 | * @return false|mixed|void|null |
| 105 | */ |
| 106 | public function get_constant_value( $constant_name ) { |
| 107 | if ( null === $this->reflection ) { |
| 108 | return null; |
| 109 | } |
| 110 | |
| 111 | if ( null !== $this->reflection ) { |
| 112 | if ( $this->has_constant( $constant_name ) ) { |
| 113 | return $this->reflection->getConstant( $constant_name ); |
| 114 | } else { |
| 115 | var_dump( "CODE MANAGER ERROR: Constant '{$constant_name}' not found" ); |
| 116 | return null; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Get reflection object properties |
| 123 | * |
| 124 | * @return array|null |
| 125 | */ |
| 126 | public function get_properties() { |
| 127 | if ( null === $this->reflection ) { |
| 128 | return null; |
| 129 | } |
| 130 | |
| 131 | $properties = |
| 132 | $this->reflection->getProperties( |
| 133 | ReflectionProperty::IS_PUBLIC | |
| 134 | ReflectionProperty::IS_PROTECTED | |
| 135 | ReflectionProperty::IS_PRIVATE |
| 136 | ); |
| 137 | |
| 138 | $all_properties = array(); |
| 139 | foreach ( $properties as $property ) { |
| 140 | if ( $property->isPrivate() ) { |
| 141 | $access = 'private'; |
| 142 | } elseif ( $property->isProtected() ) { |
| 143 | $access = 'protected'; |
| 144 | } elseif ( $property->isPublic() ) { |
| 145 | $access = 'public'; |
| 146 | } |
| 147 | |
| 148 | $all_properties[ $property->getName() ] = |
| 149 | array( |
| 150 | 'static' => $property->isStatic(), |
| 151 | 'access' => $access, |
| 152 | 'value' => $this->get_property_value( $property->getName() ), |
| 153 | ); |
| 154 | } |
| 155 | |
| 156 | return $all_properties; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Check if reflection object has property |
| 161 | * |
| 162 | * @param string $property_name Property name. |
| 163 | * @return bool|null |
| 164 | */ |
| 165 | public function has_property( $property_name ) { |
| 166 | return null !== $this->reflection ? $this->reflection->hasProperty( $property_name ) : null; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Get property value of reflection object |
| 171 | * |
| 172 | * @param string $property_name Property name. |
| 173 | * @return mixed|void|null |
| 174 | */ |
| 175 | public function get_property_value( $property_name ) { |
| 176 | if ( null === $this->reflection ) { |
| 177 | return null; |
| 178 | } |
| 179 | |
| 180 | if ( $this->has_property( $property_name ) ) { |
| 181 | try { |
| 182 | $property_name = $this->reflection->getProperty( $property_name ); |
| 183 | $property_name->setAccessible( true ); |
| 184 | |
| 185 | return $property_name->getValue( $this->obj ); |
| 186 | } catch ( Throwable $exception ) { |
| 187 | var_dump( 'CODE MANAGER ERROR: ' . $exception->getMessage() ); |
| 188 | } |
| 189 | } else { |
| 190 | var_dump( "CODE MANAGER ERROR: Property '{$property_name}' not found" ); |
| 191 | return null; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Set value of reflection property |
| 197 | * |
| 198 | * @param string $property_name Property name. |
| 199 | * @param string $value Property value. |
| 200 | * @return void|null |
| 201 | */ |
| 202 | public function set_property_value( $property_name, $value ) { |
| 203 | if ( null === $this->reflection ) { |
| 204 | return null; |
| 205 | } |
| 206 | |
| 207 | if ( $this->has_property( $property_name ) ) { |
| 208 | try { |
| 209 | $property = $this->reflection->getProperty( $property_name ); |
| 210 | $property->setAccessible( true ); |
| 211 | |
| 212 | return $property->setValue( $this->obj, $value ); |
| 213 | } catch ( Throwable $exception ) { |
| 214 | var_dump( 'CODE MANAGER ERROR: ' . $exception->getMessage() ); |
| 215 | } |
| 216 | } else { |
| 217 | var_dump( "CODE MANAGER ERROR: Property '{$property_name}' not found" ); |
| 218 | return null; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Get reflection object methods |
| 224 | * |
| 225 | * @return array|null |
| 226 | */ |
| 227 | public function get_methods() { |
| 228 | if ( null === $this->reflection ) { |
| 229 | return null; |
| 230 | } |
| 231 | |
| 232 | $methods = |
| 233 | $this->reflection->getMethods( |
| 234 | ReflectionProperty::IS_PUBLIC | |
| 235 | ReflectionProperty::IS_PROTECTED | |
| 236 | ReflectionProperty::IS_PRIVATE |
| 237 | ); |
| 238 | |
| 239 | $all_methods = array(); |
| 240 | foreach ( $methods as $method ) { |
| 241 | if ( $method->isPrivate() ) { |
| 242 | $access = 'private'; |
| 243 | } elseif ( $method->isProtected() ) { |
| 244 | $access = 'protected'; |
| 245 | } elseif ( $method->isPublic() ) { |
| 246 | $access = 'public'; |
| 247 | } |
| 248 | |
| 249 | $all_methods[ $method->getName() ] = |
| 250 | array( |
| 251 | 'static' => $method->isStatic(), |
| 252 | 'access' => $access, |
| 253 | ); |
| 254 | } |
| 255 | |
| 256 | return $all_methods; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Does reflection object have methods? |
| 261 | * |
| 262 | * @param string $method_name Method name. |
| 263 | * @return bool|null |
| 264 | */ |
| 265 | public function has_method( $method_name ) { |
| 266 | return null !== $this->reflection ? $this->reflection->hasMethod( $method_name ) : null; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Execute method of reflection object |
| 271 | * |
| 272 | * @param string $method_name Method name. |
| 273 | * @param string $args Method arguments. |
| 274 | * @return mixed|void |
| 275 | */ |
| 276 | public function invoke_method( $method_name, $args = array() ) { |
| 277 | if ( null !== $this->reflection && $this->has_method( $method_name ) ) { |
| 278 | try { |
| 279 | $method = new ReflectionMethod( $this->obj, $method_name ); |
| 280 | $method->setAccessible( true ); |
| 281 | |
| 282 | return $method->invoke( $this->obj, $args ); |
| 283 | } catch ( Throwable $exception ) { |
| 284 | var_dump( 'CODE MANAGER ERROR: ' . $exception->getMessage() ); |
| 285 | } |
| 286 | } else { |
| 287 | var_dump( "CODE MANAGER ERROR: Method '{$method_name}' not found" ); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Get menu slug |
| 293 | * |
| 294 | * @return mixed|null |
| 295 | */ |
| 296 | public static function get_menu_slug() { |
| 297 | if ( is_admin() ) { |
| 298 | return isset( $_REQUEST['page'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ) : null; |
| 299 | } else { |
| 300 | return null; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Get URL argument |
| 306 | * |
| 307 | * @param string $argument_name Argument name. |
| 308 | * @return null |
| 309 | */ |
| 310 | public static function get_argument( $argument_name ) { |
| 311 | return isset( $_REQUEST[ $argument_name ] ) ? sanitize_text_field( wp_unslash( $_REQUEST[ $argument_name ] ) ) : null; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Get full path |
| 316 | * |
| 317 | * @return mixed |
| 318 | */ |
| 319 | public static function get_full_path() { |
| 320 | return isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : null; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Get current page |
| 325 | * |
| 326 | * @return null |
| 327 | */ |
| 328 | public static function get_page() { |
| 329 | if ( is_admin() ) { |
| 330 | global $pagenow; |
| 331 | return $pagenow; |
| 332 | } else { |
| 333 | return null; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Write message to temporary file (saved in uploads) |
| 339 | * |
| 340 | * @param object $obj Object to be written to log file. |
| 341 | * @return void |
| 342 | */ |
| 343 | public static function log( $obj ) { |
| 344 | try { |
| 345 | $upload_dir = self::get_plugin_upload_dir(); |
| 346 | $file_name = 'cm_tools.log'; |
| 347 | if ( ! is_dir( $upload_dir ) ) { |
| 348 | mkdir( $upload_dir, 0755, true ); |
| 349 | } |
| 350 | $file = fopen( "{$upload_dir}{$file_name}", 'a' ); |
| 351 | fwrite( $file, date( 'd-m-Y h:i' ) . ' ' ); |
| 352 | switch ( gettype( $obj ) ) { |
| 353 | case 'array': |
| 354 | case 'object': |
| 355 | fwrite( $file, json_encode( $obj ) ); |
| 356 | break; |
| 357 | default: |
| 358 | fwrite( $file, $obj ); |
| 359 | } |
| 360 | fwrite( $file, "\n" ); |
| 361 | fclose( $file ); |
| 362 | } catch ( \Exception $exception ) { |
| 363 | echo 'ERROR: ' . $exception->getMessage(); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Get plugin upload folder |
| 369 | * |
| 370 | * @return string |
| 371 | */ |
| 372 | public static function get_plugin_upload_dir() { |
| 373 | $uploads = wp_upload_dir(); |
| 374 | return $uploads['basedir'] . DIRECTORY_SEPARATOR . 'code-manager' . DIRECTORY_SEPARATOR; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Get user login |
| 379 | * |
| 380 | * @return string |
| 381 | */ |
| 382 | public static function get_current_user_login() { |
| 383 | global $current_user; |
| 384 | if ( isset( $current_user->user_login ) ) { |
| 385 | return $current_user->user_login; |
| 386 | } else { |
| 387 | $wp_user = wp_get_current_user(); |
| 388 | if ( isset( $wp_user->data->user_login ) ) { |
| 389 | return $wp_user->data->user_login; |
| 390 | } else { |
| 391 | return 'anonymous'; |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Get user id |
| 398 | * |
| 399 | * @return int |
| 400 | */ |
| 401 | public static function get_current_user_id() { |
| 402 | global $current_user; |
| 403 | if ( isset( $current_user->ID ) ) { |
| 404 | return $current_user->ID; |
| 405 | } else { |
| 406 | $wp_user = wp_get_current_user(); |
| 407 | if ( isset( $wp_user->ID ) ) { |
| 408 | return $wp_user->ID; |
| 409 | } else { |
| 410 | return - 1; |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Run shortcode from anywhere with code id |
| 417 | * |
| 418 | * @param integer $code_id Code ID. |
| 419 | * @return void |
| 420 | */ |
| 421 | public static function run_shortcode_id_from_anywhere( $code_id ) { |
| 422 | $cm = new Code_Manager\Code_Manager(); |
| 423 | |
| 424 | if ( is_array( $code_id ) ) { |
| 425 | foreach ( $code_id as $id ) { |
| 426 | echo $cm->run_shortcode_id_from_anywhere( $id ); |
| 427 | } |
| 428 | } else { |
| 429 | $cm->run_shortcode_id_from_anywhere( $code_id ); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Run shortcode from anywhere width code name |
| 435 | * |
| 436 | * @param string $code_name Code name. |
| 437 | * @return void |
| 438 | */ |
| 439 | public static function run_shortcode_name_from_anywhere( $code_name ) { |
| 440 | $cm = new Code_Manager\Code_Manager(); |
| 441 | |
| 442 | if ( is_array( $code_name ) ) { |
| 443 | foreach ( $code_name as $name ) { |
| 444 | $cm->run_shortcode_name_from_anywhere( $name ); |
| 445 | } |
| 446 | } else { |
| 447 | $cm->run_shortcode_name_from_anywhere( $code_name ); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | } |
| 452 |