Skip to content

Commit 9650350

Browse files
refactor: add optional limit parameter to product meta count
1 parent df317c9 commit 9650350

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

classes/plugin.class.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,12 +626,16 @@ function get_product_meta_all() {
626626
/**
627627
* Get the count of all the created PPOM Group fields.
628628
*
629+
* @param int $limit Optional limit on number of results to count
629630
* @return int - The number of group fields in the database.
630631
*/
631-
public static function get_product_meta_count() {
632+
public static function get_product_meta_count( $limit = null ) {
632633
global $wpdb;
633634

634635
$qry = 'SELECT COUNT(*) FROM ' . $wpdb->prefix . PPOM_TABLE_META;
636+
if ($limit !== null) {
637+
$qry .= ' LIMIT ' . intval($limit);
638+
}
635639
$count = $wpdb->get_var($qry);
636640

637641
return intval($count);

classes/survey.class.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
*/
1515
class PPOM_Survey {
1616

17+
/**
18+
* The maximum limit of created groups to count. Used in the duration of the cached value.
19+
*
20+
* @var int
21+
*/
22+
public const GROUPS_COUNT_LIMIT = 100;
23+
1724
/**
1825
* Reference to singleton insance.
1926
*
@@ -55,8 +62,8 @@ public function get_survey_metadata( $data, $page_slug ) {
5562
$group_fields_count = get_transient( PPOM_GROUPS_COUNT_CACHE_KEY );
5663

5764
if ( false === $group_fields_count ) {
58-
$group_fields_count = min( 100, NM_PersonalizedProduct::get_product_meta_count() );
59-
set_transient( PPOM_GROUPS_COUNT_CACHE_KEY, $group_fields_count, 100 <= $group_fields_count ? WEEK_IN_SECONDS : 12 * HOUR_IN_SECONDS );
65+
$group_fields_count = NM_PersonalizedProduct::get_product_meta_count( self::GROUPS_COUNT_LIMIT );
66+
set_transient( PPOM_GROUPS_COUNT_CACHE_KEY, $group_fields_count, self::GROUPS_COUNT_LIMIT === $group_fields_count ? WEEK_IN_SECONDS : 12 * HOUR_IN_SECONDS );
6067
}
6168

6269
$install_days_number = intval( ( time() - get_option( 'woocommerce_product_addon_install', time() ) ) / DAY_IN_SECONDS );

0 commit comments

Comments
 (0)