| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Hooks. |
| 4 |
* |
| 5 |
* @package Mihdan\ReCrawler |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Mihdan\ReCrawler; |
| 9 |
|
| 10 |
use Mihdan\ReCrawler\Views\WPOSA; |
| 11 |
use WP_Post; |
| 12 |
use WP_Comment; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class Hooks. |
| 16 |
*/ |
| 17 |
class Hooks { |
| 18 |
/** |
| 19 |
* WPOSA Instance. |
| 20 |
* |
| 21 |
* @var WPOSA |
| 22 |
*/ |
| 23 |
private WPOSA $wposa; |
| 24 |
|
| 25 |
/** |
| 26 |
* Ping delay in seconds. |
| 27 |
* |
| 28 |
* @var int $ping_delay |
| 29 |
*/ |
| 30 |
private int $ping_delay; |
| 31 |
|
| 32 |
/** |
| 33 |
* Hooks constructor. |
| 34 |
* |
| 35 |
* @param WPOSA $wposa WPOSA instance. |
| 36 |
*/ |
| 37 |
public function __construct( WPOSA $wposa ) { |
| 38 |
$this->wposa = $wposa; |
| 39 |
$this->ping_delay = (int) $this->wposa->get_option( 'ping_delay', 'general', 60 ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Hooks init. |
| 44 |
* |
| 45 |
* @return void |
| 46 |
*/ |
| 47 |
public function setup_hooks() { |
| 48 |
add_action( 'transition_post_status', [ $this, 'post_updated' ], 10, 3 ); |
| 49 |
add_action( 'transition_comment_status', [ $this, 'comment_updated' ], 10, 3 ); |
| 50 |
add_action( 'wp_insert_comment', [ $this, 'comment_inserted' ], 10, 2 ); |
| 51 |
add_action( 'saved_term', [ $this, 'term_updated' ], 10, 3 ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Fires immediately after a comment is inserted into the database. |
| 56 |
* |
| 57 |
* @param int $id Идентификатор комментария. |
| 58 |
* @param WP_Comment $comment Объект комментария. |
| 59 |
* |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
public function comment_inserted( int $id, WP_Comment $comment ): void { |
| 63 |
|
| 64 |
// Comment must be manually approved. |
| 65 |
if ( (int) $comment->comment_approved !== 1 ) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
// Delay. |
| 70 |
$last_update = (int) get_comment_meta( |
| 71 |
$id, |
| 72 |
Utils::get_plugin_prefix() . '_last_update', |
| 73 |
true |
| 74 |
); |
| 75 |
|
| 76 |
if ( ( current_time( 'timestamp' ) - $last_update ) < $this->ping_delay ) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
do_action( 'recrawler/comment_updated', $comment->comment_post_ID, $comment ); |
| 81 |
|
| 82 |
update_comment_meta( |
| 83 |
$id, |
| 84 |
Utils::get_plugin_prefix() . '_last_update', |
| 85 |
current_time( 'timestamp' ) |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Fires actions related to the transitioning of a post's status. |
| 91 |
* |
| 92 |
* @param string $new_status New post status. |
| 93 |
* @param string $old_status Old post status. |
| 94 |
* @param WP_Post $post Post data. |
| 95 |
* |
| 96 |
* @link https://yandex.ru/dev/webmaster/doc/dg/reference/host-recrawl-post.html |
| 97 |
*/ |
| 98 |
public function post_updated( string $new_status, string $old_status, WP_Post $post ): void { |
| 99 |
|
| 100 |
if ( $new_status !== 'publish' ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
if ( ! empty( $_REQUEST['meta-box-loader'] ) ) { // phpcs:ignore |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
if ( wp_is_post_revision( $post ) || wp_is_post_autosave( $post ) ) { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
if ( function_exists( 'is_post_publicly_viewable' ) && ! is_post_publicly_viewable( $post ) ) { |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
if ( ! in_array( $post->post_type, (array) $this->wposa->get_option( 'post_types', 'general', [] ), true ) ) { |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
// Disable for Bulk Edit screen. |
| 121 |
if ( isset( $_REQUEST['bulk_edit'] ) && $this->wposa->get_option( 'disable_for_bulk_edit', 'general', 'on' ) === 'on' ) { |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
// Delay. |
| 126 |
$last_update = (int) get_post_meta( |
| 127 |
$post->ID, |
| 128 |
Utils::get_plugin_prefix() . '_last_update', |
| 129 |
true |
| 130 |
); |
| 131 |
|
| 132 |
if ( ( current_time( 'timestamp' ) - $last_update ) < $this->ping_delay ) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
if ( $old_status === $new_status ) { |
| 137 |
// Post updated. |
| 138 |
if ( $this->wposa->get_option( 'ping_on_post_updated', 'general', 'on' ) === 'on' ) { |
| 139 |
do_action( 'recrawler/post_updated', $post->ID, $post ); |
| 140 |
} |
| 141 |
} else { |
| 142 |
// Post added. |
| 143 |
if ( $this->wposa->get_option( 'ping_on_post', 'general', 'on' ) === 'on' ) { |
| 144 |
do_action( 'recrawler/post_added', $post->ID, $post ); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
update_post_meta( |
| 149 |
$post->ID, |
| 150 |
Utils::get_plugin_prefix() . '_last_update', |
| 151 |
current_time( 'timestamp' ) |
| 152 |
); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Fires when the comment status is in transition |
| 157 |
* from one specific status to another. |
| 158 |
* |
| 159 |
* @param int|string $new_status The new comment status. |
| 160 |
* @param int|string $old_status The old comment status. |
| 161 |
* @param WP_Comment $comment Comment object. |
| 162 |
* |
| 163 |
* @return void |
| 164 |
*/ |
| 165 |
public function comment_updated( $new_status, $old_status, WP_Comment $comment ): void { |
| 166 |
|
| 167 |
// Delay. |
| 168 |
$last_update = (int) get_comment_meta( |
| 169 |
$comment->comment_ID, |
| 170 |
Utils::get_plugin_prefix() . '_last_update', |
| 171 |
true |
| 172 |
); |
| 173 |
|
| 174 |
if ( ( current_time( 'timestamp' ) - $last_update ) < $this->ping_delay ) { |
| 175 |
return; |
| 176 |
} |
| 177 |
|
| 178 |
if ( $new_status !== 'approved' ) { |
| 179 |
return; |
| 180 |
} |
| 181 |
|
| 182 |
do_action( 'recrawler/comment_updated', $comment->comment_post_ID, $comment ); |
| 183 |
|
| 184 |
update_comment_meta( |
| 185 |
$comment->comment_ID, |
| 186 |
Utils::get_plugin_prefix() . '_last_update', |
| 187 |
current_time( 'timestamp' ) |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Fires after a term has been saved, and the term cache has been cleared. |
| 193 |
* |
| 194 |
* @param int $term_id Term ID. |
| 195 |
* @param int $tt_id Term taxonomy ID. |
| 196 |
* @param string $taxonomy Taxonomy slug. |
| 197 |
*/ |
| 198 |
public function term_updated( int $term_id, int $tt_id, string $taxonomy ): void { |
| 199 |
|
| 200 |
// Delay. |
| 201 |
$last_update = (int) get_term_meta( |
| 202 |
$term_id, |
| 203 |
Utils::get_plugin_prefix() . '_last_update', |
| 204 |
true |
| 205 |
); |
| 206 |
|
| 207 |
if ( ( current_time( 'timestamp' ) - $last_update ) < $this->ping_delay ) { |
| 208 |
return; |
| 209 |
} |
| 210 |
|
| 211 |
do_action( 'recrawler/term_updated', $term_id, $taxonomy ); |
| 212 |
|
| 213 |
update_term_meta( |
| 214 |
$term_id, |
| 215 |
Utils::get_plugin_prefix() . '_last_update', |
| 216 |
current_time( 'timestamp' ) |
| 217 |
); |
| 218 |
} |
| 219 |
} |
| 220 |
|