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-member-content-login.php

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

404 lines 10.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Member Content Login Block class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * ConvertKit Member Content Login Block for Gutenberg and Shortcode.
11 *
12 * @package ConvertKit
13 * @author ConvertKit
14 */
15 class ConvertKit_Block_Member_Content_Login extends ConvertKit_Block {
16
17 /**
18 * Constructor
19 *
20 * @since 3.4.2
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 // Register this as a Gutenberg block in the ConvertKit Plugin.
28 add_filter( 'convertkit_blocks', array( $this, 'register' ) );
29
30 // Register this block's MCP abilities.
31 add_filter( 'convertkit_abilities', array( $this, 'register_abilities' ) );
32
33 // Enqueue styles for this Gutenberg Block in the editor and frontend views.
34 add_action( 'convertkit_gutenberg_enqueue_styles_editor_and_frontend', array( $this, 'enqueue_styles' ) );
35
36 }
37
38 /**
39 * Enqueues styles for this Gutenberg Block in the editor and frontend views.
40 *
41 * @since 3.4.2
42 */
43 public function enqueue_styles() {
44
45 convertkit_enqueue_frontend_css();
46
47 // Enqueue the block button CSS.
48 wp_enqueue_style( 'wp-block-button' );
49
50 }
51
52 /**
53 * Returns this block's programmatic name, excluding the convertkit- prefix.
54 *
55 * @since 3.4.2
56 */
57 public function get_name() {
58
59 /**
60 * This will register as:
61 * - a shortcode, with the name [convertkit_login].
62 * - a shortcode, with the name [kit_login].
63 * - a Gutenberg block, with the name convertkit/login.
64 */
65 return 'login';
66
67 }
68
69 /**
70 * Returns this block's title.
71 *
72 * @since 3.4.2
73 */
74 public function get_title() {
75
76 return __( 'Kit Member Content Login', 'convertkit' );
77
78 }
79
80 /**
81 * Returns this block's plural title.
82 *
83 * @since 3.4.2
84 *
85 * @return string
86 */
87 public function get_title_plural() {
88
89 return __( 'Kit Member Content Logins', 'convertkit' );
90
91 }
92
93 /**
94 * Returns this block's icon.
95 *
96 * @since 3.4.2
97 */
98 public function get_icon() {
99
100 return 'resources/backend/images/block-icon-login.svg';
101
102 }
103
104 /**
105 * Returns this block's Title, Icon, Categories, Keywords and properties.
106 *
107 * @since 3.4.2
108 */
109 public function get_overview() {
110
111 $settings = new ConvertKit_Settings();
112
113 return array(
114 'title' => $this->get_title(),
115 'description' => __( 'Displays a login form, so subscribers can log in to view Member Content.', 'convertkit' ),
116 'icon' => $this->get_icon(),
117 'category' => 'convertkit',
118 'keywords' => array(
119 __( 'ConvertKit', 'convertkit' ),
120 __( 'Kit', 'convertkit' ),
121 __( 'Member Content', 'convertkit' ),
122 __( 'Login', 'convertkit' ),
123 ),
124
125 // Function to call when rendering as a block or a shortcode on the frontend web site.
126 'render_callback' => array( $this, 'render' ),
127
128 // Shortcode: TinyMCE / QuickTags Modal Width and Height.
129 'modal' => array(
130 'width' => 500,
131 'height' => 305,
132 ),
133
134 // Shortcode: Include a closing [/shortcode] tag when using TinyMCE or QuickTag Modals.
135 'shortcode_include_closing_tag' => false,
136
137 // Gutenberg: Block Icon in Editor.
138 'gutenberg_icon' => convertkit_get_file_contents( CONVERTKIT_PLUGIN_PATH . '/resources/backend/images/block-icon-login.svg' ),
139
140 // Help descriptions, displayed when no API key / resources exist and this block/shortcode is added.
141 'no_access_token' => array(
142 'notice' => __( 'Not connected to Kit.', 'convertkit' ),
143 'link' => convertkit_get_setup_wizard_plugin_link(),
144 'link_text' => __( 'Click here to connect your Kit account.', 'convertkit' ),
145 'instruction_text' => __( 'Connect your Kit account at Settings > Kit, and then refresh this page.', 'convertkit' ),
146 ),
147
148 // Whether an Access Token exists in the Plugin.
149 'has_access_token' => $settings->has_access_and_refresh_token(),
150 'has_resources' => true,
151 );
152
153 }
154
155 /**
156 * Returns this block's Attributes
157 *
158 * @since 3.4.2
159 */
160 public function get_attributes() {
161
162 return array(
163 // Block attributes.
164 'logged_in_text' => array(
165 'type' => 'string',
166 'default' => $this->get_default_value( 'logged_in_text' ),
167 ),
168 'logout_button_label' => array(
169 'type' => 'string',
170 'default' => $this->get_default_value( 'logout_button_label' ),
171 ),
172
173 // The below are built in Gutenberg attributes registered in get_supports().
174
175 // get_supports() style, color and typography attributes.
176 'style' => array(
177 'type' => 'object',
178 ),
179 'backgroundColor' => array(
180 'type' => 'string',
181 ),
182 'textColor' => array(
183 'type' => 'string',
184 ),
185 'fontSize' => array(
186 'type' => 'string',
187 ),
188
189 // Always required for Gutenberg.
190 'is_gutenberg_example' => array(
191 'type' => 'boolean',
192 'default' => false,
193 ),
194 );
195
196 }
197
198 /**
199 * Returns this block's supported built-in Attributes.
200 *
201 * @since 3.4.2
202 *
203 * @return array Supports
204 */
205 public function get_supports() {
206
207 return array(
208 'className' => true,
209 'color' => array(
210 'background' => true,
211 'text' => true,
212
213 // Don't apply styles to the block editor's div element.
214 // This ensures what's rendered in the Gutenberg editor matches the frontend output for styling.
215 // See: https://github.com/WordPress/gutenberg/issues/32417.
216 '__experimentalSkipSerialization' => true,
217 ),
218 'typography' => array(
219 'fontSize' => true,
220 'lineHeight' => true,
221 ),
222 'spacing' => array(
223 'margin' => true,
224 'padding' => true,
225 ),
226 );
227
228 }
229
230 /**
231 * Returns this block's Fields
232 *
233 * @since 3.4.2
234 *
235 * @return bool|array
236 */
237 public function get_fields() {
238
239 // The login form's text and labels are defined at Settings > Kit > Member Content,
240 // so that they match the login form displayed on Member Content. Only the logged in
241 // text and log out button label are defined here, as no equivalent settings exist.
242 return array(
243 'logged_in_text' => array(
244 'label' => __( 'Logged In Text', 'convertkit' ),
245 'type' => 'text',
246 'description' => __( 'The text to display when the subscriber is logged in.', 'convertkit' ),
247 ),
248 'logout_button_label' => array(
249 'label' => __( 'Log Out Button Text', 'convertkit' ),
250 'type' => 'text',
251 'description' => __( 'The text to display for the button to log the subscriber out.', 'convertkit' ),
252 ),
253
254 // These fields will only display on the shortcode, and are deliberately not registered in get_attributes(),
255 // because Gutenberg will register its own color pickers for background and text.
256 'background_color' => array(
257 'label' => __( 'Background color', 'convertkit' ),
258 'type' => 'color',
259 ),
260 'text_color' => array(
261 'label' => __( 'Text color', 'convertkit' ),
262 'type' => 'color',
263 ),
264 );
265
266 }
267
268 /**
269 * Returns this block's UI panels / sections.
270 *
271 * @since 3.4.2
272 *
273 * @return bool|array
274 */
275 public function get_panels() {
276
277 return array(
278 'general' => array(
279 'label' => __( 'General', 'convertkit' ),
280 'fields' => array(
281 'logged_in_text',
282 'logout_button_label',
283 'background_color',
284 'text_color',
285 ),
286 ),
287 );
288
289 }
290
291 /**
292 * Returns this block's Default Values
293 *
294 * @since 3.4.2
295 *
296 * @return array
297 */
298 public function get_default_values() {
299
300 return array(
301 'logged_in_text' => __( 'You are logged in.', 'convertkit' ),
302 'logout_button_label' => __( 'Log out', 'convertkit' ),
303 'background_color' => '',
304 'text_color' => '',
305
306 // Built-in Gutenberg block attributes.
307 'backgroundColor' => '',
308 'textColor' => '',
309 'fontSize' => '',
310 'style' => array(
311 'visualizers' => array(
312 'padding' => array(
313 'top' => '',
314 'bottom' => '',
315 'left' => '',
316 'right' => '',
317 ),
318 ),
319 ),
320 );
321
322 }
323
324 /**
325 * Returns the block's output, based on the supplied configuration attributes.
326 *
327 * @since 3.4.2
328 *
329 * @param array $atts Block / Shortcode / Page Builder Module Attributes.
330 * @return string
331 */
332 public function render( $atts ) {
333
334 // Parse attributes, defining fallback defaults if required
335 // and moving some attributes (such as Gutenberg's styles), if defined.
336 $atts = $this->sanitize_and_declare_atts( $atts );
337
338 // Setup Settings classes.
339 $settings = new ConvertKit_Settings();
340 $restrict_content_settings = new ConvertKit_Settings_Restrict_Content();
341
342 // Bail if the Plugin Access Token has not been configured.
343 if ( ! $settings->has_access_and_refresh_token() ) {
344 if ( $settings->debug_enabled() ) {
345 return '<!-- Kit Member Content Login: Not connected to Kit. -->';
346 }
347
348 return '';
349 }
350
351 // Define variables for the views.
352 $output_restrict_content = WP_ConvertKit()->get_class( 'output_restrict_content' );
353 $post_id = get_the_ID();
354 $css_classes = $this->get_css_classes();
355 $css_styles = $this->get_css_styles( $atts );
356
357 // Enqueue CSS and JS.
358 $output_restrict_content->enqueue_scripts_and_styles();
359
360 // If the subscriber is logged in, output the log out button.
361 $subscriber = new ConvertKit_Subscriber();
362 $subscriber_id = $subscriber->get_subscriber_id();
363 if ( ! is_wp_error( $subscriber_id ) && $subscriber_id ) {
364 $logout_url = add_query_arg(
365 array(
366 'convertkit_logout' => 1,
367 '_wpnonce' => wp_create_nonce( 'convertkit_member_content_logout' ),
368 ),
369 get_permalink( $post_id )
370 );
371
372 ob_start();
373 include CONVERTKIT_PLUGIN_PATH . '/views/frontend/restrict-content/member-content-logged-in.php';
374 return trim( ob_get_clean() );
375 }
376
377 // If the subscriber submitted their email address, output the code form.
378 if ( $output_restrict_content->token !== false ) {
379 ob_start();
380 include CONVERTKIT_PLUGIN_PATH . '/views/frontend/restrict-content/code.php';
381 return trim( ob_get_clean() );
382 }
383
384 // Output.
385 ob_start();
386 include CONVERTKIT_PLUGIN_PATH . '/views/frontend/restrict-content/member-content-login.php';
387 $html = trim( ob_get_clean() );
388
389 /**
390 * Filter the block's content immediately before it is output.
391 *
392 * @since 3.4.2
393 *
394 * @param string $html Kit Member Content Login HTML.
395 * @param array $atts Block Attributes.
396 */
397 $html = apply_filters( 'convertkit_block_member_content_login_render', $html, $atts );
398
399 return $html;
400
401 }
402
403 }
404