PluginProbe
WebberZone Top 10 — Popular Posts / 4.4.3
WebberZone Top 10 — Popular Posts v4.4.3
4.5.1 4.5.0 4.4.3 4.4.2 4.4.1 4.4.0 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 trunk 1.0 1.0.1 1.1 1.2 1.3 1.4 1.4.1 1.5 1.5.1 1.5.2 1.5.3 1.6 1.6.1 All 117 releases
top-10 / includes / class-hook-loader.php

class-hook-loader.php in WebberZone Top 10 — Popular Posts 4.4.3, at includes/class-hook-loader.php

184 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 if ( ! Feature_Manager::is_enabled( 'legacy_widgets' ) ) {
88 return;
89 }
90
91 register_widget( '\WebberZone\Top_Ten\Frontend\Widgets\Posts_Widget' );
92 register_widget( '\WebberZone\Top_Ten\Frontend\Widgets\Count_Widget' );
93 }
94
95 /**
96 * Function to register our new routes from the controller.
97 *
98 * @since 3.0.0
99 */
100 public function register_rest_routes(): void {
101 $controller = new Frontend\REST_API();
102 $controller->register_routes();
103 }
104
105 /**
106 * Hook into WP_Query to check if tptn_query is set and is true. If so, we load the Top 10 query.
107 *
108 * @since 3.5.0
109 *
110 * @param \WP_Query $query The WP_Query object.
111 */
112 public function parse_query( $query ): void {
113 if ( true === $query->get( 'top_ten_query' ) ) {
114 new Top_Ten_Core_Query( $query->query_vars );
115 }
116 }
117
118 /**
119 * Checks if another version of Top 10/Top 10 Pro is active and deactivates it.
120 * Hooked on `activated_plugin` so other plugin is deactivated when current plugin is activated.
121 *
122 * @since 3.5.0
123 *
124 * @param string $plugin The plugin being activated.
125 * @param bool $network_wide Whether the plugin is being activated network-wide.
126 */
127 public function activated_plugin( $plugin, $network_wide ): void {
128 if ( ! in_array( $plugin, array( 'top-10/top-10.php', 'top-10-pro/top-10.php' ), true ) ) {
129 return;
130 }
131
132 Activator::activation_hook( $network_wide );
133
134 $plugin_to_deactivate = 'top-10/top-10.php';
135 $deactivated_notice_id = '1';
136
137 // If we just activated the free version, deactivate the pro version.
138 if ( $plugin === $plugin_to_deactivate ) {
139 $plugin_to_deactivate = 'top-10-pro/top-10.php';
140 $deactivated_notice_id = '2';
141 }
142
143 if ( is_multisite() && is_network_admin() ) {
144 $active_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
145 $active_plugins = array_keys( $active_plugins );
146 } else {
147 $active_plugins = (array) get_option( 'active_plugins', array() );
148 }
149
150 foreach ( $active_plugins as $plugin_basename ) {
151 if ( $plugin_to_deactivate === $plugin_basename ) {
152 set_transient( 'tptn_deactivated_notice_id', $deactivated_notice_id, 1 * HOUR_IN_SECONDS );
153 deactivate_plugins( $plugin_basename );
154 return;
155 }
156 }
157 }
158
159 /**
160 * Displays a notice when either Top 10 or Top 10 Pro is automatically deactivated.
161 *
162 * @since 3.5.0
163 */
164 public function plugin_deactivated_notice(): void {
165 $deactivated_notice_id = (int) get_transient( 'tptn_deactivated_notice_id' );
166 if ( ! in_array( $deactivated_notice_id, array( 1, 2 ), true ) ) {
167 return;
168 }
169
170 $message = __( "Top 10 and Top 10 Pro should not be active at the same time. We've automatically deactivated Top 10.", 'top-10' );
171 if ( 2 === $deactivated_notice_id ) {
172 $message = __( "Top 10 and Top 10 Pro should not be active at the same time. We've automatically deactivated Top 10 Pro.", 'top-10' );
173 }
174
175 ?>
176 <div class="updated" style="border-left: 4px solid #ffba00;">
177 <p><?php echo esc_html( $message ); ?></p>
178 </div>
179 <?php
180
181 delete_transient( 'tptn_deactivated_notice_id' );
182 }
183 }
184