← All changes
|
admin/class-convertkit-admin-restrict-content.php
+38
-126
3.3.2
→
2.5.5
View file →
| @@ -17,17 +17,8 @@ | ||
| 17 | 17 | */ |
| 18 | 18 | class ConvertKit_Admin_Restrict_Content { |
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | - * Holds the ConvertKit Forms resource class. | |
| 22 | - * | |
| 23 | - * @since 2.7.3 | |
| 24 | - * | |
| 25 | - * @var bool|ConvertKit_Resource_Forms | |
| 26 | - */ | |
| 27 | - public $forms = false; | |
| 28 | - | |
| 29 | - /** | |
| 30 | 21 | * Holds the ConvertKit Tags resource class. |
| 31 | 22 | * |
| 32 | 23 | * @since 2.3.2 |
| 33 | 24 | * |
| @@ -63,17 +54,8 @@ | ||
| 63 | 54 | */ |
| 64 | 55 | public $restrict_content_filter = 0; |
| 65 | 56 | |
| 66 | 57 | /** |
| 67 | - * Holds the key for the restrict content enabled flag in the WordPress options table. | |
| 68 | - * | |
| 69 | - * @since 3.0.4 | |
| 70 | - * | |
| 71 | - * @var string | |
| 72 | - */ | |
| 73 | - public $restrict_content_enabled_key = 'convertkit_restrict_content_enabled'; | |
| 74 | - | |
| 75 | - /** | |
| 76 | 58 | * Registers action and filter hooks. |
| 77 | 59 | * |
| 78 | 60 | * @since 2.1.0 |
| 79 | 61 | */ |
| @@ -89,70 +71,11 @@ | ||
| 89 | 71 | // Filter WP_List_Table by Restrict Content setting. |
| 90 | 72 | add_action( 'pre_get_posts', array( $this, 'filter_wp_list_table_output' ) ); |
| 91 | 73 | add_action( 'restrict_manage_posts', array( $this, 'output_wp_list_table_filters' ) ); |
| 92 | 74 | |
| 93 | - // Update whether any Pages, Posts or CPTs are configured to use Restrict Content when a Page, Post or CPT is created, edited, trashed or deleted. | |
| 94 | - add_action( 'wp_insert_post', array( $this, 'update_restrict_content_enabled' ) ); | |
| 95 | - add_action( 'trashed_post', array( $this, 'update_restrict_content_enabled' ) ); | |
| 96 | - add_action( 'delete_post', array( $this, 'update_restrict_content_enabled' ) ); | |
| 97 | - | |
| 98 | 75 | } |
| 99 | 76 | |
| 100 | 77 | /** |
| 101 | - * Update whether any Pages, Posts or CPTs are configured to use Restrict Content when a | |
| 102 | - * Page, Post or CPT is created, edited or deleted. | |
| 103 | - * | |
| 104 | - * @since 3.0.4 | |
| 105 | - */ | |
| 106 | - public function update_restrict_content_enabled() { | |
| 107 | - | |
| 108 | - global $wpdb; | |
| 109 | - | |
| 110 | - // We don't use WP_Query, as it's expensive on sites with many Pages/Posts/CPTs. | |
| 111 | - $results = $wpdb->get_results( | |
| 112 | - $wpdb->prepare( | |
| 113 | - 'SELECT ' . $wpdb->posts . '.ID | |
| 114 | - FROM ' . $wpdb->postmeta . ' | |
| 115 | - LEFT JOIN ' . $wpdb->posts . ' ON ' . $wpdb->postmeta . '.post_id = ' . $wpdb->posts . ".ID | |
| 116 | - WHERE ( | |
| 117 | - (meta_key = '_wp_convertkit_post_meta' AND meta_value LIKE %s) | |
| 118 | - AND | |
| 119 | - (meta_key = '_wp_convertkit_post_meta' AND meta_value NOT LIKE %s) | |
| 120 | - AND | |
| 121 | - (meta_key = '_wp_convertkit_post_meta' AND meta_value NOT LIKE %s) | |
| 122 | - ) | |
| 123 | - AND | |
| 124 | - " . $wpdb->posts . ".post_status = 'publish' | |
| 125 | - AND | |
| 126 | - " . $wpdb->posts . ".post_type IN ('" . implode( "', '", convertkit_get_supported_post_types() ) . "') LIMIT 1;", // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.QuotedDynamicPlaceholderGeneration | |
| 127 | - '%' . $wpdb->esc_like( '"restrict_content";' ) . '%', | |
| 128 | - '%' . $wpdb->esc_like( '"restrict_content";s:0:' ) . '%', | |
| 129 | - '%' . $wpdb->esc_like( '"restrict_content";s:1:"0"' ) . '%' | |
| 130 | - ) | |
| 131 | - ); | |
| 132 | - | |
| 133 | - // If results are not empty, a Page, Post or CPT is configured to use Restrict Content. | |
| 134 | - $restrict_content_enabled = ! empty( $results ); | |
| 135 | - | |
| 136 | - // Update option. | |
| 137 | - update_option( $this->restrict_content_enabled_key, $restrict_content_enabled ); | |
| 138 | - | |
| 139 | - } | |
| 140 | - | |
| 141 | - /** | |
| 142 | - * Returns whether any Pages, Posts or CPTs are configured to use Restrict Content. | |
| 143 | - * | |
| 144 | - * @since 3.0.4 | |
| 145 | - * | |
| 146 | - * @return bool | |
| 147 | - */ | |
| 148 | - public function restrict_content_enabled() { | |
| 149 | - | |
| 150 | - return (bool) get_option( $this->restrict_content_enabled_key, false ); | |
| 151 | - | |
| 152 | - } | |
| 153 | - | |
| 154 | - /** | |
| 155 | 78 | * Query Pages in the WP_List_Table by the Restrict Content filter, if a filter |
| 156 | 79 | * was included in the request. |
| 157 | 80 | * |
| 158 | 81 | * @since 2.1.0 |
| @@ -161,14 +84,12 @@ | ||
| 161 | 84 | */ |
| 162 | 85 | public function filter_wp_list_table_output( $query ) { |
| 163 | 86 | |
| 164 | 87 | // Bail if no Restrict Content filter specified. |
| 165 | - if ( ! filter_has_var( INPUT_GET, 'convertkit_restrict_content' ) ) { | |
| 88 | + if ( ! array_key_exists( 'convertkit_restrict_content', $_REQUEST ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 166 | 89 | return; |
| 167 | 90 | } |
| 168 | - | |
| 169 | - // Bail if the filter is empty. | |
| 170 | - if ( empty( filter_input( INPUT_GET, 'convertkit_restrict_content', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) ) { | |
| 91 | + if ( ! $_REQUEST['convertkit_restrict_content'] ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 171 | 92 | return; |
| 172 | 93 | } |
| 173 | 94 | |
| 174 | 95 | // Don't filter if we're not querying a Post Type that supports Restricted Content. |
| @@ -175,44 +96,30 @@ | ||
| 175 | 96 | if ( ! in_array( $query->get( 'post_type' ), convertkit_get_supported_post_types(), true ) ) { |
| 176 | 97 | return; |
| 177 | 98 | } |
| 178 | 99 | |
| 179 | - // Store Restrict Content filter value. | |
| 180 | - $this->restrict_content_filter = filter_input( INPUT_GET, 'convertkit_restrict_content', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); | |
| 100 | + // Build query. | |
| 101 | + // Because WordPress stores metadata in a single serialized string for a single key, we have to search | |
| 102 | + // the string for the Restrict Content setting. However, other settings will also be in this serialized string, | |
| 103 | + // so to avoid false positives, we define our search value formatted as a serialized string comprising of the | |
| 104 | + // setting name and value, to be as accurate as possible. | |
| 105 | + $value = maybe_serialize( | |
| 106 | + array( | |
| 107 | + 'restrict_content' => sanitize_text_field( $_REQUEST['convertkit_restrict_content'] ), // phpcs:ignore WordPress.Security.NonceVerification | |
| 108 | + ) | |
| 109 | + ); | |
| 181 | 110 | |
| 182 | - switch ( $this->restrict_content_filter ) { | |
| 183 | - case 'all-member-only': | |
| 184 | - $meta_query = array( | |
| 185 | - 'key' => '_wp_convertkit_post_meta', | |
| 186 | - 'value' => '.*?(form|tag|product)_[0-9]+.*?', | |
| 187 | - 'compare' => 'REGEXP', | |
| 188 | - ); | |
| 189 | - break; | |
| 190 | - default: | |
| 191 | - // Build query. | |
| 192 | - // Because WordPress stores metadata in a single serialized string for a single key, we have to search | |
| 193 | - // the string for the Restrict Content setting. However, other settings will also be in this serialized string, | |
| 194 | - // so to avoid false positives, we define our search value formatted as a serialized string comprising of the | |
| 195 | - // setting name and value, to be as accurate as possible. | |
| 196 | - $value = maybe_serialize( | |
| 197 | - array( | |
| 198 | - 'restrict_content' => $this->restrict_content_filter, | |
| 199 | - ) | |
| 200 | - ); | |
| 111 | + // Strip a:1:{ and final }, as a Post's serialized settings will include other settings. | |
| 112 | + $value = str_replace( 'a:1:{', '', $value ); // e.g. s:16:"restrict_content";s:13:"product_36377";}. | |
| 113 | + $value = substr( $value, 0, strlen( $value ) - 1 ); // e.g. s:16:"restrict_content";s:13:"product_36377";. | |
| 201 | 114 | |
| 202 | - // Strip a:1:{ and final }, as a Post's serialized settings will include other settings. | |
| 203 | - $value = str_replace( 'a:1:{', '', $value ); // e.g. s:16:"restrict_content";s:13:"product_36377";}. | |
| 204 | - $value = substr( $value, 0, strlen( $value ) - 1 ); // e.g. s:16:"restrict_content";s:13:"product_36377";. | |
| 115 | + // Add value to query. | |
| 116 | + $meta_query = array( | |
| 117 | + 'key' => '_wp_convertkit_post_meta', | |
| 118 | + 'value' => $value, | |
| 119 | + 'compare' => 'LIKE', | |
| 120 | + ); | |
| 205 | 121 | |
| 206 | - // Add value to query. | |
| 207 | - $meta_query = array( | |
| 208 | - 'key' => '_wp_convertkit_post_meta', | |
| 209 | - 'value' => $value, | |
| 210 | - 'compare' => 'LIKE', | |
| 211 | - ); | |
| 212 | - break; | |
| 213 | - } | |
| 214 | - | |
| 215 | 122 | // If the existing meta query is an array, append our query to it, so we honor |
| 216 | 123 | // any other constraints that have been defined by WordPress or third party code. |
| 217 | 124 | $existing_meta_query = $query->get( 'meta_query' ); |
| 218 | 125 | if ( is_array( $existing_meta_query ) ) { |
| @@ -221,8 +128,11 @@ | ||
| 221 | 128 | } else { |
| 222 | 129 | $query->set( 'meta_query', array( $meta_query ) ); |
| 223 | 130 | } |
| 224 | 131 | |
| 132 | + // Store Restrict Content filter value. | |
| 133 | + $this->restrict_content_filter = sanitize_text_field( $_REQUEST['convertkit_restrict_content'] ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 134 | + | |
| 225 | 135 | } |
| 226 | 136 | |
| 227 | 137 | /** |
| 228 | 138 | * Registers a button in the Pages WP_List_Table linking to the the Restrict Content Setup Wizard. |
| @@ -240,13 +150,8 @@ | ||
| 240 | 150 | if ( ! $settings->has_access_and_refresh_token() ) { |
| 241 | 151 | return $buttons; |
| 242 | 152 | } |
| 243 | 153 | |
| 244 | - // If the Add New Landing Page / Member Content button is disabled, don't output the button. | |
| 245 | - if ( $settings->add_new_button_disabled() ) { | |
| 246 | - return $buttons; | |
| 247 | - } | |
| 248 | - | |
| 249 | 154 | // Bail if the Post Type isn't supported. |
| 250 | 155 | if ( ! in_array( $post_type, convertkit_get_supported_post_types(), true ) ) { |
| 251 | 156 | return $buttons; |
| 252 | 157 | } |
| @@ -259,9 +164,9 @@ | ||
| 259 | 164 | 'ck_post_type' => $post_type, |
| 260 | 165 | ), |
| 261 | 166 | admin_url( 'options.php' ) |
| 262 | 167 | ), |
| 263 | - 'label' => __( 'Member Content', 'convertkit' ), | |
| 168 | + 'label' => __( 'Add New Member Content', 'convertkit' ), | |
| 264 | 169 | ); |
| 265 | 170 | |
| 266 | 171 | return $buttons; |
| 267 | 172 | |
| @@ -287,10 +192,9 @@ | ||
| 287 | 192 | if ( ! $settings->has_access_and_refresh_token() ) { |
| 288 | 193 | return; |
| 289 | 194 | } |
| 290 | 195 | |
| 291 | - // Initialize Forms, Products and Tags resource classes. | |
| 292 | - $this->forms = new ConvertKit_Resource_Forms(); | |
| 196 | + // Fetch Products and Tags. | |
| 293 | 197 | $this->products = new ConvertKit_Resource_Products(); |
| 294 | 198 | $this->tags = new ConvertKit_Resource_Tags(); |
| 295 | 199 | |
| 296 | 200 | // Don't display filter if no Tags and no Products exist. |
| @@ -303,9 +207,9 @@ | ||
| 303 | 207 | |
| 304 | 208 | } |
| 305 | 209 | |
| 306 | 210 | /** |
| 307 | - * Appends the 'Kit Member Content' text to a Page's Title in the WP_List_Table, | |
| 211 | + * Appends the 'ConvertKit Member Content' text to a Page's Title in the WP_List_Table, | |
| 308 | 212 | * if the given Page has a Restrict Content setting. |
| 309 | 213 | * |
| 310 | 214 | * @param string[] $post_states An array of post display states. |
| 311 | 215 | * @param WP_Post $post The current post object. |
| @@ -326,9 +230,9 @@ | ||
| 326 | 230 | return $post_states; |
| 327 | 231 | } |
| 328 | 232 | |
| 329 | 233 | // Add Post State. |
| 330 | - $post_states['convertkit_restrict_content'] = esc_html__( 'Kit Member Content', 'convertkit' ); | |
| 234 | + $post_states['convertkit_restrict_content'] = esc_html__( 'ConvertKit Member Content', 'convertkit' ); | |
| 331 | 235 | |
| 332 | 236 | // Return. |
| 333 | 237 | return $post_states; |
| 334 | 238 | |
| @@ -343,15 +247,23 @@ | ||
| 343 | 247 | * @return bool Is WP_List_Table request for a supported Post Type. |
| 344 | 248 | */ |
| 345 | 249 | private function is_wp_list_table_request_for_supported_post_type() { |
| 346 | 250 | |
| 251 | + // Bail if we cannot determine the screen. | |
| 252 | + if ( ! function_exists( 'get_current_screen' ) ) { | |
| 253 | + return false; | |
| 254 | + } | |
| 255 | + | |
| 256 | + // Get screen. | |
| 257 | + $screen = get_current_screen(); | |
| 258 | + | |
| 347 | 259 | // Bail if we're not on an edit.php screen. |
| 348 | - if ( convertkit_get_current_screen( 'base' ) !== 'edit' ) { | |
| 260 | + if ( $screen->base !== 'edit' ) { | |
| 349 | 261 | return false; |
| 350 | 262 | } |
| 351 | 263 | |
| 352 | 264 | // Return whether Post Type is supported for Restrict Content functionality. |
| 353 | - return in_array( convertkit_get_current_screen( 'post_type' ), convertkit_get_supported_post_types(), true ); | |
| 265 | + return in_array( $screen->post_type, convertkit_get_supported_post_types(), true ); | |
| 354 | 266 | |
| 355 | 267 | } |
| 356 | 268 | |
| 357 | 269 | } |