PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.2.5
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.2.5
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.2.5, at admin/class-convertkit-admin-post.php

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