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

324 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 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 // Assign Post's Settings to the object.
77 $this->settings = $meta;
78
79 }
80
81 /**
82 * Returns settings for the Post.
83 *
84 * @since 1.9.6
85 *
86 * @return array
87 */
88 public function get() {
89
90 return $this->settings;
91
92 }
93
94 /**
95 * Returns the form setting for the Post.
96 *
97 * @since 1.9.6
98 *
99 * @return int
100 */
101 public function get_form() {
102
103 return $this->settings['form'];
104
105 }
106
107 /**
108 * Returns the landing page setting for the Post.
109 *
110 * @since 1.9.6
111 *
112 * @return int
113 */
114 public function get_landing_page() {
115
116 return $this->settings['landing_page'];
117
118 }
119
120 /**
121 * Returns the tag setting for the Post.
122 *
123 * @since 1.9.6
124 *
125 * @return int
126 */
127 public function get_tag() {
128
129 return $this->settings['tag'];
130
131 }
132
133 /**
134 * Returns the restrict content setting for the Post, which might be a
135 * Form, Tag or Product.
136 *
137 * @since 2.1.0
138 *
139 * @return string
140 */
141 public function get_restrict_content() {
142
143 return $this->settings['restrict_content'];
144
145 }
146
147 /**
148 * Returns the restrict content setting's resource type.
149 *
150 * @since 2.1.0
151 *
152 * @return string
153 */
154 public function get_restrict_content_type() {
155
156 // No type if restrict content isn't enabled on this Post.
157 if ( ! $this->restrict_content_enabled() ) {
158 return '';
159 }
160
161 // No type if the setting isn't of the expected format resource_ID.
162 if ( strpos( $this->settings['restrict_content'], '_' ) === false ) {
163 return '';
164 }
165
166 // Extract resource type from the setting.
167 list( $resource_type, $resource_id ) = explode( '_', $this->settings['restrict_content'] );
168
169 // Return resource type.
170 return $resource_type;
171
172 }
173
174 /**
175 * Returns the restrict content setting's resource ID.
176 *
177 * @since 2.1.0
178 *
179 * @return bool|int
180 */
181 public function get_restrict_content_id() {
182
183 // No ID if restrict content isn't enabled on this Post.
184 if ( ! $this->restrict_content_enabled() ) {
185 return false;
186 }
187
188 // No ID if the setting isn't of the expected format resource_ID.
189 if ( strpos( $this->settings['restrict_content'], '_' ) === false ) {
190 return false;
191 }
192
193 // Extract resource ID from the setting.
194 list( $resource_type, $resource_id ) = explode( '_', $this->settings['restrict_content'] );
195
196 // Return resource ID.
197 return (int) $resource_id;
198
199 }
200
201 /**
202 * Whether the Post has a ConvertKit Form defined.
203 *
204 * @since 1.9.6
205 *
206 * @return bool
207 */
208 public function has_form() {
209
210 return ( $this->settings['form'] > 0 );
211
212 }
213
214 /**
215 * Whether the Post is set to use the Plugin's Default Form Setting.
216 *
217 * @since 1.9.6
218 *
219 * @return bool
220 */
221 public function uses_default_form() {
222
223 return ( $this->settings['form'] === '-1' );
224
225 }
226
227 /**
228 * Whether the Post's Form setting is set to 'None'
229 *
230 * @since 1.9.6
231 *
232 * @return bool
233 */
234 public function uses_no_form() {
235
236 return ( $this->settings['form'] === '0' );
237
238 }
239
240 /**
241 * Whether the Post has a ConvertKit Landing Page defined.
242 *
243 * @since 1.9.6
244 *
245 * @return bool
246 */
247 public function has_landing_page() {
248
249 return ! empty( $this->settings['landing_page'] );
250
251 }
252
253 /**
254 * Whether the Post has a ConvertKit Tag defined.
255 *
256 * @since 1.9.6
257 *
258 * @return bool
259 */
260 public function has_tag() {
261
262 return ! empty( $this->settings['tag'] );
263
264 }
265
266 /**
267 * Whether the Post has a ConvertKit Form, Tag or Product defined for restricted content.
268 *
269 * @since 2.0.0
270 *
271 * @return bool
272 */
273 public function restrict_content_enabled() {
274
275 return ! empty( $this->settings['restrict_content'] );
276
277 }
278
279 /**
280 * Saves Post settings to the Post.
281 *
282 * @since 1.9.6
283 *
284 * @param array $meta Settings.
285 * @return bool Post Meta was updated
286 */
287 public function save( $meta ) {
288
289 return update_post_meta( $this->post_id, self::POST_META_KEY, $meta );
290
291 }
292
293 /**
294 * The default settings, used to populate the Post's Settings when a Post
295 * has no Settings.
296 *
297 * @since 1.9.6
298 *
299 * @return array
300 */
301 public function get_default_settings() {
302
303 $defaults = array(
304 'form' => '-1', // -1: Plugin Default Form, 0: No Form, 1+: Specific Form ID on ConvertKit.
305 'landing_page' => '',
306 'tag' => '',
307 'restrict_content' => '',
308 );
309
310 /**
311 * The default settings, used to populate the Post's Settings when a Post has no Settings.
312 *
313 * @since 1.9.6
314 *
315 * @param array $defaults Default Settings.
316 */
317 $defaults = apply_filters( 'convertkit_post_get_default_settings', $defaults );
318
319 return $defaults;
320
321 }
322
323 }
324