PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.5.6
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.5.6
3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 All 194 releases
convertkit / admin / class-convertkit-admin-restrict-content.php

class-convertkit-admin-restrict-content.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 2.5.6, at admin/class-convertkit-admin-restrict-content.php

270 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 Tags resource class.
22 *
23 * @since 2.3.2
24 *
25 * @var bool|ConvertKit_Resource_Tags
26 */
27 public $tags = false;
28
29 /**
30 * Holds the ConvertKit Products resource class.
31 *
32 * @since 2.1.0
33 *
34 * @var bool|ConvertKit_Resource_Products
35 */
36 public $products = false;
37
38 /**
39 * Holds the Restrict Content Settings class.
40 *
41 * @since 2.1.0
42 *
43 * @var bool|ConvertKit_Settings_Restrict_Content
44 */
45 public $restrict_content_settings = false;
46
47 /**
48 * Holds the value chosen for the Restrict Content filter dropdown
49 * in the WP_List_Table.
50 *
51 * @since 2.1.0
52 *
53 * @var int|string
54 */
55 public $restrict_content_filter = 0;
56
57 /**
58 * Registers action and filter hooks.
59 *
60 * @since 2.1.0
61 */
62 public function __construct() {
63
64 // Add New Member Content Wizard button to Pages.
65 add_filter( 'convertkit_admin_post_register_add_new_buttons', array( $this, 'register_add_new_button' ), 10, 2 );
66
67 // Filter Page's post state to maybe include a label denoting that Restricted Content is enabled.
68 // We do this here so we don't run this on Post Types that don't support Restricted Content.
69 add_filter( 'display_post_states', array( $this, 'maybe_display_restrict_content_post_state' ), 10, 2 );
70
71 // Filter WP_List_Table by Restrict Content setting.
72 add_action( 'pre_get_posts', array( $this, 'filter_wp_list_table_output' ) );
73 add_action( 'restrict_manage_posts', array( $this, 'output_wp_list_table_filters' ) );
74
75 }
76
77 /**
78 * Query Pages in the WP_List_Table by the Restrict Content filter, if a filter
79 * was included in the request.
80 *
81 * @since 2.1.0
82 *
83 * @param WP_Query $query WordPress Query.
84 */
85 public function filter_wp_list_table_output( $query ) {
86
87 // Bail if no Restrict Content filter specified.
88 if ( ! array_key_exists( 'convertkit_restrict_content', $_REQUEST ) ) { // phpcs:ignore WordPress.Security.NonceVerification
89 return;
90 }
91 if ( ! $_REQUEST['convertkit_restrict_content'] ) { // phpcs:ignore WordPress.Security.NonceVerification
92 return;
93 }
94
95 // Don't filter if we're not querying a Post Type that supports Restricted Content.
96 if ( ! in_array( $query->get( 'post_type' ), convertkit_get_supported_post_types(), true ) ) {
97 return;
98 }
99
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 );
110
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";.
114
115 // Add value to query.
116 $meta_query = array(
117 'key' => '_wp_convertkit_post_meta',
118 'value' => $value,
119 'compare' => 'LIKE',
120 );
121
122 // If the existing meta query is an array, append our query to it, so we honor
123 // any other constraints that have been defined by WordPress or third party code.
124 $existing_meta_query = $query->get( 'meta_query' );
125 if ( is_array( $existing_meta_query ) ) {
126 $existing_meta_query[] = $meta_query;
127 $query->set( 'meta_query', $existing_meta_query );
128 } else {
129 $query->set( 'meta_query', array( $meta_query ) );
130 }
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
135 }
136
137 /**
138 * Registers a button in the Pages WP_List_Table linking to the the Restrict Content Setup Wizard.
139 *
140 * @since 2.5.5
141 *
142 * @param array $buttons Buttons.
143 * @param string $post_type Post Type.
144 * @return array Views
145 */
146 public function register_add_new_button( $buttons, $post_type ) {
147
148 // If no API credentials have been set, don't output the button.
149 $settings = new ConvertKit_Settings();
150 if ( ! $settings->has_access_and_refresh_token() ) {
151 return $buttons;
152 }
153
154 // Bail if the Post Type isn't supported.
155 if ( ! in_array( $post_type, convertkit_get_supported_post_types(), true ) ) {
156 return $buttons;
157 }
158
159 // Register button.
160 $buttons['convertkit_restrict_content_setup'] = array(
161 'url' => add_query_arg(
162 array(
163 'page' => 'convertkit-restrict-content-setup',
164 'ck_post_type' => $post_type,
165 ),
166 admin_url( 'options.php' )
167 ),
168 'label' => __( 'Member Content', 'convertkit' ),
169 );
170
171 return $buttons;
172
173 }
174
175 /**
176 * Outputs a dropdown filter on a WP_List_Table filter section to permit
177 * filtering by a Restrict Content Form, Tag or Product.
178 *
179 * @since 2.1.0
180 *
181 * @param string $post_type Post Type.
182 */
183 public function output_wp_list_table_filters( $post_type ) {
184
185 // Don't output filters if we're not viewing a Post Type that supports Restricted Content.
186 if ( ! in_array( $post_type, convertkit_get_supported_post_types(), true ) ) {
187 return;
188 }
189
190 // Don't output filters if API credentials have not been defined in the Plugin's settings.
191 $settings = new ConvertKit_Settings();
192 if ( ! $settings->has_access_and_refresh_token() ) {
193 return;
194 }
195
196 // Fetch Products and Tags.
197 $this->products = new ConvertKit_Resource_Products();
198 $this->tags = new ConvertKit_Resource_Tags();
199
200 // Don't display filter if no Tags and no Products exist.
201 if ( ! $this->products->exist() && ! $this->tags->exist() ) {
202 return;
203 }
204
205 // Output filter.
206 include_once CONVERTKIT_PLUGIN_PATH . '/views/backend/post/wp-list-table-filter.php';
207
208 }
209
210 /**
211 * Appends the 'ConvertKit Member Content' text to a Page's Title in the WP_List_Table,
212 * if the given Page has a Restrict Content setting.
213 *
214 * @param string[] $post_states An array of post display states.
215 * @param WP_Post $post The current post object.
216 * @return string[] An array of post display states
217 */
218 public function maybe_display_restrict_content_post_state( $post_states, $post ) {
219
220 // Bail if we're not on a WP_List_Table screen for a supported Post Type.
221 if ( ! $this->is_wp_list_table_request_for_supported_post_type() ) {
222 return $post_states;
223 }
224
225 // Fetch Post's settings.
226 $convertkit_post = new ConvertKit_Post( $post->ID );
227
228 // Return post states, unedited, if Restrict Content isn't enabled on this Post.
229 if ( ! $convertkit_post->restrict_content_enabled() ) {
230 return $post_states;
231 }
232
233 // Add Post State.
234 $post_states['convertkit_restrict_content'] = esc_html__( 'ConvertKit Member Content', 'convertkit' );
235
236 // Return.
237 return $post_states;
238
239 }
240
241 /**
242 * Determines if the current request is for a WP_List_Table, and if so that
243 * the Post Type we're viewing supports Restrict Content functionality.
244 *
245 * @since 2.1.0
246 *
247 * @return bool Is WP_List_Table request for a supported Post Type.
248 */
249 private function is_wp_list_table_request_for_supported_post_type() {
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
259 // Bail if we're not on an edit.php screen.
260 if ( $screen->base !== 'edit' ) {
261 return false;
262 }
263
264 // Return whether Post Type is supported for Restrict Content functionality.
265 return in_array( $screen->post_type, convertkit_get_supported_post_types(), true );
266
267 }
268
269 }
270