From f1c83301bc73f3127ae33f4d86a0eadd8191c39b Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Wed, 25 Jun 2025 14:01:45 +0800 Subject: [PATCH 1/2] Restrict Content: Add `All member-only content` filter --- ...lass-convertkit-admin-restrict-content.php | 57 +++++++++++-------- views/backend/post/wp-list-table-filter.php | 1 + 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/admin/class-convertkit-admin-restrict-content.php b/admin/class-convertkit-admin-restrict-content.php index c6340e82f..3c0b6f8e9 100644 --- a/admin/class-convertkit-admin-restrict-content.php +++ b/admin/class-convertkit-admin-restrict-content.php @@ -106,27 +106,41 @@ public function filter_wp_list_table_output( $query ) { return; } - // Build query. - // Because WordPress stores metadata in a single serialized string for a single key, we have to search - // the string for the Restrict Content setting. However, other settings will also be in this serialized string, - // so to avoid false positives, we define our search value formatted as a serialized string comprising of the - // setting name and value, to be as accurate as possible. - $value = maybe_serialize( - array( - 'restrict_content' => sanitize_text_field( wp_unslash( $_REQUEST['convertkit_restrict_content'] ) ), // phpcs:ignore WordPress.Security.NonceVerification - ) - ); - - // Strip a:1:{ and final }, as a Post's serialized settings will include other settings. - $value = str_replace( 'a:1:{', '', $value ); // e.g. s:16:"restrict_content";s:13:"product_36377";}. - $value = substr( $value, 0, strlen( $value ) - 1 ); // e.g. s:16:"restrict_content";s:13:"product_36377";. + // Store Restrict Content filter value. + $this->restrict_content_filter = sanitize_text_field( wp_unslash( $_REQUEST['convertkit_restrict_content'] ) ); // phpcs:ignore WordPress.Security.NonceVerification - // Add value to query. - $meta_query = array( - 'key' => '_wp_convertkit_post_meta', - 'value' => $value, - 'compare' => 'LIKE', - ); + switch ( $this->restrict_content_filter ) { + case 'all-member-only': + $meta_query = array( + 'key' => '_wp_convertkit_post_meta', + 'value' => '.*?(form|tag|product)_[0-9]+.*?', + 'compare' => 'REGEXP', + ); + break; + default: + // Build query. + // Because WordPress stores metadata in a single serialized string for a single key, we have to search + // the string for the Restrict Content setting. However, other settings will also be in this serialized string, + // so to avoid false positives, we define our search value formatted as a serialized string comprising of the + // setting name and value, to be as accurate as possible. + $value = maybe_serialize( + array( + 'restrict_content' => $this->restrict_content_filter, + ) + ); + + // Strip a:1:{ and final }, as a Post's serialized settings will include other settings. + $value = str_replace( 'a:1:{', '', $value ); // e.g. s:16:"restrict_content";s:13:"product_36377";}. + $value = substr( $value, 0, strlen( $value ) - 1 ); // e.g. s:16:"restrict_content";s:13:"product_36377";. + + // Add value to query. + $meta_query = array( + 'key' => '_wp_convertkit_post_meta', + 'value' => $value, + 'compare' => 'LIKE', + ); + break; + } // If the existing meta query is an array, append our query to it, so we honor // any other constraints that have been defined by WordPress or third party code. @@ -138,9 +152,6 @@ public function filter_wp_list_table_output( $query ) { $query->set( 'meta_query', array( $meta_query ) ); } - // Store Restrict Content filter value. - $this->restrict_content_filter = sanitize_text_field( wp_unslash( $_REQUEST['convertkit_restrict_content'] ) ); // phpcs:ignore WordPress.Security.NonceVerification - } /** diff --git a/views/backend/post/wp-list-table-filter.php b/views/backend/post/wp-list-table-filter.php index 9c10cceb3..7d1883042 100644 --- a/views/backend/post/wp-list-table-filter.php +++ b/views/backend/post/wp-list-table-filter.php @@ -9,6 +9,7 @@ ?>