@@ -79,15 +79,10 @@ public function request(): bool
7979	 */ 
8080	public  static  function  send (array  $ urlsbool 
8181	{
82- 		if  (!Seo::option ('indexnow.enabled ' )) {
82+ 		if  (!Seo::option ('indexnow.enabled ' ) ||  empty ( $ urls ) ) {
8383			return  false ;
8484		}
8585
86- 		if  (empty ($ urls
87- 			return  false ;
88- 		}
89- 
90- 		$ keystatic ::key ();
9186		$ firstUrl$ urls0 ];
9287		$ parsedUrlparse_url ($ firstUrl
9388		$ host$ parsedUrl'host ' ];
@@ -103,46 +98,48 @@ public static function send(array $urls): bool
10398		$ basePath'' ;
10499		if  ($ path$ path'/ ' ) {
105100			// find the base path by comparing with site url 
106- 			$ siteUrlsite ()->url ();
101+ 			$ siteUrl$ this -> page -> site ()->url ();
107102			$ siteParsedparse_url ($ siteUrl
108103			$ basePath$ siteParsed'path ' ] ?? '' ;
109104		}
110105
111106		$ searchEngineoption ('indexnow.searchEngine ' );
112- 
113- 		// ensure search engine URL is properly formatted 
114107		$ searchEnginertrim ($ searchEngine'/ ' );
115108		if  (!str_contains ($ searchEngine'/indexnow ' )) {
116109			$ searchEngine'/indexnow ' ;
117110		}
118111
119- 		// filter urls to only include those from the same domain 
120- 		$ domainUrlsarray_filter ($ urlsfunction  ($ urluse  ($ host
121- 			return  parse_url ($ urlPHP_URL_HOST ) === $ host
122- 		});
123- 
124- 		// prepare request data 
125- 		$ data
126- 			'host '  => $ host
127- 			'key '  => $ key
128- 			'keyLocation '  => "{$ scheme:// {$ host$ basePath/indexnow- {$ key.txt " ,
129- 			'urlList '  => array_values (array_unique ($ domainUrls
130- 		];
131- 
132- 		try  {
133- 			$ responsepost ($ searchEngine
134- 				'headers '  => [
135- 					'Content-Type '  => 'application/json; charset=utf-8 ' ,
136- 					'User-Agent '  => Seo::userAgent ()
137- 				],
138- 				'data '  => json_encode ($ data
139- 			]);
140- 
141- 			return  $ responsecode () === 200  || $ responsecode () === 202 ;
142- 		} catch  (\Exception   $ e
143- 			// log error if needed 
144- 			return  false ;
112+ 		$ domainUrlsarray_filter ($ urlsfn  ($ urlparse_url ($ urlPHP_URL_HOST ) === $ host
113+ 
114+ 		// split into batches of 10,000 (IndexNow limit) 
115+ 		$ batchesarray_chunk (array_values (array_unique ($ domainUrls10000 );
116+ 		$ allSuccessfultrue ;
117+ 		$ keystatic ::key ();
118+ 
119+ 		foreach  ($ batchesas  $ batch
120+ 			try  {
121+ 				$ responsepost ($ searchEngine
122+ 					'headers '  => [
123+ 						'Content-Type '  => 'application/json; charset=utf-8 ' ,
124+ 						'User-Agent '  => Seo::userAgent ()
125+ 					],
126+ 					'data '  => json_encode ([
127+ 						'host '  => $ host
128+ 						'key '  => $ key
129+ 						'keyLocation '  => "{$ scheme:// {$ host$ basePath/indexnow- {$ key.txt " ,
130+ 						'urlList '  => $ batch
131+ 					])
132+ 				]);
133+ 
134+ 				if  ($ responsecode () > 299 ) {
135+ 					$ allSuccessfulfalse ;
136+ 				}
137+ 			} catch  (\Exception   $ e
138+ 				$ allSuccessfulfalse ;
139+ 			}
145140		}
141+ 
142+ 		return  $ allSuccessful
146143	}
147144
148145	/** 
@@ -296,12 +293,12 @@ protected function collectByTemplates(array $templates): void
296293	{
297294		$ languageinstance ()->language ();
298295
299- 		foreach  ( $ templates   as   $ template ) { 
300- 			foreach  ( site ()-> index ()  as   $ page ) { 
301- 				 if  ($ page-> intendedTemplate ()-> name () ===  $ template  &&  $ this isIndexable ($ page { 
302- 					 $ this -> urls [] =  $ page -> url ( $ language ?->code()); 
303- 				} 
304- 			} 
296+ 		$ pages  =  $ this -> page -> site ()-> index () 
297+ 			-> filterBy ( ' intendedTemplate ' ,  ' in ' ,  $ templates ) 
298+ 			-> filter ( fn  ($ page) =>  $ this isIndexable ($ page; 
299+ 
300+ 		foreach  ( $ pages   as   $ page ) { 
301+ 			$ this -> urls [] =  $ page -> url ( $ language ?->code()); 
305302		}
306303	}
307304
0 commit comments