PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.4.4
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.4.4
3.4.4 3.4.3 3.4.2 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 All 197 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.4, at includes/blocks/class-convertkit-block-content.php

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