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