PluginProbe
Hostinger Tools / 3.0.51
Hostinger Tools v3.0.51
3.0.77 3.0.76 3.0.75 3.0.74 3.0.73 3.0.72 3.0.71 3.0.70 3.0.69 3.0.68 3.0.67 3.0.66 1.8.1 1.8.2 1.8.3 1.9.1 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.4 All 108 releases
hostinger / includes / Hooks.php

Hooks.php in Hostinger Tools 3.0.51, at includes/Hooks.php

176 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Hostinger;
4
5 use Hostinger\Admin\PluginSettings;
6 use Hostinger\Admin\Jobs\NotifyMcpJob;
7 use Hostinger\Mcp\EventHandlerFactory;
8 use Hostinger\WpHelper\Utils;
9
10 defined( 'ABSPATH' ) || exit;
11
12 class Hooks {
13 public function __construct() {
14 add_filter( 'xmlrpc_enabled', array( $this, 'check_xmlrpc_enabled' ) );
15 add_filter( 'wp_is_application_passwords_available', array( $this, 'check_authentication_password_enabled' ) );
16 add_filter( 'wp_headers', array( $this, 'check_pingback' ) );
17 add_filter( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
18 add_action( 'update_option_woocommerce_coming_soon', array( $this, 'litespeed_flush_cache' ) );
19 add_action( 'update_option_woocommerce_store_pages_only', array( $this, 'litespeed_flush_cache' ) );
20 add_action( 'upgrader_process_complete', array( $this, 'disable_auth_passwords_on_update' ), 10, 2 );
21 add_action( 'transition_post_status', array( $this, 'handle_transition_post_status' ), 10, 3 );
22 add_action( 'updated_option', array( $this, 'handle_updated_option' ), 10, 3 );
23 }
24
25 public function handle_transition_post_status( string $new_status, string $old_status, \WP_Post $post ): void {
26 if ( $new_status === 'publish' || $old_status === 'publish' ) {
27 do_action(
28 NotifyMcpJob::JOB_NAME,
29 array(
30 'event' => EventHandlerFactory::MCP_EVENT_PAGE_UPDATED,
31 'post_id' => $post->ID,
32 )
33 );
34 }
35 }
36
37 public function handle_updated_option( string $option, mixed $old_value, mixed $value ): void {
38 if ( $option === 'cron' || $this->is_transient( $option ) ) {
39 return;
40 }
41
42 if ( $old_value !== $value ) {
43 do_action( NotifyMcpJob::JOB_NAME, array( 'event' => EventHandlerFactory::MCP_EVENT_UPDATED ) );
44 }
45
46 if ( $option === HOSTINGER_PLUGIN_SETTINGS_OPTION && isset( $value['optin_mcp'] ) && isset( $old_value['optin_mcp'] ) ) {
47 if ( $old_value['optin_mcp'] !== $value['optin_mcp'] ) {
48 do_action(
49 NotifyMcpJob::JOB_NAME,
50 array(
51 'event' => EventHandlerFactory::MCP_EVENT_OPTIN_TOGGLED,
52 'value' => $value['optin_mcp'],
53 )
54 );
55 }
56 }
57 }
58
59 public function disable_auth_passwords_on_update( \WP_Upgrader $upgrader_object, array $options ): void {
60 if ( $options['action'] !== 'update' || $options['type'] !== 'plugin' || empty( $options['plugins'] ) ) {
61 return;
62 }
63
64 if ( ! in_array( 'hostinger/hostinger.php', $options['plugins'], true ) ) {
65 return;
66 }
67
68 $settings = get_option( HOSTINGER_PLUGIN_SETTINGS_OPTION, array() );
69
70 if ( ! empty( $settings['disable_authentication_password'] ) ) {
71 return;
72 }
73
74 $options = new DefaultOptions();
75 $options->configure_authentication_password();
76 }
77
78 /**
79 * @return void
80 */
81 public function plugins_loaded() {
82 $utils = new Utils();
83 $plugin_settings = new PluginSettings();
84 $settings = $plugin_settings->get_plugin_settings();
85
86 if ( defined( 'WP_CLI' ) && \WP_CLI ) {
87 return;
88 }
89
90 // Xmlrpc.
91 if ( $settings->get_disable_xml_rpc() && $utils->isThisPage( 'xmlrpc.php' ) ) {
92 exit( 'Disabled' );
93 }
94
95 // SSL redirect.
96 if ( $settings->get_force_https() && ! is_ssl() ) {
97 if ( isset( $_SERVER['HTTP_HOST'] ) && isset( $_SERVER['REQUEST_URI'] ) ) {
98 $host = sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) );
99
100 if ( $settings->get_force_www() && strpos( $host, 'www.' ) === false ) {
101 $host = 'www.' . $host;
102 }
103
104 wp_safe_redirect( 'https://' . $host . sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 301 );
105 exit;
106 }
107 }
108
109 // Force www.
110 if ( $settings->get_force_www() ) {
111 if ( isset( $_SERVER['HTTP_HOST'] ) && isset( $_SERVER['REQUEST_URI'] ) ) {
112 $host = sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) );
113
114 if ( strpos( $host, 'www.' ) === false ) {
115 wp_safe_redirect( 'https://www.' . $host . sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 301 );
116 exit;
117 }
118 }
119 }
120 }
121
122 /**
123 * @param mixed $headers
124 *
125 * @return mixed
126 */
127 public function check_pingback( $headers ) {
128 $plugin_settings = new PluginSettings();
129 $settings = $plugin_settings->get_plugin_settings();
130
131 if ( $settings->get_disable_xml_rpc() ) {
132 unset( $headers['X-Pingback'] );
133 }
134
135 return $headers;
136 }
137
138 /**
139 * @return bool
140 */
141 public function check_xmlrpc_enabled(): bool {
142 $plugin_settings = new PluginSettings();
143 $settings = $plugin_settings->get_plugin_settings();
144
145 if ( $settings->get_disable_xml_rpc() ) {
146 return false;
147 }
148
149 return true;
150 }
151
152 /**
153 * @return bool
154 */
155 public function check_authentication_password_enabled(): bool {
156 $plugin_settings = new PluginSettings();
157 $settings = $plugin_settings->get_plugin_settings();
158
159 if ( $settings->get_disable_authentication_password() ) {
160 return false;
161 }
162
163 return true;
164 }
165
166 public function litespeed_flush_cache(): void {
167 if ( has_action( 'litespeed_purge_all' ) ) {
168 do_action( 'litespeed_purge_all' );
169 }
170 }
171
172 private function is_transient( $option ): bool {
173 return str_contains( $option, '_transient' );
174 }
175 }
176