PluginProbe
ReCrawler / 0.2.0
ReCrawler v0.2.0
1.1.0 1.0.2 1.0.1 1.0.0 0.3.4 0.3.3 0.1.1 0.1.2 0.1.2.1 0.1.2.2 0.1.3 0.1.4 0.1.5 0.2.0 0.3.0 0.3.1 0.3.2 trunk 0.1.0 0.1.0.1
recrawler / src / Main.php

Main.php in ReCrawler 0.2.0, at src/Main.php

372 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Main class.
4 *
5 * @package Mihdan\ReCrawler
6 */
7
8 namespace Mihdan\ReCrawler;
9
10 use Mihdan\ReCrawler\Logger\Logger;
11 use Mihdan\ReCrawler\Migrations\Migrations;
12 use Mihdan\ReCrawler\Providers\Bing\BingIndexNow;
13 use Mihdan\ReCrawler\Providers\Bing\BingWebmaster;
14 use Mihdan\ReCrawler\Providers\Google\GoogleWebmaster;
15 use Mihdan\ReCrawler\Providers\IndexNow\IndexNow;
16 use Mihdan\ReCrawler\Providers\Seznam\SeznamIndexNow;
17 use Mihdan\ReCrawler\Providers\Naver\NaverIndexNow;
18 use Mihdan\ReCrawler\Providers\BlockAiCrawler;
19 use Mihdan\ReCrawler\Providers\Yandex\YandexIndexNow;
20 use Mihdan\ReCrawler\Providers\Yandex\YandexWebmaster;
21 use Mihdan\ReCrawler\Views\HelpTab;
22 use Mihdan\ReCrawler\Views\Log_List_Table;
23 use Mihdan\ReCrawler\Views\Settings;
24 use Mihdan\ReCrawler\Views\WPOSA;
25 use WP_Post;
26 use WP_List_Table;
27 use WP_Site;
28 use WP_Query;
29
30 /**
31 * Class Main.
32 */
33 class Main {
34 /**
35 * DIC container.
36 *
37 * @var Container $container
38 */
39 private $container;
40
41 /**
42 * Settings instance.
43 *
44 * @var WPOSA $wposa
45 */
46 private $wposa;
47
48 /**
49 * Logger instance.
50 *
51 * @var Logger
52 */
53 private $logger;
54
55 /**
56 * Constructor.
57 *
58 * @param Container $container Container instnace.
59 */
60 public function __construct( Container $container ) {
61 $this->container = $container;
62 }
63
64 public function init() {
65 $this->load_requirements();
66 $this->setup_hooks();
67
68 do_action( 'recrawler/init', $this );
69 }
70
71 /**
72 * @throws NotFoundExceptionInterface
73 * @throws ContainerExceptionInterface
74 */
75 private function load_requirements() {
76
77 if ( ! function_exists( 'dbDelta' ) ) {
78 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
79 }
80
81 $this->logger = $this->container->make( Logger::class );
82
83 $this->container->set(
84 WPOSA::class,
85 function( Container $c ) {
86 return new WPOSA(
87 Utils::get_plugin_name(),
88 Utils::get_plugin_version(),
89 Utils::get_plugin_slug(),
90 Utils::get_plugin_prefix()
91 );
92 }
93 );
94
95 $this->wposa = $this->container->get( WPOSA::class );
96 $this->wposa->setup_hooks();
97
98 ( $this->container->make( BlockAiCrawler::class ) )->setup_hooks();
99 ( $this->container->make( Hooks::class ) )->setup_hooks();
100
101 ( $this->container->make( HelpTab::class ) )->setup_hooks();
102 ( $this->container->make( Settings::class ) )->setup_hooks();
103 ( $this->container->make( Cron::class ) )->setup_hooks();
104 ( $this->container->make( YandexIndexNow::class ) )->setup_hooks();
105 ( $this->container->make( BingIndexNow::class ) )->setup_hooks();
106 ( $this->container->make( SeznamIndexNow::class ) )->setup_hooks();
107 ( $this->container->make( NaverIndexNow::class ) )->setup_hooks();
108 ( $this->container->make( IndexNow::class ) )->setup_hooks();
109
110 ( $this->container->make( YandexWebmaster::class ) )->setup_hooks();
111 ( $this->container->make( BingWebmaster::class ) )->setup_hooks();
112 ( $this->container->make( GoogleWebmaster::class ) )->setup_hooks();
113 ( $this->container->make( Migrations::class ) )->setup_hooks();
114 }
115
116 /**
117 * Setup hooks.
118 */
119 public function setup_hooks(): void {
120 add_filter( 'plugin_action_links', [ $this, 'add_settings_link' ], 10, 2 );
121 add_action( 'admin_menu', [ $this, 'add_log_menu_page' ] );
122 add_filter( 'set_screen_option_logs_per_page', [ $this, 'set_screen_option' ], 10, 3 );
123 add_action( 'admin_init', [ $this, 'maybe_upgrade' ] );
124
125 //add_filter( 'post_row_actions', [ $this, 'post_row_actions' ], 10, 2 );
126 //add_filter( 'page_row_actions', [ $this, 'post_row_actions' ], 10, 2 );
127
128 // Add last update column.
129 if ( $this->wposa->get_option( 'show_last_update_column', 'general', 'on' ) === 'on' ) {
130 foreach ( (array) $this->wposa->get_option( 'post_types', 'general', [] ) as $post_type ) {
131 add_filter( "manage_{$post_type}_posts_columns", [ $this, 'add_last_update_column' ] );
132 add_filter( "manage_edit-{$post_type}_sortable_columns", [ $this, 'add_sorting_by_last_update_column' ] );
133
134 add_action( "manage_{$post_type}_posts_custom_column", [ $this, 'add_last_update_column_content' ], 10, 2 );
135 add_action( 'pre_get_posts', [ $this, 'do_sorting_by_last_update_column' ] );
136 }
137 }
138
139 register_activation_hook( RECRAWLER_FILE, [ $this, 'activate_plugin' ] );
140
141 // Multisite.
142 add_action( 'wp_delete_site', [ $this, 'delete_site_tables' ] );
143 add_action( 'wp_insert_site', [ $this, 'add_site_tables' ] );
144 }
145
146 /**
147 * Delete site tables when deleting a site.
148 *
149 * @param WP_Site $old_site Site ID.
150 * @return void
151 */
152 public function delete_site_tables( WP_Site $old_site ): void {
153 switch_to_blog( $old_site->id );
154 $this->drop_tables();
155 restore_current_blog();
156 }
157
158 /**
159 * Add site tables when creating a site.
160 *
161 * @param WP_Site $new_site Site ID.
162 * @return void
163 */
164 public function add_site_tables( WP_Site $new_site ): void {
165 switch_to_blog( $new_site->id );
166 $this->create_tables();
167 restore_current_blog();
168 }
169
170 public function add_last_update_column( array $columns ): array {
171 $columns['recrawler'] = sprintf(
172 '<span class="dashicons dashicons-share" title="%s"></span>',
173 __( 'ReCrawler: Last Update', 'recrawler' )
174 );
175
176 return $columns;
177 }
178
179 public function add_last_update_column_content( string $column_name, int $post_id ): void {
180 if ( $column_name !== 'recrawler' ) {
181 return;
182 }
183
184 $last_update = (int) get_post_meta( $post_id, Utils::get_plugin_prefix() . '_last_update', true );
185
186 if ( $last_update === 0 ) {
187 return;
188 }
189
190 echo esc_html( date( 'd.m.Y H:i', $last_update ) );
191 }
192
193 public function add_sorting_by_last_update_column( array $columns ): array {
194 $columns['recrawler'] = 'recrawler';
195
196 return $columns;
197 }
198
199 public function do_sorting_by_last_update_column( WP_Query $query ) {
200 if ( ! is_admin() ) {
201 return;
202 }
203
204 if ( $query->get( 'orderby' ) === 'recrawler' ) {
205 $query->set( 'meta_key', Utils::get_plugin_prefix() . '_last_update' );
206 $query->set( 'orderby', 'meta_value_num' );
207 }
208 }
209
210
211 public function post_row_actions( array $actions, WP_Post $post ): array {
212 if ( ! in_array( $post->post_type, (array) $this->wposa->get_option( 'post_types', 'general', [] ), true ) ) {
213 return $actions;
214 }
215
216 if ( ! is_post_publicly_viewable( $post ) ) {
217 return $actions;
218 }
219
220 $actions['recrawler'] = sprintf(
221 '<a title="%s" href="%s">ReCrawler</a>',
222 esc_attr( __( 'Notify the search engine', 'recrawler' ) ),
223 1
224 );
225
226 return $actions;
227 }
228
229 /**
230 * Set screen option.
231 *
232 * @param string $status Status.
233 * @param string $option Option name.
234 * @param string $value Option value.
235 *
236 * @return int
237 */
238 public function set_screen_option( $status, $option, $value ): int {
239 return (int) $value;
240 }
241
242 /**
243 * Fired on plugin activate.
244 */
245 public function activate_plugin( $network_wide ) {
246 global $wpdb;
247
248 if ( is_multisite() && $network_wide ) {
249 $sites = get_sites( [ 'fields' => 'ids' ] );
250 foreach ( $sites as $site_id ) {
251 switch_to_blog( $site_id );
252 $this->create_tables();
253 restore_current_blog();
254 }
255 } else {
256 $this->create_tables();
257 }
258 }
259
260 private function drop_tables() {
261 global $wpdb;
262
263 $sql = "DROP TABLE IF EXISTS {$wpdb->prefix}recrawler_log";
264 $wpdb->query( $sql );
265 }
266
267 private function create_tables( bool $upgrade = false ) {
268 global $wpdb;
269
270 $table_name = $wpdb->prefix . 'recrawler_log';
271 $charset_collate = $wpdb->get_charset_collate();
272
273 if ( $upgrade || $wpdb->get_var( "SHOW TABLES LIKE '{$table_name}'" ) !== $table_name ) {
274 $sql = "CREATE TABLE {$wpdb->prefix}recrawler_log (
275 log_id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
276 created_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
277 level enum('emergency','alert','critical','error','warning','notice','info','debug') NOT NULL DEFAULT 'debug',
278 search_engine enum('index-now','yandex-index-now','yandex-webmaster','bing-index-now','bing-webmaster','site','google-webmaster','seznam-index-now','naver-index-now') NOT NULL DEFAULT 'site',
279 direction enum('incoming','outgoing','internal') NOT NULL DEFAULT 'incoming',
280 status_code INT(11) unsigned NOT NULL,
281 message text NOT NULL,
282 PRIMARY KEY (log_id)
283 ) {$charset_collate};";
284
285 dbDelta($sql);
286
287 Utils::set_db_version( Utils::get_plugin_version() );
288 }
289 }
290
291 public function maybe_upgrade() {
292 $db_version = Utils::get_db_version();
293 $plugin_version = Utils::get_plugin_version();
294
295 if ( version_compare( $db_version, $plugin_version, '<' ) ) {
296 $this->create_tables( true );
297 }
298 }
299
300 /**
301 * Add log menu page for dashboard.
302 */
303 public function add_log_menu_page() {
304
305 if ( ! $this->is_logging_enabled() ) {
306 return;
307 }
308
309 $hook = add_submenu_page(
310 RECRAWLER_SLUG,
311 __( 'Log', 'recrawler' ),
312 __( 'Log', 'recrawler' ),
313 'manage_options',
314 RECRAWLER_SLUG . '-log',
315 [ $this, 'render_log_page' ]
316 );
317
318 add_action(
319 "load-$hook",
320 function () {
321 $GLOBALS[ RECRAWLER_PREFIX . '_log' ] = $this->container->make( Log_List_Table::class );
322 }
323 );
324 }
325
326 /**
327 * Render log menu page for dashboard.
328 */
329 public function render_log_page() {
330 ?>
331 <div class="wrap">
332 <h2><?php echo esc_html( get_admin_page_title() ); ?></h2>
333 <form action="" method="post">
334 <?php
335 /**
336 * WP_List_table.
337 *
338 * @var WP_List_Table $table
339 */
340 $table = $GLOBALS[ RECRAWLER_PREFIX . '_log' ];
341 $table->display();
342 ?>
343 </form>
344 </div>
345 <?php
346 }
347
348 /**
349 * Add plugin action links
350 *
351 * @param array $actions Default actions.
352 * @param string $plugin_file Plugin file.
353 *
354 * @return array
355 */
356 public function add_settings_link( $actions, $plugin_file ) {
357 if ( Utils::get_plugin_basename() === $plugin_file ) {
358 $actions[] = sprintf(
359 '<a href="%s">%s</a>',
360 admin_url( 'admin.php?page=' . Utils::get_plugin_slug() ),
361 esc_html__( 'Settings', 'recrawler' )
362 );
363 }
364
365 return $actions;
366 }
367
368 private function is_logging_enabled(): bool {
369 return $this->wposa->get_option( 'enable', 'logs', 'on' ) === 'on';
370 }
371 }
372