| @@ -1,12 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Hostinger; |
| 4 | 4 | |
| 5 | -use Hostinger\Admin\Menu; | |
| 6 | 5 | use Hostinger\Admin\PluginSettings; |
| 7 | -use Hostinger\Admin\Jobs\NotifyMcpJob; | |
| 8 | -use Hostinger\Mcp\EventHandlerFactory; | |
| 9 | 6 | use Hostinger\WpHelper\Utils; |
| 10 | 7 | |
| 11 | 8 | defined( 'ABSPATH' ) || exit; |
| 12 | 9 | |
| @@ -11,176 +8,93 @@ | ||
| 11 | 8 | defined( 'ABSPATH' ) || exit; |
| 12 | 9 | |
| 13 | 10 | class Hooks { |
| 14 | 11 | public function __construct() { |
| 12 | + // XMLRpc / Force SSL | |
| 15 | 13 | add_filter( 'xmlrpc_enabled', array( $this, 'check_xmlrpc_enabled' ) ); |
| 16 | - add_filter( 'wp_is_application_passwords_available', array( $this, 'check_authentication_password_enabled' ) ); | |
| 17 | - add_filter( 'wp_die_handler', array( $this, 'maybe_customize_application_password_die_handler' ) ); | |
| 18 | 14 | add_filter( 'wp_headers', array( $this, 'check_pingback' ) ); |
| 19 | - add_action( 'init', array( $this, 'plugins_loaded' ) ); | |
| 15 | + add_filter( 'plugins_loaded', array( $this, 'plugins_loaded' ) ); | |
| 16 | + | |
| 20 | 17 | add_action( 'update_option_woocommerce_coming_soon', array( $this, 'litespeed_flush_cache' ) ); |
| 21 | 18 | add_action( 'update_option_woocommerce_store_pages_only', array( $this, 'litespeed_flush_cache' ) ); |
| 22 | - add_action( 'transition_post_status', array( $this, 'handle_transition_post_status' ), 10, 3 ); | |
| 23 | - add_action( 'updated_option', array( $this, 'handle_updated_option' ), 10, 3 ); | |
| 24 | 19 | } |
| 25 | 20 | |
| 26 | - public function handle_transition_post_status( string $new_status, string $old_status, \WP_Post $post ): void { | |
| 27 | - if ( $new_status === 'publish' || $old_status === 'publish' ) { | |
| 28 | - do_action( | |
| 29 | - NotifyMcpJob::JOB_NAME, | |
| 30 | - array( | |
| 31 | - 'event' => EventHandlerFactory::MCP_EVENT_PAGE_UPDATED, | |
| 32 | - 'post_id' => $post->ID, | |
| 33 | - ) | |
| 34 | - ); | |
| 35 | - } | |
| 36 | - } | |
| 21 | + /** | |
| 22 | + * @return void | |
| 23 | + */ | |
| 24 | + public function plugins_loaded() { | |
| 25 | + $utils = new Utils(); | |
| 26 | + $plugin_settings = new PluginSettings(); | |
| 27 | + $settings = $plugin_settings->get_plugin_settings(); | |
| 37 | 28 | |
| 38 | - public function handle_updated_option( string $option, mixed $old_value, mixed $value ): void { | |
| 39 | - if ( $option === 'cron' || $this->is_transient( $option ) ) { | |
| 40 | - return; | |
| 41 | - } | |
| 29 | + if ( defined( 'WP_CLI' ) && \WP_CLI ) { | |
| 30 | + return; | |
| 31 | + } | |
| 42 | 32 | |
| 43 | - if ( $old_value !== $value ) { | |
| 44 | - do_action( NotifyMcpJob::JOB_NAME, array( 'event' => EventHandlerFactory::MCP_EVENT_UPDATED ) ); | |
| 45 | - } | |
| 33 | + // Xmlrpc. | |
| 34 | + if ( $settings->get_disable_xml_rpc() && $utils->isThisPage( 'xmlrpc.php' ) ) { | |
| 35 | + exit( 'Disabled' ); | |
| 36 | + } | |
| 46 | 37 | |
| 47 | - if ( $option === HOSTINGER_PLUGIN_SETTINGS_OPTION && isset( $value['optin_mcp'] ) && isset( $old_value['optin_mcp'] ) ) { | |
| 48 | - if ( $old_value['optin_mcp'] !== $value['optin_mcp'] ) { | |
| 49 | - do_action( | |
| 50 | - NotifyMcpJob::JOB_NAME, | |
| 51 | - array( | |
| 52 | - 'event' => EventHandlerFactory::MCP_EVENT_OPTIN_TOGGLED, | |
| 53 | - 'value' => $value['optin_mcp'], | |
| 54 | - ) | |
| 55 | - ); | |
| 56 | - } | |
| 57 | - } | |
| 58 | - } | |
| 38 | + // SSL redirect. | |
| 39 | + if ( $settings->get_force_https() && ! is_ssl() ) { | |
| 40 | + if ( isset( $_SERVER['HTTP_HOST'] ) && isset( $_SERVER['REQUEST_URI'] ) ) { | |
| 41 | + $host = sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ); | |
| 59 | 42 | |
| 60 | - /** | |
| 61 | - * @return void | |
| 62 | - */ | |
| 63 | - public function plugins_loaded() { | |
| 64 | - $utils = new Utils(); | |
| 65 | - $plugin_settings = new PluginSettings(); | |
| 66 | - $settings = $plugin_settings->get_plugin_settings(); | |
| 43 | + if ( $settings->get_force_www() && strpos( $host, 'www.' ) === false ) { | |
| 44 | + $host = 'www.' . $host; | |
| 45 | + } | |
| 67 | 46 | |
| 68 | - if ( defined( 'WP_CLI' ) && \WP_CLI ) { | |
| 69 | - return; | |
| 70 | - } | |
| 47 | + wp_safe_redirect( 'https://' . $host . sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 301 ); | |
| 48 | + exit; | |
| 49 | + } | |
| 50 | + } | |
| 71 | 51 | |
| 72 | - // Xmlrpc. | |
| 73 | - if ( $settings->get_disable_xml_rpc() && $utils->isThisPage( 'xmlrpc.php' ) ) { | |
| 74 | - exit( 'Disabled' ); | |
| 75 | - } | |
| 52 | + // Force www. | |
| 53 | + if ( $settings->get_force_www() ) { | |
| 54 | + if ( isset( $_SERVER['HTTP_HOST'] ) && isset( $_SERVER['REQUEST_URI'] ) ) { | |
| 55 | + $host = sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ); | |
| 76 | 56 | |
| 77 | - // SSL redirect. | |
| 78 | - if ( $settings->get_force_https() && ! is_ssl() ) { | |
| 79 | - if ( isset( $_SERVER['HTTP_HOST'] ) && isset( $_SERVER['REQUEST_URI'] ) ) { | |
| 80 | - $host = sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ); | |
| 57 | + if ( strpos( $host, 'www.' ) === false ) { | |
| 58 | + wp_safe_redirect( 'https://www.' . $host . sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 301 ); | |
| 59 | + exit; | |
| 60 | + } | |
| 61 | + } | |
| 62 | + } | |
| 63 | + } | |
| 81 | 64 | |
| 82 | - if ( $settings->get_force_www() && strpos( $host, 'www.' ) === false ) { | |
| 83 | - $host = 'www.' . $host; | |
| 84 | - } | |
| 65 | + /** | |
| 66 | + * @param mixed $headers | |
| 67 | + * | |
| 68 | + * @return mixed | |
| 69 | + */ | |
| 70 | + public function check_pingback( $headers ) { | |
| 71 | + $plugin_settings = new PluginSettings(); | |
| 72 | + $settings = $plugin_settings->get_plugin_settings(); | |
| 85 | 73 | |
| 86 | - wp_safe_redirect( 'https://' . $host . sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 301 ); | |
| 87 | - exit; | |
| 88 | - } | |
| 89 | - } | |
| 74 | + if ( $settings->get_disable_xml_rpc() ) { | |
| 75 | + unset( $headers['X-Pingback'] ); | |
| 76 | + } | |
| 90 | 77 | |
| 91 | - // Force www. | |
| 92 | - if ( $settings->get_force_www() ) { | |
| 93 | - if ( isset( $_SERVER['HTTP_HOST'] ) && isset( $_SERVER['REQUEST_URI'] ) ) { | |
| 94 | - $host = sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ); | |
| 78 | + return $headers; | |
| 79 | + } | |
| 95 | 80 | |
| 96 | - if ( strpos( $host, 'www.' ) === false ) { | |
| 97 | - wp_safe_redirect( 'https://www.' . $host . sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 301 ); | |
| 98 | - exit; | |
| 99 | - } | |
| 100 | - } | |
| 101 | - } | |
| 102 | - } | |
| 81 | + /** | |
| 82 | + * @return bool | |
| 83 | + */ | |
| 84 | + public function check_xmlrpc_enabled(): bool { | |
| 85 | + $plugin_settings = new PluginSettings(); | |
| 86 | + $settings = $plugin_settings->get_plugin_settings(); | |
| 103 | 87 | |
| 104 | - /** | |
| 105 | - * @param mixed $headers | |
| 106 | - * | |
| 107 | - * @return mixed | |
| 108 | - */ | |
| 109 | - public function check_pingback( $headers ) { | |
| 110 | - $plugin_settings = new PluginSettings(); | |
| 111 | - $settings = $plugin_settings->get_plugin_settings(); | |
| 88 | + if ( $settings->get_disable_xml_rpc() ) { | |
| 89 | + return false; | |
| 90 | + } | |
| 112 | 91 | |
| 113 | - if ( $settings->get_disable_xml_rpc() ) { | |
| 114 | - unset( $headers['X-Pingback'] ); | |
| 115 | - } | |
| 92 | + return true; | |
| 93 | + } | |
| 116 | 94 | |
| 117 | - return $headers; | |
| 118 | - } | |
| 119 | - | |
| 120 | - /** | |
| 121 | - * @param bool $enabled | |
| 122 | - * | |
| 123 | - * @return bool | |
| 124 | - */ | |
| 125 | - public function check_xmlrpc_enabled( bool $enabled ): bool { | |
| 126 | - $plugin_settings = new PluginSettings(); | |
| 127 | - $settings = $plugin_settings->get_plugin_settings(); | |
| 128 | - | |
| 129 | - if ( $settings->get_disable_xml_rpc() ) { | |
| 130 | - return false; | |
| 131 | - } | |
| 132 | - | |
| 133 | - return $enabled; | |
| 134 | - } | |
| 135 | - | |
| 136 | - /** | |
| 137 | - * @return bool | |
| 138 | - */ | |
| 139 | - public function check_authentication_password_enabled(): bool { | |
| 140 | - $plugin_settings = new PluginSettings(); | |
| 141 | - $settings = $plugin_settings->get_plugin_settings(); | |
| 142 | - | |
| 143 | - if ( $settings->get_disable_authentication_password() ) { | |
| 144 | - return false; | |
| 145 | - } | |
| 146 | - | |
| 147 | - return true; | |
| 148 | - } | |
| 149 | - | |
| 150 | - public function maybe_customize_application_password_die_handler( $handler ) { | |
| 151 | - $plugin_settings = new PluginSettings(); | |
| 152 | - $settings = $plugin_settings->get_plugin_settings(); | |
| 153 | - | |
| 154 | - if ( ! $settings->get_disable_authentication_password() ) { | |
| 155 | - return $handler; | |
| 156 | - } | |
| 157 | - | |
| 158 | - return function ( $message, $title = '', $args = array() ) use ( $handler ) { | |
| 159 | - if ( is_string( $message ) && $message === __( 'Application passwords are not available.' ) ) { | |
| 160 | - $message = $this->get_application_password_disabled_message(); | |
| 161 | - } | |
| 162 | - | |
| 163 | - call_user_func( $handler, $message, $title, $args ); | |
| 164 | - }; | |
| 165 | - } | |
| 166 | - | |
| 167 | - private function get_application_password_disabled_message(): string { | |
| 168 | - $settings_url = admin_url( 'admin.php?page=' . Menu::MENU_SLUG ); | |
| 169 | - | |
| 170 | - return sprintf( | |
| 171 | - /* translators: %s: URL to the Hostinger Tools settings page. */ | |
| 172 | - __( 'Application passwords have been disabled by Hostinger for security reasons. You can turn them back on from <a href="%s">Hostinger Tools → Security settings</a>.', 'hostinger' ), | |
| 173 | - esc_url( $settings_url ) | |
| 174 | - ); | |
| 175 | - } | |
| 176 | - | |
| 177 | 95 | public function litespeed_flush_cache(): void { |
| 178 | 96 | if ( has_action( 'litespeed_purge_all' ) ) { |
| 179 | 97 | do_action( 'litespeed_purge_all' ); |
| 180 | 98 | } |
| 181 | - } | |
| 182 | - | |
| 183 | - private function is_transient( $option ): bool { | |
| 184 | - return str_contains( $option, '_transient' ); | |
| 185 | 99 | } |
| 186 | 100 | } |