PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / trunk
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages vtrunk
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 / includes / class-convertkit-post.php

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

337 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Post class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Class to read ConvertKit Settings for the given Post, Page or Custom Post.
11 *
12 * @since 1.9.6
13 */
14 class ConvertKit_Post {
15
16 /**
17 * Holds the Post Meta Key that stores ConvertKit settings on a per-Post basis
18 *
19 * @var string
20 */
21 const POST_META_KEY = '_wp_convertkit_post_meta';
22
23 /**
24 * Holds the Post ID
25 *
26 * @since 1.9.6
27 *
28 * @var int
29 */
30 public $post_id = 0;
31
32 /**
33 * Holds the Post's Settings
34 *
35 * @var bool|array
36 */
37 private $settings = false;
38
39 /**
40 * Constructor. Populates the settings based on the given Post ID.
41 *
42 * @since 1.9.6
43 *
44 * @param int $post_id Post ID.
45 */
46 public function __construct( $post_id ) {
47
48 // Assign Post's ID to the object.
49 $this->post_id = $post_id;
50
51 // Get Post Meta.
52 $meta = get_post_meta( $post_id, self::POST_META_KEY, true );
53
54 // If no settings exist, or the settings are a string, fallback to the default settings.
55 if ( ! $meta || is_string( $meta ) ) {
56 // Fallback to default settings.
57 $meta = $this->get_default_settings();
58
59 // Backward compat check for older Plugin version settings.
60 $old_value = intval( get_post_meta( $post_id, '_convertkit_convertkit_form', true ) );
61 if ( 0 !== $old_value ) {
62 $meta['form'] = $old_value;
63 }
64 }
65
66 // Iterate through default settings, assigning them to the Post Meta array if any keys are missing
67 // to guarantee that the Post Meta array has all expected key/value pairs.
68 // This covers upgrades from 1.4.6 and earlier that would not set e.g. landing_page and tag keys
69 // if no values existed.
70 foreach ( $this->get_default_settings() as $key => $value ) {
71 if ( ! array_key_exists( $key, $meta ) ) {
72 $meta[ $key ] = $value;
73 }
74 }
75
76 /**
77 * Programmatically define ConvertKit settings for an individual Post, overriding those defined in the
78 * meta box.
79 *
80 * @since 2.4.4
81 *
82 * @param array $meta Post Settings.
83 * @param int $post_id Post ID.
84 * @return array Post Settings.
85 */
86 $meta = apply_filters( 'convertkit_post_settings', $meta, $this->post_id );
87
88 // Assign Post's Settings to the object.
89 $this->settings = $meta;
90
91 }
92
93 /**
94 * Returns settings for the Post.
95 *
96 * @since 1.9.6
97 *
98 * @return array
99 */
100 public function get() {
101
102 return $this->settings;
103
104 }
105
106 /**
107 * Returns the form setting for the Post.
108 *
109 * @since 1.9.6
110 *
111 * @return int
112 */
113 public function get_form() {
114
115 return $this->settings['form'];
116
117 }
118
119 /**
120 * Returns the landing page setting for the Post.
121 *
122 * @since 1.9.6
123 *
124 * @return int
125 */
126 public function get_landing_page() {
127
128 return $this->settings['landing_page'];
129
130 }
131
132 /**
133 * Returns the tag setting for the Post.
134 *
135 * @since 1.9.6
136 *
137 * @return int
138 */
139 public function get_tag() {
140
141 return $this->settings['tag'];
142
143 }
144
145 /**
146 * Returns the restrict content setting for the Post, which might be a
147 * Form, Tag or Product.
148 *
149 * @since 2.1.0
150 *
151 * @return string
152 */
153 public function get_restrict_content() {
154
155 return $this->settings['restrict_content'];
156
157 }
158
159 /**
160 * Returns the restrict content setting's resource type.
161 *
162 * @since 2.1.0
163 *
164 * @return string
165 */
166 public function get_restrict_content_type() {
167
168 // No type if restrict content isn't enabled on this Post.
169 if ( ! $this->restrict_content_enabled() ) {
170 return '';
171 }
172
173 // No type if the setting isn't of the expected format resource_ID.
174 if ( strpos( $this->settings['restrict_content'], '_' ) === false ) {
175 return '';
176 }
177
178 // Extract resource type from the setting.
179 list( $resource_type, $resource_id ) = explode( '_', $this->settings['restrict_content'] );
180
181 // Return resource type.
182 return $resource_type;
183
184 }
185
186 /**
187 * Returns the restrict content setting's resource ID.
188 *
189 * @since 2.1.0
190 *
191 * @return bool|int
192 */
193 public function get_restrict_content_id() {
194
195 // No ID if restrict content isn't enabled on this Post.
196 if ( ! $this->restrict_content_enabled() ) {
197 return false;
198 }
199
200 // No ID if the setting isn't of the expected format resource_ID.
201 if ( strpos( $this->settings['restrict_content'], '_' ) === false ) {
202 return false;
203 }
204
205 // Extract resource ID from the setting.
206 list( $resource_type, $resource_id ) = explode( '_', $this->settings['restrict_content'] );
207
208 // Return resource ID.
209 return (int) $resource_id;
210
211 }
212
213 /**
214 * Whether the Post has a ConvertKit Form defined.
215 *
216 * @since 1.9.6
217 *
218 * @return bool
219 */
220 public function has_form() {
221
222 return ( $this->settings['form'] > 0 );
223
224 }
225
226 /**
227 * Whether the Post is set to use the Plugin's Default Form Setting.
228 *
229 * @since 1.9.6
230 *
231 * @return bool
232 */
233 public function uses_default_form() {
234
235 return ( $this->settings['form'] === '-1' );
236
237 }
238
239 /**
240 * Whether the Post's Form setting is set to 'None'
241 *
242 * @since 1.9.6
243 *
244 * @return bool
245 */
246 public function uses_no_form() {
247
248 return ( $this->settings['form'] === '0' );
249
250 }
251
252 /**
253 * Whether the Post has a ConvertKit Landing Page defined.
254 *
255 * @since 1.9.6
256 *
257 * @return bool
258 */
259 public function has_landing_page() {
260
261 return ! empty( $this->settings['landing_page'] );
262
263 }
264
265 /**
266 * Whether the Post has a ConvertKit Tag defined.
267 *
268 * @since 1.9.6
269 *
270 * @return bool
271 */
272 public function has_tag() {
273
274 return ! empty( $this->settings['tag'] );
275
276 }
277
278 /**
279 * Whether the Post has a ConvertKit Form, Tag or Product defined for restricted content.
280 *
281 * @since 2.0.0
282 *
283 * @return bool
284 */
285 public function restrict_content_enabled() {
286
287 return ! empty( $this->settings['restrict_content'] );
288
289 }
290
291 /**
292 * Saves Post settings to the Post.
293 *
294 * @since 1.9.6
295 *
296 * @param array $meta Settings.
297 * @return bool Post Meta was updated
298 */
299 public function save( $meta ) {
300
301 return update_post_meta( $this->post_id, self::POST_META_KEY, $meta );
302
303 }
304
305 /**
306 * The default settings, used to populate the Post's Settings when a Post
307 * has no Settings.
308 *
309 * @since 1.9.6
310 *
311 * @return array
312 */
313 public function get_default_settings() {
314
315 $defaults = array(
316 'form' => '-1', // -1: Plugin Default Form, 0: No Form, 1+: Specific Form ID on ConvertKit.
317 'landing_page' => '0', // 0: None, 1+: Specific Landing Page ID.
318 'tag' => '0', // 0: None, 1+: Specific Tag ID.
319 'restrict_content' => '0', // 0: None, form_<id> / tag_<id> / product_<id>: restrict to that resource.
320 );
321
322 /**
323 * The default settings, used to populate the Post's Settings when a Post has no Settings.
324 *
325 * @since 1.9.6
326 *
327 * @param array $defaults Default Settings.
328 * @param int $post_id Post ID.
329 */
330 $defaults = apply_filters( 'convertkit_post_get_default_settings', $defaults, $this->post_id );
331
332 return $defaults;
333
334 }
335
336 }
337