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