| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Admin Restrict Content class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Modifies the Pages WP_List_Table to provide: |
| 11 |
* - an 'Add New Member Content' button next to the 'Add New' button |
| 12 |
* - a dropdown filter to show Pages restricted to a Form, Tag or Product |
| 13 |
* - a 'ConvertKit Member Content' label appended to the Page's title when a Form, Tag or Product is selected |
| 14 |
* |
| 15 |
* @package ConvertKit |
| 16 |
* @author ConvertKit |
| 17 |
*/ |
| 18 |
class ConvertKit_Admin_Restrict_Content { |
| 19 |
|
| 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 |
* Holds the ConvertKit Tags resource class. |
| 31 |
* |
| 32 |
* @since 2.3.2 |
| 33 |
* |
| 34 |
* @var bool|ConvertKit_Resource_Tags |
| 35 |
*/ |
| 36 |
public $tags = false; |
| 37 |
|
| 38 |
/** |
| 39 |
* Holds the ConvertKit Products resource class. |
| 40 |
* |
| 41 |
* @since 2.1.0 |
| 42 |
* |
| 43 |
* @var bool|ConvertKit_Resource_Products |
| 44 |
*/ |
| 45 |
public $products = false; |
| 46 |
|
| 47 |
/** |
| 48 |
* Holds the Restrict Content Settings class. |
| 49 |
* |
| 50 |
* @since 2.1.0 |
| 51 |
* |
| 52 |
* @var bool|ConvertKit_Settings_Restrict_Content |
| 53 |
*/ |
| 54 |
public $restrict_content_settings = false; |
| 55 |
|
| 56 |
/** |
| 57 |
* Holds the value chosen for the Restrict Content filter dropdown |
| 58 |
* in the WP_List_Table. |
| 59 |
* |
| 60 |
* @since 2.1.0 |
| 61 |
* |
| 62 |
* @var int|string |
| 63 |
*/ |
| 64 |
public $restrict_content_filter = 0; |
| 65 |
|
| 66 |
/** |
| 67 |
* Registers action and filter hooks. |
| 68 |
* |
| 69 |
* @since 2.1.0 |
| 70 |
*/ |
| 71 |
public function __construct() { |
| 72 |
|
| 73 |
// Add New Member Content Wizard button to Pages. |
| 74 |
add_filter( 'convertkit_admin_post_register_add_new_buttons', array( $this, 'register_add_new_button' ), 10, 2 ); |
| 75 |
|
| 76 |
// Filter Page's post state to maybe include a label denoting that Restricted Content is enabled. |
| 77 |
// We do this here so we don't run this on Post Types that don't support Restricted Content. |
| 78 |
add_filter( 'display_post_states', array( $this, 'maybe_display_restrict_content_post_state' ), 10, 2 ); |
| 79 |
|
| 80 |
// Filter WP_List_Table by Restrict Content setting. |
| 81 |
add_action( 'pre_get_posts', array( $this, 'filter_wp_list_table_output' ) ); |
| 82 |
add_action( 'restrict_manage_posts', array( $this, 'output_wp_list_table_filters' ) ); |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Query Pages in the WP_List_Table by the Restrict Content filter, if a filter |
| 88 |
* was included in the request. |
| 89 |
* |
| 90 |
* @since 2.1.0 |
| 91 |
* |
| 92 |
* @param WP_Query $query WordPress Query. |
| 93 |
*/ |
| 94 |
public function filter_wp_list_table_output( $query ) { |
| 95 |
|
| 96 |
// Bail if no Restrict Content filter specified. |
| 97 |
if ( ! array_key_exists( 'convertkit_restrict_content', $_REQUEST ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 98 |
return; |
| 99 |
} |
| 100 |
if ( ! sanitize_text_field( wp_unslash( $_REQUEST['convertkit_restrict_content'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
// Don't filter if we're not querying a Post Type that supports Restricted Content. |
| 105 |
if ( ! in_array( $query->get( 'post_type' ), convertkit_get_supported_post_types(), true ) ) { |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
// Build query. |
| 110 |
// Because WordPress stores metadata in a single serialized string for a single key, we have to search |
| 111 |
// the string for the Restrict Content setting. However, other settings will also be in this serialized string, |
| 112 |
// so to avoid false positives, we define our search value formatted as a serialized string comprising of the |
| 113 |
// setting name and value, to be as accurate as possible. |
| 114 |
$value = maybe_serialize( |
| 115 |
array( |
| 116 |
'restrict_content' => sanitize_text_field( wp_unslash( $_REQUEST['convertkit_restrict_content'] ) ), // phpcs:ignore WordPress.Security.NonceVerification |
| 117 |
) |
| 118 |
); |
| 119 |
|
| 120 |
// Strip a:1:{ and final }, as a Post's serialized settings will include other settings. |
| 121 |
$value = str_replace( 'a:1:{', '', $value ); // e.g. s:16:"restrict_content";s:13:"product_36377";}. |
| 122 |
$value = substr( $value, 0, strlen( $value ) - 1 ); // e.g. s:16:"restrict_content";s:13:"product_36377";. |
| 123 |
|
| 124 |
// Add value to query. |
| 125 |
$meta_query = array( |
| 126 |
'key' => '_wp_convertkit_post_meta', |
| 127 |
'value' => $value, |
| 128 |
'compare' => 'LIKE', |
| 129 |
); |
| 130 |
|
| 131 |
// If the existing meta query is an array, append our query to it, so we honor |
| 132 |
// any other constraints that have been defined by WordPress or third party code. |
| 133 |
$existing_meta_query = $query->get( 'meta_query' ); |
| 134 |
if ( is_array( $existing_meta_query ) ) { |
| 135 |
$existing_meta_query[] = $meta_query; |
| 136 |
$query->set( 'meta_query', $existing_meta_query ); |
| 137 |
} else { |
| 138 |
$query->set( 'meta_query', array( $meta_query ) ); |
| 139 |
} |
| 140 |
|
| 141 |
// Store Restrict Content filter value. |
| 142 |
$this->restrict_content_filter = sanitize_text_field( wp_unslash( $_REQUEST['convertkit_restrict_content'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Registers a button in the Pages WP_List_Table linking to the the Restrict Content Setup Wizard. |
| 148 |
* |
| 149 |
* @since 2.5.5 |
| 150 |
* |
| 151 |
* @param array $buttons Buttons. |
| 152 |
* @param string $post_type Post Type. |
| 153 |
* @return array Views |
| 154 |
*/ |
| 155 |
public function register_add_new_button( $buttons, $post_type ) { |
| 156 |
|
| 157 |
// If no API credentials have been set, don't output the button. |
| 158 |
$settings = new ConvertKit_Settings(); |
| 159 |
if ( ! $settings->has_access_and_refresh_token() ) { |
| 160 |
return $buttons; |
| 161 |
} |
| 162 |
|
| 163 |
// Bail if the Post Type isn't supported. |
| 164 |
if ( ! in_array( $post_type, convertkit_get_supported_post_types(), true ) ) { |
| 165 |
return $buttons; |
| 166 |
} |
| 167 |
|
| 168 |
// Register button. |
| 169 |
$buttons['convertkit_restrict_content_setup'] = array( |
| 170 |
'url' => add_query_arg( |
| 171 |
array( |
| 172 |
'page' => 'convertkit-restrict-content-setup', |
| 173 |
'ck_post_type' => $post_type, |
| 174 |
), |
| 175 |
admin_url( 'options.php' ) |
| 176 |
), |
| 177 |
'label' => __( 'Member Content', 'convertkit' ), |
| 178 |
); |
| 179 |
|
| 180 |
return $buttons; |
| 181 |
|
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Outputs a dropdown filter on a WP_List_Table filter section to permit |
| 186 |
* filtering by a Restrict Content Form, Tag or Product. |
| 187 |
* |
| 188 |
* @since 2.1.0 |
| 189 |
* |
| 190 |
* @param string $post_type Post Type. |
| 191 |
*/ |
| 192 |
public function output_wp_list_table_filters( $post_type ) { |
| 193 |
|
| 194 |
// Don't output filters if we're not viewing a Post Type that supports Restricted Content. |
| 195 |
if ( ! in_array( $post_type, convertkit_get_supported_post_types(), true ) ) { |
| 196 |
return; |
| 197 |
} |
| 198 |
|
| 199 |
// Don't output filters if API credentials have not been defined in the Plugin's settings. |
| 200 |
$settings = new ConvertKit_Settings(); |
| 201 |
if ( ! $settings->has_access_and_refresh_token() ) { |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
// Initialize Forms, Products and Tags resource classes. |
| 206 |
$this->forms = new ConvertKit_Resource_Forms(); |
| 207 |
$this->products = new ConvertKit_Resource_Products(); |
| 208 |
$this->tags = new ConvertKit_Resource_Tags(); |
| 209 |
|
| 210 |
// Don't display filter if no Tags and no Products exist. |
| 211 |
if ( ! $this->products->exist() && ! $this->tags->exist() ) { |
| 212 |
return; |
| 213 |
} |
| 214 |
|
| 215 |
// Output filter. |
| 216 |
include_once CONVERTKIT_PLUGIN_PATH . '/views/backend/post/wp-list-table-filter.php'; |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Appends the 'Kit Member Content' text to a Page's Title in the WP_List_Table, |
| 222 |
* if the given Page has a Restrict Content setting. |
| 223 |
* |
| 224 |
* @param string[] $post_states An array of post display states. |
| 225 |
* @param WP_Post $post The current post object. |
| 226 |
* @return string[] An array of post display states |
| 227 |
*/ |
| 228 |
public function maybe_display_restrict_content_post_state( $post_states, $post ) { |
| 229 |
|
| 230 |
// Bail if we're not on a WP_List_Table screen for a supported Post Type. |
| 231 |
if ( ! $this->is_wp_list_table_request_for_supported_post_type() ) { |
| 232 |
return $post_states; |
| 233 |
} |
| 234 |
|
| 235 |
// Fetch Post's settings. |
| 236 |
$convertkit_post = new ConvertKit_Post( $post->ID ); |
| 237 |
|
| 238 |
// Return post states, unedited, if Restrict Content isn't enabled on this Post. |
| 239 |
if ( ! $convertkit_post->restrict_content_enabled() ) { |
| 240 |
return $post_states; |
| 241 |
} |
| 242 |
|
| 243 |
// Add Post State. |
| 244 |
$post_states['convertkit_restrict_content'] = esc_html__( 'Kit Member Content', 'convertkit' ); |
| 245 |
|
| 246 |
// Return. |
| 247 |
return $post_states; |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Determines if the current request is for a WP_List_Table, and if so that |
| 253 |
* the Post Type we're viewing supports Restrict Content functionality. |
| 254 |
* |
| 255 |
* @since 2.1.0 |
| 256 |
* |
| 257 |
* @return bool Is WP_List_Table request for a supported Post Type. |
| 258 |
*/ |
| 259 |
private function is_wp_list_table_request_for_supported_post_type() { |
| 260 |
|
| 261 |
// Bail if we're not on an edit.php screen. |
| 262 |
if ( convertkit_get_current_screen( 'base' ) !== 'edit' ) { |
| 263 |
return false; |
| 264 |
} |
| 265 |
|
| 266 |
// Return whether Post Type is supported for Restrict Content functionality. |
| 267 |
return in_array( convertkit_get_current_screen( 'post_type' ), convertkit_get_supported_post_types(), true ); |
| 268 |
|
| 269 |
} |
| 270 |
|
| 271 |
} |
| 272 |
|