@@ -379,15 +379,17 @@ function ( int $link_id ): ?Link {
379379 *
380380 * @since 1.2.0
381381 *
382- * @param integer $limit The limit of links to return.
383- * @param integer $page The page of links to return.
384- * @param array $status The status of the links to return.
385- * @param array $link_ids The link ids to query.
386- * @param array $archive_status The archive status of the links to return.
387- * @param string $order_by The order by.
388- * @param string|NULL $search_term The search term to query.
389- * @param string|NULL $date The date of the links to return (yy-mm).
390- * @param boolean|NULL $excluded Whether to return excluded links.
382+ * @param integer $limit The limit of links to return.
383+ * @param integer $page The page of links to return.
384+ * @param array $status The status of the links to return.
385+ * @param array $link_ids The link ids to query.
386+ * @param array $archive_status The archive status of the links to return.
387+ * @param string $order_by The order by.
388+ * @param string|NULL $search_term The search term to query.
389+ * @param string|NULL $date The date of the links to return (yy-mm).
390+ * @param boolean|NULL $excluded Whether to return excluded links.
391+ * @param string[]|NULL $snapshot_process The snapshot status of the links to return.
392+ * @param boolean|NULL $has_checks Whether to return links with or without checks.
391393 *
392394 * @return Link[]
393395 */
@@ -400,9 +402,78 @@ public function query_links(
400402 string $ order_by = self ::ORDER_DATE_DESC ,
401403 ?string $ search_term = null ,
402404 ?string $ date = null ,
403- ?bool $ excluded = null
405+ ?bool $ excluded = null ,
406+ ?array $ snapshot_process = null ,
407+ ?bool $ has_checks = null
404408 ): array {
405- // Remove any invalid statuses.
409+ $ rows = $ this ->perform_query ( $ limit , $ page , $ status , $ link_ids , $ archive_status , $ order_by , $ search_term , $ date , $ excluded , $ snapshot_process , $ has_checks );
410+ return array_map ( array ( $ this , 'map_link ' ), $ rows );
411+ }
412+
413+ /**
414+ * Gets the count of links for a given query.
415+ *
416+ * @param integer $limit The limit of links to return.
417+ * @param integer $page The page of links to return.
418+ * @param array $status The status of the links to return.
419+ * @param array $link_ids The link ids to query.
420+ * @param array $archive_status The archive status of the links to return.
421+ * @param string $order_by The order by.
422+ * @param string|NULL $search_term The search term to query.
423+ * @param string|NULL $date The date of the links to return (yy-mm).
424+ * @param boolean|NULL $excluded Whether to return excluded links.
425+ * @param string[]|NULL $snapshot_process The snapshot status of the links to return.
426+ * @param boolean|NULL $has_checks Whether to return links with or without checks.
427+ *
428+ * @return integer
429+ */
430+ public function count_links (
431+ int $ limit = 10 ,
432+ int $ page = 1 ,
433+ array $ status = array (),
434+ array $ link_ids = array (),
435+ array $ archive_status = array (),
436+ string $ order_by = self ::ORDER_DATE_DESC ,
437+ ?string $ search_term = null ,
438+ ?string $ date = null ,
439+ ?bool $ excluded = null ,
440+ ?array $ snapshot_process = null ,
441+ ?bool $ has_checks = null
442+ ): int {
443+ return count ( $ this ->perform_query ( $ limit , $ page , $ status , $ link_ids , $ archive_status , $ order_by , $ search_term , $ date , $ excluded , $ snapshot_process , $ has_checks ) );
444+ }
445+
446+ /**
447+ * Performs a link query.
448+ *
449+ * @param integer $limit The limit of links to return.
450+ * @param integer $page The page of links to return.
451+ * @param array $status The status of the links to return.
452+ * @param array $link_ids The link ids to query.
453+ * @param array $archive_status The archive status of the links to return.
454+ * @param string $order_by The order by.
455+ * @param string|NULL $search_term The search term to query.
456+ * @param string|NULL $date The date of the links to return (yy-mm).
457+ * @param boolean|NULL $excluded Whether to return excluded links.
458+ * @param string[]|NULL $snapshot_process The snapshot status of the links to return.
459+ * @param boolean|NULL $has_checks Whether to return links with or without checks.
460+ *
461+ * @return stdClass[]
462+ */
463+ private function perform_query (
464+ int $ limit = 10 ,
465+ int $ page = 1 ,
466+ array $ status = array (),
467+ array $ link_ids = array (),
468+ array $ archive_status = array (),
469+ string $ order_by = self ::ORDER_DATE_DESC ,
470+ ?string $ search_term = null ,
471+ ?string $ date = null ,
472+ ?bool $ excluded = null ,
473+ ?array $ snapshot_process = null ,
474+ ?bool $ has_checks = null
475+ ): array {
476+ // Remove any invalid statuses.
406477 $ status = array_filter (
407478 $ status ,
408479 function ( $ status ): bool {
@@ -474,6 +545,29 @@ function ( $link_id ): bool {
474545 $ where = true ;
475546 }
476547
548+ // If we are looking for links with or without checks, add to the query.
549+ if ( null !== $ has_checks ) {
550+ $ query .= true === $ where ? ' AND ' : ' WHERE ' ;
551+ $ query .= $ has_checks ? ' JSON_LENGTH(`checks`) > 0 ' : ' JSON_LENGTH(`checks`) = 0 ' ;
552+ $ where = true ;
553+ }
554+
555+ // If we have snapshot process status, add to the query.
556+ if ( ! empty ( $ snapshot_process ) ) {
557+ // Remove any invalid snapshot statuses.
558+ $ snapshot_process = array_filter (
559+ $ snapshot_process ,
560+ function ( $ status ): bool {
561+ return is_string ( $ status ) && in_array ( $ status , array ( Link::PROCESS_NEW , Link::PROCESS_PENDING , Link::PROCESS_DONE ), true );
562+ }
563+ );
564+
565+ $ place_holders = join ( ', ' , array_fill ( 0 , count ( $ snapshot_process ), '%s ' ) );
566+ $ snapshot_template = true === $ where ? " AND archive_process IN ( {$ place_holders }) " : " WHERE archive_process IN ( {$ place_holders }) " ;
567+ $ query .= $ this ->wpdb ->prepare ( $ snapshot_template , $ snapshot_process ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, Compiled in parts, very hard to escape
568+ $ where = true ;
569+ }
570+
477571 // If we have a search term, add to the query.
478572 if ( $ search_term ) {
479573 // Prepare the search term.
@@ -489,6 +583,7 @@ function ( $link_id ): bool {
489583 if ( null !== $ excluded ) {
490584 $ query .= true === $ where ? ' AND ' : ' WHERE ' ;
491585 $ query .= $ excluded ? ' excluded = 1 ' : ' excluded = 0 ' ;
586+ $ where = true ;
492587 }
493588
494589 // Add the order by.
@@ -506,7 +601,7 @@ function ( $link_id ): bool {
506601 return array ();
507602 }
508603
509- return array_map ( array ( $ this , ' map_link ' ), $ rows ) ;
604+ return $ rows ;
510605 }
511606
512607 /**
0 commit comments