PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.5.5
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.5.5
3.4.2 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 All 195 releases
convertkit / admin / class-convertkit-admin-post.php

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

347 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Admin Post class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Registers a metabox on Posts, Pages and public facing Custom Post Types
11 * and saves its settings when the Post is saved in the WordPress Administration
12 * interface.
13 *
14 * @package ConvertKit
15 * @author ConvertKit
16 */
17 class ConvertKit_Admin_Post {
18
19 /**
20 * Registers action and filter hooks.
21 *
22 * @since 1.9.6
23 */
24 public function __construct() {
25
26 // Register "Add New" ConvertKit button on Pages.
27 add_filter( 'views_edit-page', array( $this, 'output_wp_list_table_buttons' ) );
28
29 add_action( 'post_submitbox_misc_actions', array( $this, 'output_pre_publish_actions' ) );
30 add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
31 add_action( 'save_post', array( $this, 'save_post_meta' ) );
32
33 }
34
35 /**
36 * Registers 'Add New' buttons for the given Post Type's admin screen.
37 *
38 * If no options are registered, no button is displayed.
39 *
40 * JS will move this button to be displayed next to the "Add New" button when viewing the table of Pages or Posts,
41 * as there is not a native WordPress action/filter for registering buttons next to the "Add New" button.
42 *
43 * @since 2.5.5
44 *
45 * @param array $views Views.
46 * @return array Views
47 */
48 public function output_wp_list_table_buttons( $views ) {
49
50 // Get current post type that we're viewing.
51 $post_type = $this->get_current_post_type();
52
53 // Don't output any buttons if we couldn't determine the current post type.
54 if ( ! $post_type ) {
55 return $views;
56 }
57
58 // Define a blank array of buttons to be filtered.
59 $buttons = array();
60
61 /**
62 * Registers 'Add New' buttons for the given Post Type's admin screen.
63 *
64 * @since 2.5.5
65 *
66 * @param array $buttons Buttons.
67 * @param string $post_type Post Type.
68 */
69 $buttons = apply_filters( 'convertkit_admin_post_register_add_new_buttons', $buttons, $post_type );
70
71 // Bail if no buttons are registered for display.
72 if ( ! count( $buttons ) ) {
73 return $views;
74 }
75
76 // Enqueue JS and CSS.
77 wp_enqueue_script( 'convertkit-admin-wp-list-table-buttons', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/wp-list-table-buttons.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
78 wp_enqueue_style( 'convertkit-admin-wp-list-table-buttons', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/wp-list-table-buttons.css', array(), CONVERTKIT_PLUGIN_VERSION );
79
80 // Register buttons.
81 foreach ( $buttons as $button_name => $button ) {
82 $views[ $button_name ] = sprintf(
83 '<a href="%s" class="convertkit-action page-title-action hidden">%s</a>',
84 esc_attr( $button['url'] ),
85 esc_html( $button['label'] )
86 );
87
88 }
89
90 return $views;
91
92 }
93
94 /**
95 * Enqueue JavaScript when editing a Page, Post or Custom Post Type that outputs
96 * ConvertKit Plugin settings.
97 *
98 * @since 1.9.6.4
99 */
100 public function enqueue_scripts() {
101
102 // Enqueue Select2 JS.
103 convertkit_select2_enqueue_scripts();
104
105 /**
106 * Enqueue JavaScript when editing a Page, Post or Custom Post Type that outputs
107 * ConvertKit Plugin settings.
108 *
109 * @since 1.9.6.4
110 */
111 do_action( 'convertkit_admin_post_enqueue_scripts' );
112
113 }
114
115 /**
116 * Enqueue CSS when editing a Page, Post or Custom Post Type that outputs
117 * ConvertKit Plugin settings.
118 *
119 * @since 1.9.6.4
120 */
121 public function enqueue_styles() {
122
123 // Enqueue Select2 CSS.
124 convertkit_select2_enqueue_styles();
125
126 // Enqueue Post CSS.
127 wp_enqueue_style( 'convertkit-post', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/post.css', array(), CONVERTKIT_PLUGIN_VERSION );
128
129 /**
130 * Enqueue CSS for the Settings Screen at Settings > ConvertKit
131 *
132 * @since 1.9.6.4
133 */
134 do_action( 'convertkit_admin_post_enqueue_styles' );
135
136 }
137
138 /**
139 * Registers actions in the pre-publish actions section of the Publish metabox
140 * in the Classic Editor.
141 *
142 * @since 2.4.0
143 *
144 * @param WP_Post $post WordPress Post.
145 */
146 public function output_pre_publish_actions( $post ) {
147
148 // Bail if no actions registered.
149 $pre_publish_actions = convertkit_get_pre_publish_actions();
150 if ( ! count( $pre_publish_actions ) ) {
151 return;
152 }
153
154 // Bail if Post is not a supported Post Type.
155 if ( get_post_type( $post ) !== 'post' ) {
156 return;
157 }
158
159 // Bail if Post is not a draft.
160 if ( ! in_array( $post->post_status, array( 'draft', 'auto-draft' ), true ) ) {
161 return;
162 }
163
164 // Load pre-publish actions view.
165 include CONVERTKIT_PLUGIN_PATH . '/views/backend/post/pre-publish-actions.php';
166
167 }
168
169 /**
170 * Adds a meta box for the given Post Type.
171 *
172 * @since 1.9.6
173 *
174 * @param string $post_type Post Type.
175 */
176 public function add_meta_boxes( $post_type ) {
177
178 // Don't register the meta box if this Post Type isn't supported.
179 $supported_post_types = convertkit_get_supported_post_types();
180 if ( ! in_array( $post_type, $supported_post_types, true ) ) {
181 return;
182 }
183
184 // Register Meta Box.
185 add_meta_box( 'wp-convertkit-meta-box', __( 'ConvertKit', 'convertkit' ), array( $this, 'display_meta_box' ), $post_type, 'normal' );
186
187 // Enqueue JS and CSS.
188 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
189 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
190
191 }
192
193 /**
194 * Outputs the meta box.
195 *
196 * @since 1.9.6
197 *
198 * @param WP_Post $post The Post being edited.
199 */
200 public function display_meta_box( $post ) {
201
202 // Don't register the meta box if this Post is the blog archive page.
203 if ( $post->ID === get_option( 'page_for_posts' ) ) {
204 return;
205 }
206
207 // Show a warning if the API credentials haven't been set.
208 $settings = new ConvertKit_Settings();
209 if ( ! $settings->has_access_and_refresh_token() ) {
210 $post_type = get_post_type_object( $post->post_type );
211 $api = new ConvertKit_API_V4( CONVERTKIT_OAUTH_CLIENT_ID, CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI );
212 include CONVERTKIT_PLUGIN_PATH . '/views/backend/post/no-api-key.php';
213 return;
214 }
215
216 // Initialize Restrict Content Settings class.
217 $restrict_content_settings = new ConvertKit_Settings_Restrict_Content();
218
219 // Fetch Post Settings, Forms, Landing Pages and Tags.
220 $convertkit_post = new ConvertKit_Post( $post->ID );
221 $convertkit_forms = new ConvertKit_Resource_Forms();
222 $convertkit_landing_pages = new ConvertKit_Resource_Landing_Pages();
223 $convertkit_products = new ConvertKit_Resource_Products();
224 $convertkit_tags = new ConvertKit_Resource_Tags();
225
226 // Get settings page link.
227 $settings_link = convertkit_get_settings_link();
228
229 // Load metabox view.
230 include CONVERTKIT_PLUGIN_PATH . '/views/backend/post/meta-box.php';
231
232 }
233
234 /**
235 * Saves Post Settings when either editing a Post/Page or using the Quick Edit functionality.
236 *
237 * @since 1.9.6
238 *
239 * @param int $post_id Post ID.
240 */
241 public function save_post_meta( $post_id ) {
242
243 // Bail if this is an autosave.
244 if ( wp_is_post_autosave( $post_id ) ) {
245 return;
246 }
247
248 // Bail if this is a post revision.
249 if ( wp_is_post_revision( $post_id ) ) {
250 return;
251 }
252
253 // Bail if no nonce field exists.
254 if ( ! isset( $_POST['wp-convertkit-save-meta-nonce'] ) ) {
255 return;
256 }
257
258 // Bail if the nonce verification fails.
259 if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['wp-convertkit-save-meta-nonce'] ) ), 'wp-convertkit-save-meta' ) ) {
260 return;
261 }
262
263 // Bail if no ConvertKit settings were posted.
264 if ( ! isset( $_POST['wp-convertkit'] ) ) {
265 return;
266 }
267
268 // Save Post's settings.
269 $this->save_post_settings( $post_id, $_POST['wp-convertkit'] );
270
271 }
272
273 /**
274 * Saves the Post's settings submitted via $_POST or $_REQUEST.
275 * Can be used across Edit, Quick Edit and Bulk Edit.
276 *
277 * @since 1.9.8.0
278 *
279 * @param int $post_id Post ID.
280 * @param array $settings Settings.
281 */
282 public function save_post_settings( $post_id, $settings ) {
283
284 // Get Post's settings.
285 $convertkit_post = new ConvertKit_Post( $post_id );
286 $meta = $convertkit_post->get();
287
288 // Update Post's setting values if they were included in the $_POST data.
289 // Some values may not be included in the $_POST data e.g. if Quick Edit is used and no Landing Page was specified,
290 // in which case the existing Post's value will be used.
291 // This ensures settings are not deleted by accident.
292 foreach ( $meta as $key => $existing_value ) {
293 // Skip if this setting isn't included in the $_POST data.
294 if ( ! isset( $settings[ $key ] ) ) {
295 continue;
296 }
297
298 // Sanitize value.
299 $new_value = sanitize_text_field( wp_unslash( $settings[ $key ] ) );
300
301 // Skip if the setting value is -2, as this means it's a Bulk Edit request and this setting
302 // is set as 'No Change'.
303 if ( $new_value == '-2' ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
304 continue;
305 }
306
307 // Update setting using posted value.
308 $meta[ $key ] = $new_value;
309 }
310
311 // Save settings.
312 $convertkit_post->save( $meta );
313
314 // If a Form or Landing Page was specified, request a review.
315 // This can safely be called multiple times, as the review request
316 // class will ensure once a review request is dismissed by the user,
317 // it is never displayed again.
318 if ( $meta['form'] || $meta['landing_page'] ) {
319 WP_ConvertKit()->get_class( 'review_request' )->request_review();
320 }
321
322 }
323
324 /**
325 * Get the current post type based on the screen that is viewed.
326 *
327 * @since 2.5.5
328 *
329 * @return bool|string
330 */
331 private function get_current_post_type() {
332
333 // Bail if we cannot determine the screen.
334 if ( ! function_exists( 'get_current_screen' ) ) {
335 return false;
336 }
337
338 // Get screen.
339 $screen = get_current_screen();
340
341 // Return post type.
342 return $screen->post_type;
343
344 }
345
346 }
347