HookDeactivator.php
174 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Hook Deactivator settings |
| 4 | * |
| 5 | * User can disable Jalali date in some hooks |
| 6 | * Format: mainCallFunction,methodOfClass,ClassName |
| 7 | * Example: wp_date,render_field,acf_field_date_picker |
| 8 | * |
| 9 | * Without class: |
| 10 | * Format: mainCallFunction,usedFunction |
| 11 | * Example: wp_date,acf_format_date |
| 12 | */ |
| 13 | |
| 14 | namespace WPParsidate\App\Tools; |
| 15 | |
| 16 | use WPParsidate\Helper\{Cache, WordPress}; |
| 17 | use WPParsidate\Settings\Settings; |
| 18 | |
| 19 | class HookDeactivator { |
| 20 | private const sectionID = 'hook-deactivator'; |
| 21 | |
| 22 | public function __construct() { |
| 23 | add_filter( 'wp_parsidate_tools_settings_sections', [ $this, 'addSectionSettings' ] ); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Check hook list for deactivating WP-Parsidate functionality, If disable return True. |
| 28 | * |
| 29 | * @return bool Disable status |
| 30 | */ |
| 31 | public static function checkDisable(): bool { |
| 32 | if ( apply_filters( 'wp_parsidate_hook_deactivator_check_disable', false ) ) { |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | if ( WordPress::isFeed() ) { |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | $i = 0; |
| 41 | $dis_hook = self::getList(); |
| 42 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace |
| 43 | $calls = debug_backtrace(); |
| 44 | unset( $calls[0], $calls[1], $calls[2] ); |
| 45 | |
| 46 | foreach ( $calls as $i => $call ) { |
| 47 | unset( $calls[ $i ] ); |
| 48 | |
| 49 | if ( $call['function'] === 'apply_filters' && empty( $call['class'] ) ) { |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | $func = $calls[ ++ $i ]['function']; |
| 55 | |
| 56 | if ( empty( $dis_hook[ $func ] ) ) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | $hooks = $dis_hook[ $func ]; |
| 61 | |
| 62 | unset( $calls[ $i ] ); |
| 63 | |
| 64 | foreach ( $calls as $i => $call ) { |
| 65 | foreach ( $hooks as $hook ) { |
| 66 | $hook['class'] = trim( $hook['class'] ); |
| 67 | |
| 68 | if ( ( isset( $call['class'] ) && empty( $hook['class'] ) ) || ( ! isset( $call['class'] ) && ! empty( $hook['class'] ) ) ) { |
| 69 | continue; |
| 70 | } |
| 71 | |
| 72 | if ( ! empty( $hook['func'] ) && ( $call['function'] !== trim( $hook['func'] ) ) ) { |
| 73 | continue; |
| 74 | } |
| 75 | |
| 76 | /*if ( |
| 77 | ! empty( $call['function'] ) && |
| 78 | ! empty( $hook['func'] ) && |
| 79 | ! empty( $call['class'] ) && |
| 80 | ! empty( $hook['class'] ) && |
| 81 | $call['function'] === $hook['func'] && |
| 82 | $call['class'] === $hook['class'] |
| 83 | ) { |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | if ( |
| 88 | ! empty( $call['function'] ) && |
| 89 | ! empty( $hook['func'] ) && |
| 90 | empty( $call['class'] ) && |
| 91 | empty( $hook['class'] ) && |
| 92 | $call['function'] === $hook['func'] |
| 93 | ) { |
| 94 | return true; |
| 95 | }*/ |
| 96 | |
| 97 | if ( ( ! isset( $call['class'] ) && empty( $hook['class'] ) ) || $call['class'] === $hook['class'] ) { |
| 98 | return true; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | public static function getList(): array { |
| 107 | $cache = Cache::get( 'hook_deactivator_list', false ); |
| 108 | if ( is_array( $cache ) ) { |
| 109 | return $cache; |
| 110 | } |
| 111 | |
| 112 | $hooks = array(); |
| 113 | $rawList = Settings::get( 'hook_deactivator_list', '' ); |
| 114 | $rawList = apply_filters( 'wp_parsidate_hook_deactivator_raw_list', $rawList ); |
| 115 | $lists = explode( "\n", $rawList ); |
| 116 | |
| 117 | foreach ( $lists as $item ) { |
| 118 | $item = explode( ',', $item ); |
| 119 | $item = array_map( 'trim', $item ); |
| 120 | |
| 121 | if ( count( $item ) < 2 ) { |
| 122 | continue; |
| 123 | } |
| 124 | |
| 125 | $hooks[ $item[0] ][] = array( 'func' => $item[1], 'class' => ( $item[2] ?? '' ) ); |
| 126 | } |
| 127 | |
| 128 | Cache::set( 'hook_deactivator_list', $hooks ); |
| 129 | |
| 130 | return apply_filters( 'wp_parsidate_hook_deactivator_list', $hooks ); |
| 131 | } |
| 132 | |
| 133 | public function addSectionSettings( array $sections ): array { |
| 134 | $listPlaceholder = esc_html__( 'Format:', 'wp-parsidate' ) . "\n" . |
| 135 | "<code>mainCallFunction,methodOfClass,ClassName</code>\n" . |
| 136 | esc_html__( 'Example:', 'wp-parsidate' ) . "\n" . |
| 137 | "<code>wp_date,render_field,acf_field_date_picker</code>\n\n" . |
| 138 | esc_html__( 'Without class format:', 'wp-parsidate' ) . "\n" . |
| 139 | "<code>mainCallFunction,usedFunction</code>\n" . |
| 140 | esc_html__( 'Example:', 'wp-parsidate' ) . "\n" . |
| 141 | "<code>wp_date,acf_format_date</code>\n"; |
| 142 | |
| 143 | $settings = [ |
| 144 | 'start_grid_hook_deactivator' => array( |
| 145 | 'title' => esc_html__( 'Hooks', 'wp-parsidate' ), |
| 146 | 'type' => 'startGrid', |
| 147 | ), |
| 148 | 'hook_deactivator_list' => array( |
| 149 | 'id' => 'hook_deactivator_list', |
| 150 | 'title' => esc_html__( 'Hook list', 'wp-parsidate' ), |
| 151 | 'type' => 'textarea', |
| 152 | 'desc' => esc_html__( 'Enter hook, function and class to remove Parsidate filter from it', |
| 153 | 'wp-parsidate' ) . "\n\n" . $listPlaceholder, |
| 154 | 'class' => 'ltr-field', |
| 155 | 'placeholder' => wp_strip_all_tags( $listPlaceholder ), |
| 156 | 'attributes' => array( |
| 157 | 'rows' => 10 |
| 158 | ) |
| 159 | ), |
| 160 | 'end_grid_hook_deactivator' => array( |
| 161 | 'type' => 'endGrid', |
| 162 | ) |
| 163 | ]; |
| 164 | |
| 165 | $sections[ self::sectionID ] = array( |
| 166 | 'title' => esc_html__( 'Hook deactivator', 'wp-parsidate' ), |
| 167 | 'desc' => esc_html__( 'Disable plugin hooks', 'wp-parsidate' ), |
| 168 | 'settings' => $settings |
| 169 | ); |
| 170 | |
| 171 | return $sections; |
| 172 | } |
| 173 | } |
| 174 |