PluginProbe
WebberZone Top 10 — Popular Posts / 4.5.2
WebberZone Top 10 — Popular Posts v4.5.2
4.5.2 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 All 118 releases
top-10 / includes / class-hook-loader.php

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

190 lines 5.0 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 if ( Feature_Manager::is_enabled( 'rest_api' ) ) {
59 Hook_Registry::add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );
60 }
61 Hook_Registry::add_action( 'parse_query', array( $this, 'parse_query' ) );
62 }
63
64 /**
65 * Register plugin management hooks.
66 *
67 * @since 4.0.0
68 */
69 private function register_plugin_hooks(): void {
70 Hook_Registry::add_action( 'activated_plugin', array( $this, 'activated_plugin' ), 10, 2 );
71 Hook_Registry::add_action( 'pre_current_active_plugins', array( $this, 'plugin_deactivated_notice' ) );
72 }
73
74 /**
75 * Initialise the plugin translations and media.
76 *
77 * @since 3.3.0
78 */
79 public function initiate_plugin(): void {
80 Frontend\Media_Handler::add_image_sizes();
81 }
82
83 /**
84 * Initialise the Top 10 widgets.
85 *
86 * @since 3.3.0
87 */
88 public function register_widgets(): void {
89 if ( ! Feature_Manager::is_enabled( 'legacy_widgets' ) ) {
90 return;
91 }
92
93 register_widget( '\WebberZone\Top_Ten\Frontend\Widgets\Posts_Widget' );
94 register_widget( '\WebberZone\Top_Ten\Frontend\Widgets\Count_Widget' );
95 }
96
97 /**
98 * Function to register our new routes from the controller.
99 *
100 * @since 3.0.0
101 */
102 public function register_rest_routes(): void {
103 if ( ! Feature_Manager::is_enabled( 'rest_api' ) ) {
104 return;
105 }
106
107 $controller = new Frontend\REST_API();
108 $controller->register_routes();
109 }
110
111 /**
112 * Hook into WP_Query to check if tptn_query is set and is true. If so, we load the Top 10 query.
113 *
114 * @since 3.5.0
115 *
116 * @param \WP_Query $query The WP_Query object.
117 */
118 public function parse_query( $query ): void {
119 if ( true === $query->get( 'top_ten_query' ) ) {
120 new Top_Ten_Core_Query( $query->query_vars );
121 }
122 }
123
124 /**
125 * Checks if another version of Top 10/Top 10 Pro is active and deactivates it.
126 * Hooked on `activated_plugin` so other plugin is deactivated when current plugin is activated.
127 *
128 * @since 3.5.0
129 *
130 * @param string $plugin The plugin being activated.
131 * @param bool $network_wide Whether the plugin is being activated network-wide.
132 */
133 public function activated_plugin( $plugin, $network_wide ): void {
134 if ( ! in_array( $plugin, array( 'top-10/top-10.php', 'top-10-pro/top-10.php' ), true ) ) {
135 return;
136 }
137
138 Activator::activation_hook( $network_wide );
139
140 $plugin_to_deactivate = 'top-10/top-10.php';
141 $deactivated_notice_id = '1';
142
143 // If we just activated the free version, deactivate the pro version.
144 if ( $plugin === $plugin_to_deactivate ) {
145 $plugin_to_deactivate = 'top-10-pro/top-10.php';
146 $deactivated_notice_id = '2';
147 }
148
149 if ( is_multisite() && is_network_admin() ) {
150 $active_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
151 $active_plugins = array_keys( $active_plugins );
152 } else {
153 $active_plugins = (array) get_option( 'active_plugins', array() );
154 }
155
156 foreach ( $active_plugins as $plugin_basename ) {
157 if ( $plugin_to_deactivate === $plugin_basename ) {
158 set_transient( 'tptn_deactivated_notice_id', $deactivated_notice_id, 1 * HOUR_IN_SECONDS );
159 deactivate_plugins( $plugin_basename );
160 return;
161 }
162 }
163 }
164
165 /**
166 * Displays a notice when either Top 10 or Top 10 Pro is automatically deactivated.
167 *
168 * @since 3.5.0
169 */
170 public function plugin_deactivated_notice(): void {
171 $deactivated_notice_id = (int) get_transient( 'tptn_deactivated_notice_id' );
172 if ( ! in_array( $deactivated_notice_id, array( 1, 2 ), true ) ) {
173 return;
174 }
175
176 $message = __( "Top 10 and Top 10 Pro should not be active at the same time. We've automatically deactivated Top 10.", 'top-10' );
177 if ( 2 === $deactivated_notice_id ) {
178 $message = __( "Top 10 and Top 10 Pro should not be active at the same time. We've automatically deactivated Top 10 Pro.", 'top-10' );
179 }
180
181 ?>
182 <div class="updated" style="border-left: 4px solid #ffba00;">
183 <p><?php echo esc_html( $message ); ?></p>
184 </div>
185 <?php
186
187 delete_transient( 'tptn_deactivated_notice_id' );
188 }
189 }
190