| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hostinger; |
| 4 |
|
| 5 |
use Hostinger\Admin\PluginSettings; |
| 6 |
use Hostinger\WpHelper\Utils; |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
class Hooks { |
| 11 |
public function __construct() { |
| 12 |
add_filter( 'xmlrpc_enabled', array( $this, 'check_xmlrpc_enabled' ) ); |
| 13 |
add_filter( 'wp_is_application_passwords_available', array( $this, 'check_authentication_password_enabled' ) ); |
| 14 |
add_filter( 'wp_headers', array( $this, 'check_pingback' ) ); |
| 15 |
add_filter( 'plugins_loaded', array( $this, 'plugins_loaded' ) ); |
| 16 |
|
| 17 |
add_action( 'update_option_woocommerce_coming_soon', array( $this, 'litespeed_flush_cache' ) ); |
| 18 |
add_action( 'update_option_woocommerce_store_pages_only', array( $this, 'litespeed_flush_cache' ) ); |
| 19 |
add_action( 'upgrader_process_complete', array( $this, 'disable_auth_passwords_on_update' ), 10, 2 ); |
| 20 |
} |
| 21 |
|
| 22 |
public function disable_auth_passwords_on_update( \WP_Upgrader $upgrader_object, array $options ): void { |
| 23 |
if ( $options['action'] !== 'update' || $options['type'] !== 'plugin' || empty( $options['plugins'] ) ) { |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
if ( ! in_array( 'hostinger/hostinger.php', $options['plugins'], true ) ) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
$settings = get_option( HOSTINGER_PLUGIN_SETTINGS_OPTION, array() ); |
| 32 |
|
| 33 |
if ( ! empty( $settings['disable_authentication_password'] ) ) { |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
$options = new DefaultOptions(); |
| 38 |
$options->configure_authentication_password(); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
public function plugins_loaded() { |
| 45 |
$utils = new Utils(); |
| 46 |
$plugin_settings = new PluginSettings(); |
| 47 |
$settings = $plugin_settings->get_plugin_settings(); |
| 48 |
|
| 49 |
if ( defined( 'WP_CLI' ) && \WP_CLI ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
// Xmlrpc. |
| 54 |
if ( $settings->get_disable_xml_rpc() && $utils->isThisPage( 'xmlrpc.php' ) ) { |
| 55 |
exit( 'Disabled' ); |
| 56 |
} |
| 57 |
|
| 58 |
// SSL redirect. |
| 59 |
if ( $settings->get_force_https() && ! is_ssl() ) { |
| 60 |
if ( isset( $_SERVER['HTTP_HOST'] ) && isset( $_SERVER['REQUEST_URI'] ) ) { |
| 61 |
$host = sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ); |
| 62 |
|
| 63 |
if ( $settings->get_force_www() && strpos( $host, 'www.' ) === false ) { |
| 64 |
$host = 'www.' . $host; |
| 65 |
} |
| 66 |
|
| 67 |
wp_safe_redirect( 'https://' . $host . sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 301 ); |
| 68 |
exit; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
// Force www. |
| 73 |
if ( $settings->get_force_www() ) { |
| 74 |
if ( isset( $_SERVER['HTTP_HOST'] ) && isset( $_SERVER['REQUEST_URI'] ) ) { |
| 75 |
$host = sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ); |
| 76 |
|
| 77 |
if ( strpos( $host, 'www.' ) === false ) { |
| 78 |
wp_safe_redirect( 'https://www.' . $host . sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 301 ); |
| 79 |
exit; |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* @param mixed $headers |
| 87 |
* |
| 88 |
* @return mixed |
| 89 |
*/ |
| 90 |
public function check_pingback( $headers ) { |
| 91 |
$plugin_settings = new PluginSettings(); |
| 92 |
$settings = $plugin_settings->get_plugin_settings(); |
| 93 |
|
| 94 |
if ( $settings->get_disable_xml_rpc() ) { |
| 95 |
unset( $headers['X-Pingback'] ); |
| 96 |
} |
| 97 |
|
| 98 |
return $headers; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* @return bool |
| 103 |
*/ |
| 104 |
public function check_xmlrpc_enabled(): bool { |
| 105 |
$plugin_settings = new PluginSettings(); |
| 106 |
$settings = $plugin_settings->get_plugin_settings(); |
| 107 |
|
| 108 |
if ( $settings->get_disable_xml_rpc() ) { |
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
return true; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* @return bool |
| 117 |
*/ |
| 118 |
public function check_authentication_password_enabled(): bool { |
| 119 |
$plugin_settings = new PluginSettings(); |
| 120 |
$settings = $plugin_settings->get_plugin_settings(); |
| 121 |
|
| 122 |
if ( $settings->get_disable_authentication_password() ) { |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
return true; |
| 127 |
} |
| 128 |
|
| 129 |
public function litespeed_flush_cache(): void { |
| 130 |
if ( has_action( 'litespeed_purge_all' ) ) { |
| 131 |
do_action( 'litespeed_purge_all' ); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|