PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.2.2
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.2.2
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 2.3.1 2.3.2 All 195 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 2.2.2, at includes/blocks/class-convertkit-block-content.php

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