setting( $key, 0 ); // settings() already fills in the field's default. return is_numeric( $value ) ? (int) $value : 0; }; } /** * Threshold facts of a check whose days come from a power-up setting: `setting` (the row links * to Settings) and `days` (read from it, with the field's default). One place for both. * * @since 1.0.0 * @param Module $module Power-up. * @param string $key Setting. * @param \Closure|null $days How to turn the setting into days (default: the setting itself). * @return array{setting: string, days: \Closure} */ public static function threshold( Module $module, string $key, ?\Closure $days = null ): array { return array( 'setting' => $key, 'days' => $days ? $days : self::days( $module, $key ), ); } /** * WordPress's trash period in days (EMPTY_TRASH_DAYS, else 30). * * @since 1.0.0 * @return int */ public static function wp_trash_days(): int { return defined( 'EMPTY_TRASH_DAYS' ) && (int) EMPTY_TRASH_DAYS > 0 ? (int) EMPTY_TRASH_DAYS : 30; } /** * Trash period in days: the setting, or WordPress's. * * @since 1.0.0 * @param Module $module Power-up. * @return \Closure(): int */ public static function trash_days( Module $module ): \Closure { return static function () use ( $module ): int { $days = $module->setting( 'trash_days', 0 ); $days = is_numeric( $days ) ? (int) $days : 0; return $days > 0 ? $days : self::wp_trash_days(); }; } /** * A `forget` callback that deletes each parent ID from a cache group. * * @since 1.0.0 * @param string $group Cache group. * @return \Closure(array): void */ public static function forget_group( string $group ): \Closure { return static function ( array $ids ) use ( $group ): void { foreach ( $ids as $id ) { wp_cache_delete( (int) $id, $group ); } }; } /** * Sample columns of a meta table (the value is masked by key and content). * * @since 1.0.0 * @param string $id_col Meta ID column. * @param string $parent_col Parent ID column. * @param string $parent_label Parent column label. * @param bool $length_only Show only the value's length (the default: meta values can hold * anything a customer or plugin stored, such as names and notes). * @return array */ public static function meta_sample( string $id_col, string $parent_col, string $parent_label, bool $length_only = true ): array { return array( 'columns' => array( $id_col => __( 'Meta ID', 'admin-powerups-for-woocommerce' ), $parent_col => $parent_label, 'meta_key' => __( 'Key', 'admin-powerups-for-woocommerce' ), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- a column label, not a query. 'meta_value' => __( 'Value', 'admin-powerups-for-woocommerce' ), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- a column label, not a query. ), 'meta' => array( 'key' => 'meta_key', 'value' => 'meta_value', 'length_only' => $length_only, ), ); } /** * Sample rows of posts: ID, type, title, parent and date. * * @since 1.0.0 * @param array $rows Rows with ID, post_type, post_title, post_parent, post_date. * @param string $parent_label Parent column label. * @return array{columns: array, rows: array>} */ public static function post_sample( array $rows, string $parent_label ): array { return array( 'columns' => array( 'id' => __( 'ID', 'admin-powerups-for-woocommerce' ), 'type' => __( 'Type', 'admin-powerups-for-woocommerce' ), 'title' => __( 'Title', 'admin-powerups-for-woocommerce' ), 'parent' => $parent_label, 'date' => __( 'Date', 'admin-powerups-for-woocommerce' ), ), 'rows' => array_map( static fn( $row ) => array( 'id' => BaseCheck::cell( $row->ID ?? '' ), 'type' => BaseCheck::cell( $row->post_type ?? '' ), 'title' => BaseCheck::value_cell( $row->post_title ?? '' ), 'parent' => BaseCheck::cell( $row->post_parent ?? '' ), 'date' => BaseCheck::cell( $row->post_date ?? '' ), ), $rows ), ); } }