class-classes-helper.php
3 years ago
class-file-writer.php
3 years ago
class-php-helper.php
3 years ago
class-user-helper.php
3 years ago
class-wp-helper.php
3 years ago
class-classes-helper.php
174 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Responsible for the User's operations. |
| 4 | * |
| 5 | * @package wp2fa |
| 6 | * @subpackage helpers |
| 7 | * |
| 8 | * @since 2.4.0 |
| 9 | * |
| 10 | * @copyright 2023 WP White Security |
| 11 | * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0 |
| 12 | * |
| 13 | * @see https://wordpress.org/plugins/wp-2fa/ |
| 14 | */ |
| 15 | |
| 16 | declare(strict_types=1); |
| 17 | |
| 18 | namespace WP2FA\Admin\Helpers; |
| 19 | |
| 20 | // Exit if accessed directly. |
| 21 | if ( ! defined( 'ABSPATH' ) ) { |
| 22 | exit; |
| 23 | } |
| 24 | |
| 25 | if ( ! class_exists( 'WP2FA\Admin\Helpers\Classes_Helper' ) ) { |
| 26 | /** |
| 27 | * Responsible for the proper class loading. |
| 28 | */ |
| 29 | class Classes_Helper { |
| 30 | /** |
| 31 | * Holds the classmap array for more info check @see autoload_classmap.php from the auto generated Composer file. |
| 32 | * |
| 33 | * @var array |
| 34 | * |
| 35 | * @since 2.4.0 |
| 36 | */ |
| 37 | private static $class_map = array(); |
| 38 | |
| 39 | /** |
| 40 | * Caches and returns the classmap structure of the plugin. |
| 41 | * |
| 42 | * @since 2.4.0 |
| 43 | */ |
| 44 | public static function get_class_map(): array { |
| 45 | if ( empty( self::$class_map ) ) { |
| 46 | self::$class_map = require WP_2FA_PATH . 'vendor/composer/autoload_classmap.php'; |
| 47 | } |
| 48 | |
| 49 | return self::$class_map; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Returns the class by its filename. Checks if it exists and returns it as string. Returns false otherwise. |
| 54 | * |
| 55 | * @param string $file - The filename of the class to check. |
| 56 | * |
| 57 | * @return string|false |
| 58 | * |
| 59 | * @since 2.4.0 |
| 60 | */ |
| 61 | public static function get_class_by_filename( string $file ) { |
| 62 | if ( in_array( $file, self::get_class_map(), true ) ) { |
| 63 | $class = array_search( $file, self::get_class_map(), true ); |
| 64 | |
| 65 | if ( class_exists( $class ) ) { |
| 66 | return $class; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Extracts subclasses of the given class, optionally abstract classes could be included as well. |
| 75 | * |
| 76 | * @param string $current_class - The calling class. |
| 77 | * @param string $base_class - The class which subclasses should be extracted. |
| 78 | * @param bool $exclude_abstracts - Should we exclude abstract classes. |
| 79 | * |
| 80 | * @since 2.4.0 |
| 81 | */ |
| 82 | public static function get_subclasses_of_class( string $current_class, string $base_class, bool $exclude_abstracts = true ): array { |
| 83 | $matching_classes = array(); |
| 84 | foreach ( array_keys( self::get_class_map() ) as $class_name ) { |
| 85 | if ( $current_class !== $class_name && is_subclass_of( $class_name, $base_class ) ) { |
| 86 | if ( $exclude_abstracts && ( false !== strpos( $class_name, 'Abstract' ) ) ) { |
| 87 | continue; |
| 88 | } |
| 89 | $matching_classes[ $class_name ] = $class_name; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return $matching_classes; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns all the classes which are part of the given namespace. |
| 98 | * |
| 99 | * @param string $namespace - The namespace to search for. |
| 100 | * |
| 101 | * @return array |
| 102 | * |
| 103 | * @since 2.4.0 |
| 104 | */ |
| 105 | public static function get_classes_by_namespace( string $namespace ) { |
| 106 | if ( 0 === strpos( $namespace, '\\' ) ) { |
| 107 | $namespace = ltrim( $namespace, '\\' ); |
| 108 | } |
| 109 | |
| 110 | $namespace = rtrim( $namespace, '\\' ); |
| 111 | |
| 112 | $term_upper = strtoupper( $namespace ); |
| 113 | |
| 114 | return array_filter( |
| 115 | array_keys( self::get_class_map() ), |
| 116 | function ( $class ) use ( $term_upper ) { |
| 117 | $class_name = strtoupper( $class ); |
| 118 | |
| 119 | /** |
| 120 | * Find class name, by finding the last occurrence of the \ |
| 121 | * if it is false (from the strrchr) then class does not belong to any namespace currently. |
| 122 | */ |
| 123 | $esc_position = strrchr( $class_name, '\\' ); |
| 124 | |
| 125 | if ( false !== $esc_position ) { |
| 126 | $class_name_no_ns = substr( $esc_position, 1 ); |
| 127 | } else { |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | if ( $class_name_no_ns && |
| 132 | $term_upper . '\\' . $class_name_no_ns === $class_name && |
| 133 | false === strpos( $class_name, strtoupper( 'Abstract' ) ) && |
| 134 | false === strpos( $class_name, strtoupper( 'Interface' ) ) |
| 135 | ) { |
| 136 | return $class; |
| 137 | } |
| 138 | |
| 139 | return false; |
| 140 | } |
| 141 | ); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Search for classes by given term. |
| 146 | * |
| 147 | * @param string $term - The term to search for. |
| 148 | * |
| 149 | * @return array |
| 150 | * |
| 151 | * @since 2.4.0 |
| 152 | */ |
| 153 | public static function get_classes_with_term( $term ) { |
| 154 | $term_upper = strtoupper( $term ); |
| 155 | |
| 156 | return array_filter( |
| 157 | self::get_class_map(), |
| 158 | function ( $class ) use ( $term_upper ) { |
| 159 | $class_name = strtoupper( $class ); |
| 160 | if ( |
| 161 | false !== strpos( $class_name, $term_upper ) && |
| 162 | false === strpos( $class_name, strtoupper( 'Abstract' ) ) && |
| 163 | false === strpos( $class_name, strtoupper( 'Interface' ) ) |
| 164 | ) { |
| 165 | return $class; |
| 166 | } |
| 167 | |
| 168 | return false; |
| 169 | } |
| 170 | ); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 |