@@ -15,7 +15,7 @@ class RM_GIAPI {
1515 *
1616 * @var string
1717 */
18- public $ version = '1.1.16 ' ;
18+ public $ version = '1.1.17 ' ;
1919
2020 /**
2121 * Holds the admin menu hook suffix for the "dummy" dashboard.
@@ -87,6 +87,20 @@ class RM_GIAPI {
8787 */
8888 public $ rmapi = null ;
8989
90+ /**
91+ * Rank Math Instant Indexing module instance.
92+ *
93+ * @var object
94+ */
95+ public $ rm_module = null ;
96+
97+ /**
98+ * Holds the Google API client.
99+ *
100+ * @var object
101+ */
102+ public $ client = null ;
103+
90104 /**
91105 * URL of the Google plugin setup guide on rankmath.com.
92106 *
@@ -101,6 +115,11 @@ class RM_GIAPI {
101115 */
102116 public $ bing_guide_url = 'https://rankmath.com/blog/bing-indexing-api/?utm_source=Instant+Indexing+Plugin&utm_medium=Setup+Guide+Button&utm_campaign=WP ' ;
103117
118+ /**
119+ * Restrict to one request every X seconds to a given URL.
120+ */
121+ const THROTTLE_LIMIT = 5 ;
122+
104123 /**
105124 * Constructor method.
106125 */
@@ -127,7 +146,7 @@ public function __construct() {
127146
128147 if ( $ this ->is_rm_active && class_exists ( 'RankMath \\Instant_Indexing \\Api ' ) ) {
129148 $ this ->rm_module = new RankMath \Instant_Indexing \Instant_Indexing ();
130- $ this ->rmapi = RankMath \Instant_Indexing \Api::get ();
149+ $ this ->rmapi = RankMath \Instant_Indexing \Api::get ();
131150 add_action ( 'admin_init ' , [ $ this , 'remove_rm_admin_page ' ] );
132151 } else {
133152 unset( $ this ->nav_tabs ['bing_settings ' ] );
@@ -140,7 +159,7 @@ public function __construct() {
140159 $ this ->default_nav_tab = 'console ' ;
141160 }
142161
143- if ( $ this ->is_rm_active ) {
162+ if ( $ this ->is_rm_active && isset ( $ this -> nav_tabs [ ' bing_settings ' ] ) ) {
144163 $ this ->nav_tabs ['bing_settings ' ] = '<span class="dashicons dashicons-yes-alt"></span> ' . $ this ->nav_tabs ['bing_settings ' ];
145164 unset( $ this ->nav_tabs ['console ' ] );
146165 $ this ->nav_tabs = [ 'console ' => __ ( 'Console ' , 'fast-indexing-api ' ) ] + $ this ->nav_tabs ;
@@ -172,7 +191,7 @@ public function __construct() {
172191 add_filter ( 'bulk_actions-edit- ' . $ post_type , [ $ this , 'register_bulk_actions ' ] );
173192 add_filter ( 'handle_bulk_actions-edit- ' . $ post_type , [ $ this , 'bulk_action_handler ' ], 10 , 3 );
174193 }
175- add_action ( 'trashed_post ' , [ $ this , 'delete_post ' ], 10 , 1 );
194+ add_action ( 'wp_trash_post ' , [ $ this , 'delete_post ' ], 10 , 1 );
176195 }
177196
178197 if ( $ this ->is_rm_active ) {
@@ -315,7 +334,7 @@ public function send_to_api_link( $actions, $post ) {
315334 $ actions ['rmgiapi_update ' ] = '<a href=" ' . admin_url ( 'admin.php?page=instant-indexing&tab=console&apiaction=update&_wpnonce= ' . $ nonce . '&apiurl= ' . rawurlencode ( get_permalink ( $ post ) ) ) . '" class="rmgiapi-link rmgiapi_update"> ' . __ ( 'Instant Indexing: Google Update ' , 'fast-indexing-api ' ) . '</a> ' ;
316335 $ actions ['rmgiapi_getstatus ' ] = '<a href=" ' . admin_url ( 'admin.php?page=instant-indexing&tab=console&apiaction=getstatus&_wpnonce= ' . $ nonce . '&apiurl= ' . rawurlencode ( get_permalink ( $ post ) ) ) . '" class="rmgiapi-link rmgiapi_update"> ' . __ ( 'Instant Indexing: Google Get Status ' , 'fast-indexing-api ' ) . '</a> ' ;
317336 }
318- if ( in_array ( $ post ->post_type , $ bing_post_types , true ) ) {
337+ if ( in_array ( $ post ->post_type , $ bing_post_types , true ) && function_exists ( ' rank_math ' ) ) {
319338 $ actions ['rmgiapi_bing_submit ' ] = '<a href=" ' . admin_url ( 'admin.php?page=instant-indexing&tab=console&apiaction=bing_submit&_wpnonce= ' . $ nonce . '&apiurl= ' . rawurlencode ( get_permalink ( $ post ) ) ) . '" class="rmgiapi-link rmgiapi_update"> ' . __ ( 'Instant Indexing: Submit to IndexNow ' , 'fast-indexing-api ' ) . '</a> ' ;
320339 }
321340
@@ -357,13 +376,40 @@ public function ajax_rm_giapi() {
357376 *
358377 * @param array $url_input URLs.
359378 * @param string $action API action.
379+ * @param bool $is_manual Whether the URL is submitted manually by the user.
380+ *
360381 * @return array $data Result of the API call.
361382 */
362383 public function send_to_api ( $ url_input , $ action , $ is_manual = true ) {
363384 $ url_input = (array ) $ url_input ;
364385 $ urls_count = count ( $ url_input );
365386
366387 if ( strpos ( $ action , 'bing ' ) === false ) {
388+ /**
389+ * Filter the URL to be submitted to IndexNow.
390+ * Returning false will prevent the URL from being submitted.
391+ *
392+ * @param bool $is_manual Whether the URL is submitted manually by the user.
393+ */
394+ $ url_input = apply_filters ( 'rank_math/instant_indexing/submit_url ' , $ url_input , $ is_manual );
395+ if ( ! $ url_input ) {
396+ return false ;
397+ }
398+ $ url_input = array_unique ( $ url_input );
399+
400+ if ( count ( $ url_input ) > 100 ) {
401+ $ url_input = array_slice ( $ url_input , 0 , 100 );
402+ }
403+
404+ $ auto_submission_log = get_option ( 'giapi_auto_submissions ' , [] );
405+ if ( ! $ is_manual ) {
406+ // We keep the auto-submitted URLs in a log to prevent duplicates.
407+ $ logs = array_values ( array_reverse ( $ auto_submission_log ) );
408+ if ( ! empty ( $ logs [0 ] ) && $ logs [0 ]['url ' ] === $ url_input [0 ] && time () - $ logs [0 ]['time ' ] < self ::THROTTLE_LIMIT ) {
409+ return false ;
410+ }
411+ }
412+
367413 // This is NOT a Bing API request, so it's Google.
368414 include_once RM_GIAPI_PATH . 'vendor/autoload.php ' ;
369415 $ this ->client = new Google_Client ();
@@ -387,6 +433,21 @@ public function send_to_api( $url_input, $action, $is_manual = true ) {
387433 $ request_part = $ service ->urlNotifications ->publish ( $ post_body ); // phpcs:ignore
388434 }
389435 $ batch ->add ( $ request_part , 'url- ' . $ i );
436+
437+ // Log auto-submitted URLs.
438+ if ( ! $ is_manual ) {
439+ $ auto_submission_log [] = [
440+ 'url ' => $ url ,
441+ 'time ' => time (),
442+ ];
443+ }
444+ }
445+
446+ if ( ! $ is_manual ) {
447+ if ( count ( $ auto_submission_log ) > 100 ) {
448+ $ auto_submission_log = array_slice ( $ auto_submission_log , -100 , 100 , true );
449+ }
450+ update_option ( 'giapi_auto_submissions ' , $ auto_submission_log );
390451 }
391452
392453 $ results = $ batch ->execute ();
@@ -420,7 +481,7 @@ public function send_to_api( $url_input, $action, $is_manual = true ) {
420481
421482 if ( ! $ is_manual ) {
422483 $ logs = array_values ( array_reverse ( $ this ->rmapi ->get_log () ) );
423- if ( ! empty ( $ logs [0 ] ) && $ logs [0 ]['url ' ] === $ url_input [0 ] && time () - $ logs [0 ]['time ' ] < 15 ) {
484+ if ( ! empty ( $ logs [0 ] ) && $ logs [0 ]['url ' ] === $ url_input [0 ] && time () - $ logs [0 ]['time ' ] < self :: THROTTLE_LIMIT ) {
424485 return false ;
425486 }
426487 }
@@ -1084,6 +1145,11 @@ public function delete_post( $post_id ) {
10841145 return ;
10851146 }
10861147
1148+ // Only submit delete action if post was published.
1149+ if ( $ post ->post_status !== 'publish ' ) {
1150+ return ;
1151+ }
1152+
10871153 $ send_url = apply_filters ( 'rank_math/indexing_api/delete_url ' , get_permalink ( $ post ), $ post );
10881154 // Early exit if filter is set to false.
10891155 if ( ! $ send_url ) {
0 commit comments