| 1 |
<?php |
| 2 |
/** |
| 3 |
* Hook Loader class. |
| 4 |
* |
| 5 |
* Handles all hook registrations and callbacks for the plugin. |
| 6 |
* |
| 7 |
* @package WebberZone\Top_Ten |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace WebberZone\Top_Ten; |
| 11 |
|
| 12 |
use WebberZone\Top_Ten\Admin\Activator; |
| 13 |
use WebberZone\Top_Ten\Util\Hook_Registry; |
| 14 |
use WebberZone\Top_Ten\Top_Ten_Core_Query; |
| 15 |
use WebberZone\Top_Ten\Frontend\Media_Handler; |
| 16 |
use WebberZone\Top_Ten\Frontend\REST_API; |
| 17 |
|
| 18 |
if ( ! defined( 'WPINC' ) ) { |
| 19 |
exit; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Hook Loader class. |
| 24 |
* |
| 25 |
* Centralizes all hook registrations and their callback implementations. |
| 26 |
* |
| 27 |
* @since 4.0.0 |
| 28 |
*/ |
| 29 |
final class Hook_Loader { |
| 30 |
|
| 31 |
/** |
| 32 |
* Constructor. |
| 33 |
* |
| 34 |
* @since 4.0.0 |
| 35 |
*/ |
| 36 |
public function __construct() { |
| 37 |
$this->register_hooks(); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Register all plugin hooks. |
| 42 |
* |
| 43 |
* @since 4.0.0 |
| 44 |
*/ |
| 45 |
private function register_hooks(): void { |
| 46 |
$this->register_init_hooks(); |
| 47 |
$this->register_plugin_hooks(); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Register initialization hooks. |
| 52 |
* |
| 53 |
* @since 4.0.0 |
| 54 |
*/ |
| 55 |
private function register_init_hooks(): void { |
| 56 |
Hook_Registry::add_action( 'init', array( $this, 'initiate_plugin' ) ); |
| 57 |
Hook_Registry::add_action( 'widgets_init', array( $this, 'register_widgets' ) ); |
| 58 |
Hook_Registry::add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) ); |
| 59 |
Hook_Registry::add_action( 'parse_query', array( $this, 'parse_query' ) ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Register plugin management hooks. |
| 64 |
* |
| 65 |
* @since 4.0.0 |
| 66 |
*/ |
| 67 |
private function register_plugin_hooks(): void { |
| 68 |
Hook_Registry::add_action( 'activated_plugin', array( $this, 'activated_plugin' ), 10, 2 ); |
| 69 |
Hook_Registry::add_action( 'pre_current_active_plugins', array( $this, 'plugin_deactivated_notice' ) ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Initialise the plugin translations and media. |
| 74 |
* |
| 75 |
* @since 3.3.0 |
| 76 |
*/ |
| 77 |
public function initiate_plugin(): void { |
| 78 |
Frontend\Media_Handler::add_image_sizes(); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Initialise the Top 10 widgets. |
| 83 |
* |
| 84 |
* @since 3.3.0 |
| 85 |
*/ |
| 86 |
public function register_widgets(): void { |
| 87 |
register_widget( '\WebberZone\Top_Ten\Frontend\Widgets\Posts_Widget' ); |
| 88 |
register_widget( '\WebberZone\Top_Ten\Frontend\Widgets\Count_Widget' ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Function to register our new routes from the controller. |
| 93 |
* |
| 94 |
* @since 3.0.0 |
| 95 |
*/ |
| 96 |
public function register_rest_routes(): void { |
| 97 |
$controller = new Frontend\REST_API(); |
| 98 |
$controller->register_routes(); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Hook into WP_Query to check if tptn_query is set and is true. If so, we load the Top 10 query. |
| 103 |
* |
| 104 |
* @since 3.5.0 |
| 105 |
* |
| 106 |
* @param \WP_Query $query The WP_Query object. |
| 107 |
*/ |
| 108 |
public function parse_query( $query ): void { |
| 109 |
if ( true === $query->get( 'top_ten_query' ) ) { |
| 110 |
new Top_Ten_Core_Query( $query->query_vars ); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Checks if another version of Top 10/Top 10 Pro is active and deactivates it. |
| 116 |
* Hooked on `activated_plugin` so other plugin is deactivated when current plugin is activated. |
| 117 |
* |
| 118 |
* @since 3.5.0 |
| 119 |
* |
| 120 |
* @param string $plugin The plugin being activated. |
| 121 |
* @param bool $network_wide Whether the plugin is being activated network-wide. |
| 122 |
*/ |
| 123 |
public function activated_plugin( $plugin, $network_wide ): void { |
| 124 |
if ( ! in_array( $plugin, array( 'top-10/top-10.php', 'top-10-pro/top-10.php' ), true ) ) { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
Activator::activation_hook( $network_wide ); |
| 129 |
|
| 130 |
$plugin_to_deactivate = 'top-10/top-10.php'; |
| 131 |
$deactivated_notice_id = '1'; |
| 132 |
|
| 133 |
// If we just activated the free version, deactivate the pro version. |
| 134 |
if ( $plugin === $plugin_to_deactivate ) { |
| 135 |
$plugin_to_deactivate = 'top-10-pro/top-10.php'; |
| 136 |
$deactivated_notice_id = '2'; |
| 137 |
} |
| 138 |
|
| 139 |
if ( is_multisite() && is_network_admin() ) { |
| 140 |
$active_plugins = (array) get_site_option( 'active_sitewide_plugins', array() ); |
| 141 |
$active_plugins = array_keys( $active_plugins ); |
| 142 |
} else { |
| 143 |
$active_plugins = (array) get_option( 'active_plugins', array() ); |
| 144 |
} |
| 145 |
|
| 146 |
foreach ( $active_plugins as $plugin_basename ) { |
| 147 |
if ( $plugin_to_deactivate === $plugin_basename ) { |
| 148 |
set_transient( 'tptn_deactivated_notice_id', $deactivated_notice_id, 1 * HOUR_IN_SECONDS ); |
| 149 |
deactivate_plugins( $plugin_basename ); |
| 150 |
return; |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Displays a notice when either Top 10 or Top 10 Pro is automatically deactivated. |
| 157 |
* |
| 158 |
* @since 3.5.0 |
| 159 |
*/ |
| 160 |
public function plugin_deactivated_notice(): void { |
| 161 |
$deactivated_notice_id = (int) get_transient( 'tptn_deactivated_notice_id' ); |
| 162 |
if ( ! in_array( $deactivated_notice_id, array( 1, 2 ), true ) ) { |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
$message = __( "Top 10 and Top 10 Pro should not be active at the same time. We've automatically deactivated Top 10.", 'top-10' ); |
| 167 |
if ( 2 === $deactivated_notice_id ) { |
| 168 |
$message = __( "Top 10 and Top 10 Pro should not be active at the same time. We've automatically deactivated Top 10 Pro.", 'top-10' ); |
| 169 |
} |
| 170 |
|
| 171 |
?> |
| 172 |
<div class="updated" style="border-left: 4px solid #ffba00;"> |
| 173 |
<p><?php echo esc_html( $message ); ?></p> |
| 174 |
</div> |
| 175 |
<?php |
| 176 |
|
| 177 |
delete_transient( 'tptn_deactivated_notice_id' ); |
| 178 |
} |
| 179 |
} |
| 180 |
|