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

285 lines 6.7 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 mixed 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 mixed 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_id = WP_ConvertKit()->get_class( 'output' )->get_subscriber_id_from_request();
207 if ( ! $subscriber_id ) {
208 if ( $settings->debug_enabled() ) {
209 return '<!-- ConvertKit Custom Content: Subscriber ID does not exist -->';
210 }
211
212 return '';
213 }
214
215 // Bail if the API hasn't been configured.
216 $settings = new ConvertKit_Settings();
217 if ( ! $settings->has_api_key_and_secret() ) {
218 if ( $settings->debug_enabled() ) {
219 return '<!-- ConvertKit Custom Content: No API Key and Secret -->';
220 }
221
222 return '';
223 }
224
225 // Initialize the API.
226 $api = new ConvertKit_API( $settings->get_api_key(), $settings->get_api_secret(), $settings->debug_enabled() );
227
228 // Get the subscriber's tags, to see if they subscribed to this tag.
229 $tags = $api->get_subscriber_tags( $subscriber_id );
230
231 // Bail if an error occured i.e. the subscriber has no tags.
232 if ( is_wp_error( $tags ) ) {
233 if ( $settings->debug_enabled() ) {
234 return '<!-- ConvertKit Custom Content: ' . $tags->get_error_message() . ' -->';
235 }
236
237 return '';
238 }
239
240 // Iterate through ConvertKit Tags to find a match.
241 foreach ( $tags as $tag ) {
242 // Skip if this ConvertKit Tag isn't the Tag specified in the block.
243 if ( absint( $tag['id'] ) !== absint( $atts['tag'] ) ) {
244 continue;
245 }
246
247 /**
248 * Filters the content in the ConvertKit Custom Content block/shortcode
249 * immediately before it is output.
250 *
251 * @since 1.9.6
252 *
253 * @param string $content Content
254 * @param array $atts Block / Shortcode Attributes
255 * @param int $subscriber_id ConvertKit Subscriber's ID
256 * @param array $tags ConvertKit Subscriber's Tags
257 * @param array $tag ConvertKit Subscriber's Tag that matches $atts['tag']
258 */
259 $content = apply_filters( 'convertkit_block_content_render', $content, $atts, $subscriber_id, $tags, $tag );
260
261 /**
262 * Backward compat. filter for < 1.9.6. Filters the content in the ConvertKit Custom Content block/shortcode
263 * immediately before it is output.
264 *
265 * @since 1.0.0
266 *
267 * @param string $content Content
268 * @param array $atts Block / Shortcode Attributes
269 */
270 $content = apply_filters( 'wp_convertkit_shortcode_custom_content', $content, $atts );
271
272 return $content;
273 }
274
275 // If here, the subscriber does not have the block's tag.
276 if ( $settings->debug_enabled() ) {
277 return '<!-- ConvertKit Custom Content: Subscriber does not have tag -->';
278 }
279
280 return '';
281
282 }
283
284 }
285