PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.4.1
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.4.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 / blocks / class-convertkit-block-content.php

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

347 lines 7.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Custom Content Block class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * ConvertKit Custom Content Block for Gutenberg and Shortcode.
11 *
12 * @package ConvertKit
13 * @author ConvertKit
14 */
15 class ConvertKit_Block_Content extends ConvertKit_Block {
16
17 /**
18 * Constructor
19 *
20 * @since 1.9.6
21 */
22 public function __construct() {
23
24 // Register this as a shortcode in the ConvertKit Plugin.
25 add_filter( 'convertkit_shortcodes', array( $this, 'register' ) );
26
27 }
28
29 /**
30 * Returns this block's programmatic name, excluding the convertkit- prefix.
31 *
32 * @since 1.9.6
33 *
34 * @return string
35 */
36 public function get_name() {
37
38 /**
39 * This will register as:
40 * - a shortcode, with the name [convertkit_content]
41 * - a Gutenberg block, with the name convertkit/content
42 */
43 return 'content';
44
45 }
46
47 /**
48 * Returns this block's title.
49 *
50 * @since 3.1.1
51 */
52 public function get_title() {
53
54 return __( 'Kit Custom Content', 'convertkit' );
55
56 }
57
58 /**
59 * Returns this block's icon.
60 *
61 * @since 3.1.1
62 */
63 public function get_icon() {
64
65 return 'resources/backend/images/block-icon-content.svg';
66
67 }
68
69 /**
70 * Returns this block's Title, Icon, Categories, Keywords and properties.
71 *
72 * @since 1.9.6
73 *
74 * @return array
75 */
76 public function get_overview() {
77
78 return array(
79 'title' => $this->get_title(),
80 'description' => __( 'Displays Kit Custom Content for a subscriber if their tag matches the Page\'s tag.', 'convertkit' ),
81 'icon' => $this->get_icon(),
82 'category' => 'convertkit',
83 'keywords' => array(
84 __( 'ConvertKit', 'convertkit' ),
85 __( 'Kit', 'convertkit' ),
86 __( 'Content', 'convertkit' ),
87 ),
88
89 // TinyMCE / QuickTags Modal Width and Height.
90 'modal' => array(
91 'width' => 500,
92 'height' => 106,
93 ),
94
95 'shortcode_include_closing_tag' => true,
96
97 // Function to call when rendering the block/shortcode on the frontend web site.
98 'render_callback' => array( $this, 'render' ),
99 );
100
101 }
102
103 /**
104 * Returns this block's Attributes
105 *
106 * @since 1.9.6.5
107 *
108 * @return array
109 */
110 public function get_attributes() {
111
112 return array(
113 'tag' => array(
114 'type' => 'string',
115 ),
116 );
117
118 }
119
120 /**
121 * Returns this block's Fields
122 *
123 * @since 1.9.6
124 *
125 * @return bool|array
126 */
127 public function get_fields() {
128
129 // Get ConvertKit Tags.
130 $tags = array();
131 $convertkit_tags = new ConvertKit_Resource_Tags();
132 if ( $convertkit_tags->exist() ) {
133 foreach ( $convertkit_tags->get() as $tag ) {
134 $tags[ absint( $tag['id'] ) ] = sanitize_text_field( $tag['name'] );
135 }
136 }
137
138 return array(
139 'tag' => array(
140 'label' => __( 'Tag', 'convertkit' ),
141 'type' => 'select',
142 'values' => $tags,
143 ),
144 );
145
146 }
147
148 /**
149 * Returns this block's UI panels / sections.
150 *
151 * @since 1.9.6
152 *
153 * @return bool|array
154 */
155 public function get_panels() {
156
157 return array(
158 'general' => array(
159 'label' => __( 'General', 'convertkit' ),
160 'fields' => array(
161 'tag',
162 ),
163 ),
164 );
165
166 }
167
168 /**
169 * Returns this block's Default Values
170 *
171 * @since 1.9.6
172 *
173 * @return array
174 */
175 public function get_default_values() {
176
177 return array(
178 'tag' => '',
179 );
180
181 }
182
183 /**
184 * Returns the block's output, based on the supplied configuration attributes.
185 *
186 * @since 1.9.6
187 *
188 * @param array $atts Block / Shortcode Attributes.
189 * @param string $content Content.
190 * @return string Output
191 */
192 public function render( $atts, $content = '' ) {
193
194 // Bail if the request is not for the frontend site.
195 if ( is_admin() ) {
196 return '';
197 }
198
199 // Parse shortcode attributes, defining fallback defaults if required.
200 $atts = shortcode_atts(
201 $this->get_default_values(),
202 $this->sanitize_atts( $atts ),
203 $this->get_name()
204 );
205
206 // Setup Settings class.
207 $settings = new ConvertKit_Settings();
208
209 // Bail if the tag isn't specified.
210 if ( ! $atts['tag'] ) {
211 if ( $settings->debug_enabled() ) {
212 return '<!-- Kit Custom Content: No Tag Specified -->';
213 }
214
215 return '';
216 }
217
218 // Bail if there is no subscriber ID from the cookie or request.
219 $subscriber = new ConvertKit_Subscriber();
220 $subscriber_id = $subscriber->get_subscriber_id();
221 if ( is_wp_error( $subscriber_id ) ) {
222 if ( $settings->debug_enabled() ) {
223 return '<!-- Kit Custom Content: Subscriber ID Error: ' . $subscriber_id->get_error_message() . ' -->';
224 }
225
226 return '';
227 }
228 if ( ! $subscriber_id ) {
229 if ( $settings->debug_enabled() ) {
230 return '<!-- Kit Custom Content: Subscriber ID does not exist -->';
231 }
232
233 return '';
234 }
235
236 // Bail if the API hasn't been configured.
237 $settings = new ConvertKit_Settings();
238 if ( ! $settings->has_access_and_refresh_token() ) {
239 if ( $settings->debug_enabled() ) {
240 return '<!-- Kit Custom Content: No Access Token -->';
241 }
242
243 return '';
244 }
245
246 // Initialize the API.
247 $api = new ConvertKit_API_V4(
248 CONVERTKIT_OAUTH_CLIENT_ID,
249 CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI,
250 $settings->get_access_token(),
251 $settings->get_refresh_token(),
252 $settings->debug_enabled(),
253 'output_content'
254 );
255
256 // Get the subscriber's tags, to see if they subscribed to this tag.
257 if ( is_numeric( $subscriber_id ) ) {
258 $tags = $api->get_subscriber_tags( $subscriber_id );
259
260 // Bail if an error occurred.
261 if ( is_wp_error( $tags ) ) {
262 if ( $settings->debug_enabled() ) {
263 return '<!-- Kit Custom Content: ' . $tags->get_error_message() . ' -->';
264 }
265
266 return '';
267 }
268 } else {
269 // Subscriber ID is a signed subscriber ID.
270 // Get tags that the subscriber has access to via the profile() method.
271 $result = $api->profile( $subscriber_id );
272
273 // If an error occurred, the subscriber ID is invalid.
274 if ( is_wp_error( $result ) ) {
275 if ( $settings->debug_enabled() ) {
276 return '<!-- Kit Custom Content: ' . $result->get_error_message() . ' -->';
277 }
278
279 return '';
280 }
281
282 // Build tags array.
283 $tags = array(
284 'tags' => array(),
285 );
286 foreach ( $result['tags'] as $tag_id ) {
287 $tags['tags'][] = array(
288 'id' => $tag_id,
289 );
290 }
291 }
292
293 // Bail if the subscriber has no tags.
294 if ( ! count( $tags ) ) {
295 if ( $settings->debug_enabled() ) {
296 return '<!-- Kit Custom Content: Subscriber has no tags -->';
297 }
298
299 return '';
300 }
301
302 // Iterate through ConvertKit Tags to find a match.
303 foreach ( $tags['tags'] as $tag ) {
304 // Skip if this ConvertKit Tag isn't the Tag specified in the block.
305 if ( absint( $tag['id'] ) !== absint( $atts['tag'] ) ) {
306 continue;
307 }
308
309 /**
310 * Filters the content in the ConvertKit Custom Content block/shortcode
311 * immediately before it is output.
312 *
313 * @since 1.9.6
314 *
315 * @param string $content Content
316 * @param array $atts Block / Shortcode Attributes
317 * @param int $subscriber_id ConvertKit Subscriber's ID
318 * @param array $tags ConvertKit Subscriber's Tags
319 * @param array $tag ConvertKit Subscriber's Tag that matches $atts['tag']
320 */
321 $content = apply_filters( 'convertkit_block_content_render', $content, $atts, $subscriber_id, $tags, $tag );
322
323 /**
324 * Backward compat. filter for < 1.9.6. Filters the content in the ConvertKit Custom Content block/shortcode
325 * immediately before it is output.
326 *
327 * @since 1.0.0
328 *
329 * @param string $content Content
330 * @param array $atts Block / Shortcode Attributes
331 */
332 $content = apply_filters( 'wp_convertkit_shortcode_custom_content', $content, $atts );
333
334 return $content;
335 }
336
337 // If here, the subscriber does not have the block's tag.
338 if ( $settings->debug_enabled() ) {
339 return '<!-- Kit Custom Content: Subscriber does not have tag -->';
340 }
341
342 return '';
343
344 }
345
346 }
347