PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.3.3
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.3.3
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-post.php

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

262 lines 7.0 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 add_action( 'post_submitbox_misc_actions', array( $this, 'output_pre_publish_actions' ) );
27 add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
28 add_action( 'save_post', array( $this, 'save_post_meta' ) );
29
30 }
31
32 /**
33 * Enqueue JavaScript when editing a Page, Post or Custom Post Type that outputs
34 * ConvertKit Plugin settings.
35 *
36 * @since 1.9.6.4
37 */
38 public function enqueue_scripts() {
39
40 // Enqueue Select2 JS.
41 convertkit_select2_enqueue_scripts();
42
43 /**
44 * Enqueue JavaScript when editing a Page, Post or Custom Post Type that outputs
45 * ConvertKit Plugin settings.
46 *
47 * @since 1.9.6.4
48 */
49 do_action( 'convertkit_admin_post_enqueue_scripts' );
50
51 }
52
53 /**
54 * Enqueue CSS when editing a Page, Post or Custom Post Type that outputs
55 * ConvertKit Plugin settings.
56 *
57 * @since 1.9.6.4
58 */
59 public function enqueue_styles() {
60
61 // Enqueue Select2 CSS.
62 convertkit_select2_enqueue_styles();
63
64 // Enqueue Post CSS.
65 wp_enqueue_style( 'convertkit-post', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/post.css', array(), CONVERTKIT_PLUGIN_VERSION );
66
67 /**
68 * Enqueue CSS for the Settings Screen at Settings > ConvertKit
69 *
70 * @since 1.9.6.4
71 */
72 do_action( 'convertkit_admin_post_enqueue_styles' );
73
74 }
75
76 /**
77 * Registers actions in the pre-publish actions section of the Publish metabox
78 * in the Classic Editor.
79 *
80 * @since 2.4.0
81 *
82 * @param WP_Post $post WordPress Post.
83 */
84 public function output_pre_publish_actions( $post ) {
85
86 // Bail if no actions registered.
87 $pre_publish_actions = convertkit_get_pre_publish_actions();
88 if ( ! count( $pre_publish_actions ) ) {
89 return;
90 }
91
92 // Bail if Post is not a supported Post Type.
93 if ( get_post_type( $post ) !== 'post' ) {
94 return;
95 }
96
97 // Bail if Post is not a draft.
98 if ( ! in_array( $post->post_status, array( 'draft', 'auto-draft' ), true ) ) {
99 return;
100 }
101
102 // Load pre-publish actions view.
103 include CONVERTKIT_PLUGIN_PATH . '/views/backend/post/pre-publish-actions.php';
104
105 }
106
107 /**
108 * Adds a meta box for the given Post Type.
109 *
110 * @since 1.9.6
111 *
112 * @param string $post_type Post Type.
113 */
114 public function add_meta_boxes( $post_type ) {
115
116 // Don't register the meta box if this Post Type isn't supported.
117 $supported_post_types = convertkit_get_supported_post_types();
118 if ( ! in_array( $post_type, $supported_post_types, true ) ) {
119 return;
120 }
121
122 // Register Meta Box.
123 add_meta_box( 'wp-convertkit-meta-box', __( 'ConvertKit', 'convertkit' ), array( $this, 'display_meta_box' ), $post_type, 'normal' );
124
125 // Enqueue JS and CSS.
126 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
127 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
128
129 }
130
131 /**
132 * Outputs the meta box.
133 *
134 * @since 1.9.6
135 *
136 * @param WP_Post $post The Post being edited.
137 */
138 public function display_meta_box( $post ) {
139
140 // Don't register the meta box if this Post is the blog archive page.
141 if ( $post->ID === get_option( 'page_for_posts' ) ) {
142 return;
143 }
144
145 // Show a warning if the API credentials haven't been set.
146 $settings = new ConvertKit_Settings();
147 if ( ! $settings->has_api_key_and_secret() ) {
148 $post_type = get_post_type_object( $post->post_type );
149 include CONVERTKIT_PLUGIN_PATH . '/views/backend/post/no-api-key.php';
150 return;
151 }
152
153 // Initialize Restrict Content Settings class.
154 $restrict_content_settings = new ConvertKit_Settings_Restrict_Content();
155
156 // Fetch Post Settings, Forms, Landing Pages and Tags.
157 $convertkit_post = new ConvertKit_Post( $post->ID );
158 $convertkit_forms = new ConvertKit_Resource_Forms();
159 $convertkit_landing_pages = new ConvertKit_Resource_Landing_Pages();
160 $convertkit_products = new ConvertKit_Resource_Products();
161 $convertkit_tags = new ConvertKit_Resource_Tags();
162
163 // Get settings page link.
164 $settings_link = convertkit_get_settings_link();
165
166 // Load metabox view.
167 include CONVERTKIT_PLUGIN_PATH . '/views/backend/post/meta-box.php';
168
169 }
170
171 /**
172 * Saves Post Settings when either editing a Post/Page or using the Quick Edit functionality.
173 *
174 * @since 1.9.6
175 *
176 * @param int $post_id Post ID.
177 */
178 public function save_post_meta( $post_id ) {
179
180 // Bail if this is an autosave.
181 if ( wp_is_post_autosave( $post_id ) ) {
182 return;
183 }
184
185 // Bail if this is a post revision.
186 if ( wp_is_post_revision( $post_id ) ) {
187 return;
188 }
189
190 // Bail if no nonce field exists.
191 if ( ! isset( $_POST['wp-convertkit-save-meta-nonce'] ) ) {
192 return;
193 }
194
195 // Bail if the nonce verification fails.
196 if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['wp-convertkit-save-meta-nonce'] ) ), 'wp-convertkit-save-meta' ) ) {
197 return;
198 }
199
200 // Bail if no ConvertKit settings were posted.
201 if ( ! isset( $_POST['wp-convertkit'] ) ) {
202 return;
203 }
204
205 // Save Post's settings.
206 $this->save_post_settings( $post_id, $_POST['wp-convertkit'] );
207
208 }
209
210 /**
211 * Saves the Post's settings submitted via $_POST or $_REQUEST.
212 * Can be used across Edit, Quick Edit and Bulk Edit.
213 *
214 * @since 1.9.8.0
215 *
216 * @param int $post_id Post ID.
217 * @param array $settings Settings.
218 */
219 public function save_post_settings( $post_id, $settings ) {
220
221 // Get Post's settings.
222 $convertkit_post = new ConvertKit_Post( $post_id );
223 $meta = $convertkit_post->get();
224
225 // Update Post's setting values if they were included in the $_POST data.
226 // Some values may not be included in the $_POST data e.g. if Quick Edit is used and no Landing Page was specified,
227 // in which case the existing Post's value will be used.
228 // This ensures settings are not deleted by accident.
229 foreach ( $meta as $key => $existing_value ) {
230 // Skip if this setting isn't included in the $_POST data.
231 if ( ! isset( $settings[ $key ] ) ) {
232 continue;
233 }
234
235 // Sanitize value.
236 $new_value = sanitize_text_field( wp_unslash( $settings[ $key ] ) );
237
238 // Skip if the setting value is -2, as this means it's a Bulk Edit request and this setting
239 // is set as 'No Change'.
240 if ( $new_value == '-2' ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
241 continue;
242 }
243
244 // Update setting using posted value.
245 $meta[ $key ] = $new_value;
246 }
247
248 // Save settings.
249 $convertkit_post->save( $meta );
250
251 // If a Form or Landing Page was specified, request a review.
252 // This can safely be called multiple times, as the review request
253 // class will ensure once a review request is dismissed by the user,
254 // it is never displayed again.
255 if ( $meta['form'] || $meta['landing_page'] ) {
256 WP_ConvertKit()->get_class( 'review_request' )->request_review();
257 }
258
259 }
260
261 }
262