← All changes
|
includes/blocks/class-convertkit-block-broadcasts.php
+235
-89
2.2.6
→
3.4.5
View file →
| @@ -26,30 +26,87 @@ | ||
| 26 | 26 | |
| 27 | 27 | // Register this as a Gutenberg block in the ConvertKit Plugin. |
| 28 | 28 | add_filter( 'convertkit_blocks', array( $this, 'register' ) ); |
| 29 | 29 | |
| 30 | - // Enqueue scripts and styles for this Gutenberg Block in the editor view. | |
| 31 | - add_action( 'convertkit_gutenberg_enqueue_scripts', array( $this, 'enqueue_scripts_editor' ) ); | |
| 30 | + // Register this block's MCP abilities. | |
| 31 | + add_filter( 'convertkit_abilities', array( $this, 'register_abilities' ) ); | |
| 32 | 32 | |
| 33 | 33 | // Enqueue scripts and styles for this Gutenberg Block in the editor and frontend views. |
| 34 | 34 | add_action( 'convertkit_gutenberg_enqueue_scripts_editor_and_frontend', array( $this, 'enqueue_scripts' ) ); |
| 35 | 35 | add_action( 'convertkit_gutenberg_enqueue_styles_editor_and_frontend', array( $this, 'enqueue_styles' ) ); |
| 36 | 36 | |
| 37 | - // Render Broadcasts block via AJAX. | |
| 38 | - add_action( 'wp_ajax_nopriv_convertkit_broadcasts_render', array( $this, 'render_ajax' ) ); | |
| 39 | - add_action( 'wp_ajax_convertkit_broadcasts_render', array( $this, 'render_ajax' ) ); | |
| 37 | + // Register REST API routes. | |
| 38 | + add_action( 'rest_api_init', array( $this, 'register_routes' ) ); | |
| 40 | 39 | |
| 41 | 40 | } |
| 42 | 41 | |
| 43 | 42 | /** |
| 44 | - * Enqueues scripts for this Gutenberg Block in the editor view. | |
| 43 | + * Register REST API routes. | |
| 45 | 44 | * |
| 46 | - * @since 2.0.1 | |
| 45 | + * @since 3.1.8 | |
| 47 | 46 | */ |
| 48 | - public function enqueue_scripts_editor() { | |
| 47 | + public function register_routes() { | |
| 49 | 48 | |
| 50 | - wp_enqueue_script( 'convertkit-gutenberg-block-broadcasts', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg-block-broadcasts.js', array( 'convertkit-gutenberg' ), CONVERTKIT_PLUGIN_VERSION, true ); | |
| 49 | + register_rest_route( | |
| 50 | + 'kit/v1', | |
| 51 | + '/broadcasts', | |
| 52 | + array( | |
| 53 | + 'methods' => WP_REST_Server::READABLE, | |
| 54 | + 'args' => array( | |
| 55 | + 'date_format' => array( | |
| 56 | + 'default' => $this->get_default_value( 'date_format' ), | |
| 57 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 58 | + ), | |
| 59 | + 'display_image' => array( | |
| 60 | + 'default' => $this->get_default_value( 'display_image' ), | |
| 61 | + 'sanitize_callback' => 'absint', | |
| 62 | + ), | |
| 63 | + 'display_description' => array( | |
| 64 | + 'default' => $this->get_default_value( 'display_description' ), | |
| 65 | + 'sanitize_callback' => 'absint', | |
| 66 | + ), | |
| 67 | + 'display_read_more' => array( | |
| 68 | + 'default' => $this->get_default_value( 'display_read_more' ), | |
| 69 | + 'sanitize_callback' => 'absint', | |
| 70 | + ), | |
| 71 | + 'read_more_label' => array( | |
| 72 | + 'default' => $this->get_default_value( 'read_more_label' ), | |
| 73 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 74 | + ), | |
| 75 | + 'limit' => array( | |
| 76 | + 'default' => $this->get_default_value( 'limit' ), | |
| 77 | + 'sanitize_callback' => 'absint', | |
| 78 | + ), | |
| 79 | + 'page' => array( | |
| 80 | + 'default' => $this->get_default_value( 'page' ), | |
| 81 | + 'sanitize_callback' => 'absint', | |
| 82 | + ), | |
| 83 | + 'paginate' => array( | |
| 84 | + 'default' => $this->get_default_value( 'paginate' ), | |
| 85 | + 'sanitize_callback' => 'absint', | |
| 86 | + ), | |
| 87 | + 'paginate_label_next' => array( | |
| 88 | + 'default' => $this->get_default_value( 'paginate_label_next' ), | |
| 89 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 90 | + ), | |
| 91 | + 'paginate_label_prev' => array( | |
| 92 | + 'default' => $this->get_default_value( 'paginate_label_prev' ), | |
| 93 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 94 | + ), | |
| 95 | + 'link_color' => array( | |
| 96 | + 'default' => $this->get_default_value( 'link_color' ), | |
| 97 | + 'sanitize_callback' => 'sanitize_text_field', | |
| 98 | + ), | |
| 99 | + ), | |
| 100 | + 'callback' => function ( $request ) { | |
| 101 | + return rest_ensure_response( $this->render_ajax( $request ) ); | |
| 102 | + }, | |
| 51 | 103 | |
| 104 | + // No authentication required, as this is on the frontend site. | |
| 105 | + 'permission_callback' => '__return_true', | |
| 106 | + ) | |
| 107 | + ); | |
| 108 | + | |
| 52 | 109 | } |
| 53 | 110 | |
| 54 | 111 | /** |
| 55 | 112 | * Enqueues scripts for this Gutenberg Block in the editor and frontend views. |
| @@ -60,23 +117,24 @@ | ||
| 60 | 117 | |
| 61 | 118 | // Get ConvertKit Settings. |
| 62 | 119 | $settings = new ConvertKit_Settings(); |
| 63 | 120 | |
| 64 | - wp_enqueue_script( 'convertkit-' . $this->get_name(), CONVERTKIT_PLUGIN_URL . 'resources/frontend/js/broadcasts.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true ); | |
| 121 | + // Enqueue frontend JS. | |
| 122 | + convertkit_enqueue_frontend_js(); | |
| 123 | + | |
| 124 | + // Define variables. | |
| 65 | 125 | wp_localize_script( |
| 66 | - 'convertkit-' . $this->get_name(), | |
| 126 | + 'convertkit-js', | |
| 67 | 127 | 'convertkit_broadcasts', |
| 68 | 128 | array( |
| 69 | - // WordPress AJAX URL endpoint. | |
| 70 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), | |
| 129 | + // REST API URL endpoint. | |
| 130 | + 'ajax_url' => rest_url( 'kit/v1/broadcasts' ), | |
| 71 | 131 | |
| 72 | - // AJAX action registered in __construct(). | |
| 73 | - 'action' => 'convertkit_broadcasts_render', | |
| 74 | - | |
| 75 | 132 | // Whether debugging is enabled. |
| 76 | 133 | 'debug' => $settings->debug_enabled(), |
| 77 | 134 | ) |
| 78 | 135 | ); |
| 136 | + | |
| 79 | 137 | } |
| 80 | 138 | |
| 81 | 139 | /** |
| 82 | 140 | * Enqueues styles for this Gutenberg Block in the editor and frontend views. |
| @@ -84,9 +142,9 @@ | ||
| 84 | 142 | * @since 1.9.7.4 |
| 85 | 143 | */ |
| 86 | 144 | public function enqueue_styles() { |
| 87 | 145 | |
| 88 | - wp_enqueue_style( 'convertkit-' . $this->get_name(), CONVERTKIT_PLUGIN_URL . 'resources/frontend/css/broadcasts.css', array(), CONVERTKIT_PLUGIN_VERSION ); | |
| 146 | + convertkit_enqueue_frontend_css(); | |
| 89 | 147 | |
| 90 | 148 | } |
| 91 | 149 | |
| 92 | 150 | /** |
| @@ -98,8 +156,9 @@ | ||
| 98 | 156 | |
| 99 | 157 | /** |
| 100 | 158 | * This will register as: |
| 101 | 159 | * - a shortcode, with the name [convertkit_broadcasts]. |
| 160 | + * - a shortcode, with the name [kit_broadcasts]. | |
| 102 | 161 | * - a Gutenberg block, with the name convertkit/broadcasts. |
| 103 | 162 | */ |
| 104 | 163 | return 'broadcasts'; |
| 105 | 164 | |
| @@ -105,8 +164,43 @@ | ||
| 105 | 164 | |
| 106 | 165 | } |
| 107 | 166 | |
| 108 | 167 | /** |
| 168 | + * Returns this block's title. | |
| 169 | + * | |
| 170 | + * @since 3.1.1 | |
| 171 | + */ | |
| 172 | + public function get_title() { | |
| 173 | + | |
| 174 | + return __( 'Kit Broadcasts', 'convertkit' ); | |
| 175 | + | |
| 176 | + } | |
| 177 | + | |
| 178 | + /** | |
| 179 | + * Returns this block's plural title. | |
| 180 | + * | |
| 181 | + * @since 3.4.0 | |
| 182 | + * | |
| 183 | + * @return string | |
| 184 | + */ | |
| 185 | + public function get_title_plural() { | |
| 186 | + | |
| 187 | + return __( 'Kit Broadcasts', 'convertkit' ); | |
| 188 | + | |
| 189 | + } | |
| 190 | + | |
| 191 | + /** | |
| 192 | + * Returns this block's icon. | |
| 193 | + * | |
| 194 | + * @since 3.1.1 | |
| 195 | + */ | |
| 196 | + public function get_icon() { | |
| 197 | + | |
| 198 | + return 'resources/backend/images/block-icon-broadcasts.svg'; | |
| 199 | + | |
| 200 | + } | |
| 201 | + | |
| 202 | + /** | |
| 109 | 203 | * Returns this block's Title, Icon, Categories, Keywords and properties. |
| 110 | 204 | * |
| 111 | 205 | * @since 1.9.7.4 |
| 112 | 206 | */ |
| @@ -116,56 +210,55 @@ | ||
| 116 | 210 | $posts = new ConvertKit_Resource_Posts( 'output_broadcasts' ); |
| 117 | 211 | $settings = new ConvertKit_Settings(); |
| 118 | 212 | |
| 119 | 213 | return array( |
| 120 | - 'title' => __( 'ConvertKit Broadcasts', 'convertkit' ), | |
| 121 | - 'description' => __( 'Displays a list of your ConvertKit broadcasts.', 'convertkit' ), | |
| 122 | - 'icon' => 'resources/backend/images/block-icon-broadcasts.svg', | |
| 123 | - 'category' => 'convertkit', | |
| 124 | - 'keywords' => array( | |
| 214 | + 'title' => $this->get_title(), | |
| 215 | + 'description' => __( 'Displays a list of your Kit broadcasts.', 'convertkit' ), | |
| 216 | + 'icon' => $this->get_icon(), | |
| 217 | + 'category' => 'convertkit', | |
| 218 | + 'keywords' => array( | |
| 125 | 219 | __( 'ConvertKit', 'convertkit' ), |
| 220 | + __( 'Kit', 'convertkit' ), | |
| 126 | 221 | __( 'Broadcasts', 'convertkit' ), |
| 127 | 222 | __( 'Posts', 'convertkit' ), |
| 128 | 223 | ), |
| 129 | 224 | |
| 130 | 225 | // Function to call when rendering as a block or a shortcode on the frontend web site. |
| 131 | - 'render_callback' => array( $this, 'render' ), | |
| 226 | + 'render_callback' => array( $this, 'render' ), | |
| 132 | 227 | |
| 133 | 228 | // Shortcode: TinyMCE / QuickTags Modal Width and Height. |
| 134 | - 'modal' => array( | |
| 229 | + 'modal' => array( | |
| 135 | 230 | 'width' => 650, |
| 136 | - 'height' => 405, | |
| 231 | + 'height' => 455, | |
| 137 | 232 | ), |
| 138 | 233 | |
| 139 | 234 | // Shortcode: Include a closing [/shortcode] tag when using TinyMCE or QuickTag Modals. |
| 140 | - 'shortcode_include_closing_tag' => false, | |
| 235 | + 'shortcode_include_closing_tag' => false, | |
| 141 | 236 | |
| 142 | 237 | // Gutenberg: Block Icon in Editor. |
| 143 | - 'gutenberg_icon' => convertkit_get_file_contents( CONVERTKIT_PLUGIN_PATH . '/resources/backend/images/block-icon-broadcasts.svg' ), | |
| 238 | + 'gutenberg_icon' => convertkit_get_file_contents( CONVERTKIT_PLUGIN_PATH . '/resources/backend/images/block-icon-broadcasts.svg' ), | |
| 144 | 239 | |
| 145 | 240 | // Gutenberg: Example image showing how this block looks when choosing it in Gutenberg. |
| 146 | - 'gutenberg_example_image' => CONVERTKIT_PLUGIN_URL . 'resources/backend/images/block-example-broadcasts.png', | |
| 241 | + 'gutenberg_example_image' => CONVERTKIT_PLUGIN_URL . 'resources/backend/images/block-example-broadcasts.png', | |
| 147 | 242 | |
| 148 | - // Help descriptions, displayed when no API key / resources exist and this block/shortcode is added. | |
| 149 | - 'no_api_key' => array( | |
| 150 | - 'notice' => __( 'No API Key specified.', 'convertkit' ), | |
| 151 | - 'link' => convertkit_get_setup_wizard_plugin_link(), | |
| 152 | - 'link_text' => __( 'Click here to add your API Key.', 'convertkit' ), | |
| 243 | + // Help descriptions, displayed when no Access Token / resources exist and this block/shortcode is added. | |
| 244 | + 'no_access_token' => array( | |
| 245 | + 'notice' => __( 'Not connected to Kit.', 'convertkit' ), | |
| 246 | + 'link' => convertkit_get_setup_wizard_plugin_link(), | |
| 247 | + 'link_text' => __( 'Click here to connect your Kit account.', 'convertkit' ), | |
| 248 | + 'instruction_text' => __( 'Connect your Kit account at Settings > Kit, and then refresh this page to configure broadcasts to display.', 'convertkit' ), | |
| 153 | 249 | ), |
| 154 | - 'no_resources' => array( | |
| 155 | - 'notice' => __( 'No broadcasts exist in ConvertKit.', 'convertkit' ), | |
| 156 | - 'link' => convertkit_get_new_broadcast_url(), | |
| 157 | - 'link_text' => __( 'Click here to send your first broadcast.', 'convertkit' ), | |
| 250 | + 'no_resources' => array( | |
| 251 | + 'notice' => __( 'No broadcasts exist in Kit.', 'convertkit' ), | |
| 252 | + 'link' => convertkit_get_new_broadcast_url(), | |
| 253 | + 'link_text' => __( 'Click here to send your first broadcast.', 'convertkit' ), | |
| 254 | + 'instruction_text' => __( 'Add a broadcast to your Kit account, and then refresh this page to configure broadcasts to display.', 'convertkit' ), | |
| 158 | 255 | ), |
| 159 | 256 | |
| 160 | - // Gutenberg: JS function to call when rendering the block preview in the Gutenberg editor. | |
| 161 | - // If not defined, render_callback above will be used. | |
| 162 | - 'gutenberg_preview_render_callback' => 'convertKitGutenbergBroadcastsBlockRenderPreview', | |
| 163 | - | |
| 164 | 257 | // Whether an API Key exists in the Plugin, and are the required resources (broadcasts) available. |
| 165 | 258 | // If no API Key is specified in the Plugin's settings, render the "No API Key" output. |
| 166 | - 'has_api_key' => $settings->has_api_key_and_secret(), | |
| 167 | - 'has_resources' => $posts->exist(), | |
| 259 | + 'has_access_token' => $settings->has_access_and_refresh_token(), | |
| 260 | + 'has_resources' => $posts->exist(), | |
| 168 | 261 | ); |
| 169 | 262 | |
| 170 | 263 | } |
| 171 | 264 | |
| @@ -181,8 +274,12 @@ | ||
| 181 | 274 | 'display_grid' => array( |
| 182 | 275 | 'type' => 'boolean', |
| 183 | 276 | 'default' => $this->get_default_value( 'display_grid' ), |
| 184 | 277 | ), |
| 278 | + 'display_order' => array( | |
| 279 | + 'type' => 'string', | |
| 280 | + 'default' => $this->get_default_value( 'display_order' ), | |
| 281 | + ), | |
| 185 | 282 | 'date_format' => array( |
| 186 | 283 | 'type' => 'string', |
| 187 | 284 | 'default' => $this->get_default_value( 'date_format' ), |
| 188 | 285 | ), |
| @@ -222,9 +319,9 @@ | ||
| 222 | 319 | 'type' => 'string', |
| 223 | 320 | 'default' => $this->get_default_value( 'paginate_label_next' ), |
| 224 | 321 | ), |
| 225 | 322 | |
| 226 | - // get_supports() color attribute. | |
| 323 | + // get_supports() style, color and typography attributes. | |
| 227 | 324 | 'style' => array( |
| 228 | 325 | 'type' => 'object', |
| 229 | 326 | ), |
| 230 | 327 | 'backgroundColor' => array( |
| @@ -232,8 +329,11 @@ | ||
| 232 | 329 | ), |
| 233 | 330 | 'textColor' => array( |
| 234 | 331 | 'type' => 'string', |
| 235 | 332 | ), |
| 333 | + 'fontSize' => array( | |
| 334 | + 'type' => 'string', | |
| 335 | + ), | |
| 236 | 336 | |
| 237 | 337 | // Always required for Gutenberg. |
| 238 | 338 | 'is_gutenberg_example' => array( |
| 239 | 339 | 'type' => 'boolean', |
| @@ -252,14 +352,22 @@ | ||
| 252 | 352 | */ |
| 253 | 353 | public function get_supports() { |
| 254 | 354 | |
| 255 | 355 | return array( |
| 256 | - 'className' => true, | |
| 257 | - 'color' => array( | |
| 356 | + 'className' => true, | |
| 357 | + 'color' => array( | |
| 258 | 358 | 'link' => true, |
| 259 | 359 | 'background' => true, |
| 260 | 360 | 'text' => true, |
| 261 | 361 | ), |
| 362 | + 'typography' => array( | |
| 363 | + 'fontSize' => true, | |
| 364 | + 'lineHeight' => true, | |
| 365 | + ), | |
| 366 | + 'spacing' => array( | |
| 367 | + 'margin' => true, | |
| 368 | + 'padding' => true, | |
| 369 | + ), | |
| 262 | 370 | ); |
| 263 | 371 | |
| 264 | 372 | } |
| 265 | 373 | |
| @@ -271,13 +379,8 @@ | ||
| 271 | 379 | * @return bool|array |
| 272 | 380 | */ |
| 273 | 381 | public function get_fields() { |
| 274 | 382 | |
| 275 | - // Bail if the request is not for the WordPress Administration or frontend editor. | |
| 276 | - if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) { | |
| 277 | - return false; | |
| 278 | - } | |
| 279 | - | |
| 280 | 383 | return array( |
| 281 | 384 | 'display_grid' => array( |
| 282 | 385 | 'label' => __( 'Display as grid', 'convertkit' ), |
| 283 | 386 | 'type' => 'toggle', |
| @@ -282,8 +385,16 @@ | ||
| 282 | 385 | 'label' => __( 'Display as grid', 'convertkit' ), |
| 283 | 386 | 'type' => 'toggle', |
| 284 | 387 | 'description' => __( 'If enabled, displays broadcasts in a grid, instead of a list.', 'convertkit' ), |
| 285 | 388 | ), |
| 389 | + 'display_order' => array( | |
| 390 | + 'label' => __( 'Display order', 'convertkit' ), | |
| 391 | + 'type' => 'select', | |
| 392 | + 'values' => array( | |
| 393 | + 'date-broadcast' => __( 'Date, Broadcast', 'convertkit' ), | |
| 394 | + 'broadcast-date' => __( 'Broadcast, Date', 'convertkit' ), | |
| 395 | + ), | |
| 396 | + ), | |
| 286 | 397 | 'date_format' => array( |
| 287 | 398 | 'label' => __( 'Date format', 'convertkit' ), |
| 288 | 399 | 'type' => 'select', |
| 289 | 400 | 'values' => array( |
| @@ -308,8 +419,12 @@ | ||
| 308 | 419 | 'read_more_label' => array( |
| 309 | 420 | 'label' => __( 'Read more label', 'convertkit' ), |
| 310 | 421 | 'type' => 'text', |
| 311 | 422 | 'description' => __( 'The label to display for the "read more" link below each broadcast.', 'convertkit' ), |
| 423 | + 'display_if' => array( | |
| 424 | + 'key' => 'display_read_more', | |
| 425 | + 'value' => 1, | |
| 426 | + ), | |
| 312 | 427 | ), |
| 313 | 428 | 'limit' => array( |
| 314 | 429 | 'label' => __( 'Number of posts', 'convertkit' ), |
| 315 | 430 | 'type' => 'number', |
| @@ -325,13 +440,21 @@ | ||
| 325 | 440 | 'paginate_label_prev' => array( |
| 326 | 441 | 'label' => __( 'Newer posts label', 'convertkit' ), |
| 327 | 442 | 'type' => 'text', |
| 328 | 443 | 'description' => __( 'The label to display for the link to newer broadcasts.', 'convertkit' ), |
| 444 | + 'display_if' => array( | |
| 445 | + 'key' => 'paginate', | |
| 446 | + 'value' => 1, | |
| 447 | + ), | |
| 329 | 448 | ), |
| 330 | 449 | 'paginate_label_next' => array( |
| 331 | 450 | 'label' => __( 'Older posts label', 'convertkit' ), |
| 332 | 451 | 'type' => 'text', |
| 333 | 452 | 'description' => __( 'The label to display for the link to older broadcasts.', 'convertkit' ), |
| 453 | + 'display_if' => array( | |
| 454 | + 'key' => 'paginate', | |
| 455 | + 'value' => 1, | |
| 456 | + ), | |
| 334 | 457 | ), |
| 335 | 458 | |
| 336 | 459 | // These fields will only display on the shortcode, and are deliberately not registered in get_attributes(), |
| 337 | 460 | // because Gutenberg will register its own color pickers for link, background and text. |
| @@ -359,18 +482,14 @@ | ||
| 359 | 482 | * @return bool|array |
| 360 | 483 | */ |
| 361 | 484 | public function get_panels() { |
| 362 | 485 | |
| 363 | - // Bail if the request is not for the WordPress Administration or frontend editor. | |
| 364 | - if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) { | |
| 365 | - return false; | |
| 366 | - } | |
| 367 | - | |
| 368 | 486 | return array( |
| 369 | 487 | 'general' => array( |
| 370 | 488 | 'label' => __( 'General', 'convertkit' ), |
| 371 | 489 | 'fields' => array( |
| 372 | 490 | 'display_grid', |
| 491 | + 'display_order', | |
| 373 | 492 | 'date_format', |
| 374 | 493 | 'display_image', |
| 375 | 494 | 'display_description', |
| 376 | 495 | 'display_read_more', |
| @@ -408,8 +527,9 @@ | ||
| 408 | 527 | public function get_default_values() { |
| 409 | 528 | |
| 410 | 529 | return array( |
| 411 | 530 | 'display_grid' => false, |
| 531 | + 'display_order' => 'date-broadcast', | |
| 412 | 532 | 'date_format' => 'F j, Y', |
| 413 | 533 | 'display_image' => false, |
| 414 | 534 | 'display_description' => false, |
| 415 | 535 | 'display_read_more' => false, |
| @@ -437,10 +557,10 @@ | ||
| 437 | 557 | * Returns the block's output, based on the supplied configuration attributes. |
| 438 | 558 | * |
| 439 | 559 | * @since 1.9.7.4 |
| 440 | 560 | * |
| 441 | - * @param array $atts Block / Shortcode Attributes. | |
| 442 | - * @return string Output | |
| 561 | + * @param array $atts Block / Shortcode / Page Builder Module Attributes. | |
| 562 | + * @return string | |
| 443 | 563 | */ |
| 444 | 564 | public function render( $atts ) { |
| 445 | 565 | |
| 446 | 566 | // Parse attributes, defining fallback defaults if required |
| @@ -452,19 +572,12 @@ | ||
| 452 | 572 | |
| 453 | 573 | // Fetch Posts. |
| 454 | 574 | $posts = new ConvertKit_Resource_Posts( 'output_broadcasts' ); |
| 455 | 575 | |
| 456 | - // If this is an admin request, refresh the Posts resource now from the API, | |
| 457 | - // as it's an inexpensive query of ~ 0.5 seconds when we're editing a Page | |
| 458 | - // containing this block. | |
| 459 | - if ( function_exists( 'is_admin' ) && is_admin() ) { | |
| 460 | - $posts->refresh(); | |
| 461 | - } | |
| 462 | - | |
| 463 | 576 | // If no Posts exist, bail. |
| 464 | 577 | if ( ! $posts->exist() ) { |
| 465 | 578 | if ( $settings->debug_enabled() ) { |
| 466 | - return '<!-- ' . __( 'No Broadcasts exist in ConvertKit.', 'convertkit' ) . ' -->'; | |
| 579 | + return '<!-- ' . __( 'No Broadcasts exist in Kit.', 'convertkit' ) . ' -->'; | |
| 467 | 580 | } |
| 468 | 581 | |
| 469 | 582 | return ''; |
| 470 | 583 | } |
| @@ -469,9 +582,29 @@ | ||
| 469 | 582 | return ''; |
| 470 | 583 | } |
| 471 | 584 | |
| 472 | 585 | // Build HTML. |
| 473 | - $html = $this->build_html( $posts, $atts ); | |
| 586 | + if ( $this->is_block_editor_request() ) { | |
| 587 | + // For the block editor, don't include compiled CSS classes and styles, | |
| 588 | + // as the block editor will add these to the parent container. | |
| 589 | + // Otherwise the block will render incorrectly with double padding, double margins etc. | |
| 590 | + $html = $this->build_html( | |
| 591 | + $posts, | |
| 592 | + $atts, | |
| 593 | + true, | |
| 594 | + array( | |
| 595 | + 'convertkit-' . $this->get_name(), | |
| 596 | + ) | |
| 597 | + ); | |
| 598 | + } else { | |
| 599 | + $html = $this->build_html( | |
| 600 | + $posts, | |
| 601 | + $atts, | |
| 602 | + true, | |
| 603 | + $this->get_css_classes(), | |
| 604 | + $this->get_css_styles( $atts ) | |
| 605 | + ); | |
| 606 | + } | |
| 474 | 607 | |
| 475 | 608 | /** |
| 476 | 609 | * Filter the block's content immediately before it is output. |
| 477 | 610 | * |
| @@ -476,10 +609,10 @@ | ||
| 476 | 609 | * Filter the block's content immediately before it is output. |
| 477 | 610 | * |
| 478 | 611 | * @since 1.9.7.4 |
| 479 | 612 | * |
| 480 | - * @param string $html ConvertKit Broadcasts HTML. | |
| 481 | - * @param array $atts Block Attributes. | |
| 613 | + * @param string $html ConvertKit Broadcasts HTML. | |
| 614 | + * @param array $atts Block Attributes. | |
| 482 | 615 | */ |
| 483 | 616 | $html = apply_filters( 'convertkit_block_broadcasts_render', $html, $atts ); |
| 484 | 617 | |
| 485 | 618 | return $html; |
| @@ -490,27 +623,27 @@ | ||
| 490 | 623 | * Returns the block's output, based on the supplied configuration attributes, |
| 491 | 624 | * when requested via AJAX. |
| 492 | 625 | * |
| 493 | 626 | * @since 1.9.7.6 |
| 627 | + * | |
| 628 | + * @param WP_REST_Request $request The REST request. | |
| 629 | + * @return string | |
| 494 | 630 | */ |
| 495 | - public function render_ajax() { | |
| 631 | + public function render_ajax( $request ) { | |
| 496 | 632 | |
| 497 | - // Check nonce. | |
| 498 | - check_ajax_referer( 'convertkit-broadcasts', 'nonce' ); | |
| 499 | - | |
| 500 | 633 | // Build attributes array. |
| 501 | 634 | $atts = array( |
| 502 | - 'date_format' => stripslashes( sanitize_text_field( $_REQUEST['date_format'] ) ), | |
| 503 | - 'display_image' => absint( $_REQUEST['display_image'] ), | |
| 504 | - 'display_description' => absint( $_REQUEST['display_description'] ), | |
| 505 | - 'display_read_more' => absint( $_REQUEST['display_read_more'] ), | |
| 506 | - 'read_more_label' => stripslashes( sanitize_text_field( $_REQUEST['read_more_label'] ) ), | |
| 507 | - 'limit' => absint( $_REQUEST['limit'] ), | |
| 508 | - 'page' => absint( $_REQUEST['page'] ), | |
| 509 | - 'paginate' => absint( $_REQUEST['paginate'] ), | |
| 510 | - 'paginate_label_next' => stripslashes( sanitize_text_field( $_REQUEST['paginate_label_next'] ) ), | |
| 511 | - 'paginate_label_prev' => stripslashes( sanitize_text_field( $_REQUEST['paginate_label_prev'] ) ), | |
| 512 | - 'link_color' => stripslashes( sanitize_text_field( $_REQUEST['link_color'] ) ), | |
| 635 | + 'date_format' => $request->get_param( 'date_format' ), | |
| 636 | + 'display_image' => $request->get_param( 'display_image' ), | |
| 637 | + 'display_description' => $request->get_param( 'display_description' ), | |
| 638 | + 'display_read_more' => $request->get_param( 'display_read_more' ), | |
| 639 | + 'read_more_label' => $request->get_param( 'read_more_label' ), | |
| 640 | + 'limit' => $request->get_param( 'limit' ), | |
| 641 | + 'page' => $request->get_param( 'page' ), | |
| 642 | + 'paginate' => $request->get_param( 'paginate' ), | |
| 643 | + 'paginate_label_next' => $request->get_param( 'paginate_label_next' ), | |
| 644 | + 'paginate_label_prev' => $request->get_param( 'paginate_label_prev' ), | |
| 645 | + 'link_color' => $request->get_param( 'link_color' ), | |
| 513 | 646 | ); |
| 514 | 647 | |
| 515 | 648 | // Parse attributes, defining fallback defaults if required |
| 516 | 649 | // and moving some attributes (such as Gutenberg's styles), if defined. |
| @@ -515,11 +648,23 @@ | ||
| 515 | 648 | // Parse attributes, defining fallback defaults if required |
| 516 | 649 | // and moving some attributes (such as Gutenberg's styles), if defined. |
| 517 | 650 | $atts = $this->sanitize_and_declare_atts( $atts ); |
| 518 | 651 | |
| 652 | + // Setup Settings class. | |
| 653 | + $settings = new ConvertKit_Settings(); | |
| 654 | + | |
| 519 | 655 | // Fetch Posts. |
| 520 | 656 | $posts = new ConvertKit_Resource_Posts( 'output_broadcasts' ); |
| 521 | 657 | |
| 658 | + // If no Posts exist, bail. | |
| 659 | + if ( ! $posts->exist() ) { | |
| 660 | + if ( $settings->debug_enabled() ) { | |
| 661 | + return '<!-- ' . __( 'No Broadcasts exist in Kit.', 'convertkit' ) . ' -->'; | |
| 662 | + } | |
| 663 | + | |
| 664 | + return ''; | |
| 665 | + } | |
| 666 | + | |
| 522 | 667 | // Build HTML. |
| 523 | 668 | $html = $this->build_html( $posts, $atts, false ); |
| 524 | 669 | |
| 525 | 670 | /** |
| @@ -532,10 +677,9 @@ | ||
| 532 | 677 | * @param array $atts Block Attributes. |
| 533 | 678 | */ |
| 534 | 679 | $html = apply_filters( 'convertkit_block_broadcasts_render_ajax', $html, $atts ); |
| 535 | 680 | |
| 536 | - // Send HTML as response. | |
| 537 | - wp_send_json_success( $html ); | |
| 681 | + return $html; | |
| 538 | 682 | |
| 539 | 683 | } |
| 540 | 684 | |
| 541 | 685 | /** |
| @@ -546,11 +690,13 @@ | ||
| 546 | 690 | * |
| 547 | 691 | * @param ConvertKit_Resource_Posts $posts ConvertKit Posts Resource class. |
| 548 | 692 | * @param array $atts Block attributes. |
| 549 | 693 | * @param bool $include_container Include container div in HTML. |
| 550 | - * @return string HTML | |
| 694 | + * @param array $css_classes CSS classes to apply to block. | |
| 695 | + * @param array $css_styles CSS inline styles to apply to block. | |
| 696 | + * @return string | |
| 551 | 697 | */ |
| 552 | - private function build_html( $posts, $atts, $include_container = true ) { | |
| 698 | + private function build_html( $posts, $atts, $include_container = true, $css_classes = array(), $css_styles = array() ) { | |
| 553 | 699 | |
| 554 | 700 | // Get paginated subset of Posts. |
| 555 | 701 | $broadcasts = $posts->get_paginated_subset( $atts['page'], $atts['limit'] ); |
| 556 | 702 | |
| @@ -561,9 +707,9 @@ | ||
| 561 | 707 | $html = ''; |
| 562 | 708 | |
| 563 | 709 | // Include container, if required. |
| 564 | 710 | if ( $include_container ) { |
| 565 | - $html .= '<div class="' . implode( ' ', map_deep( $atts['_css_classes'], 'sanitize_html_class' ) ) . '" style="' . implode( ';', map_deep( $atts['_css_styles'], 'esc_attr' ) ) . '" ' . $this->get_atts_as_html_data_attributes( $atts ) . '>'; | |
| 711 | + $html .= '<div class="' . implode( ' ', map_deep( $css_classes, 'sanitize_html_class' ) ) . '" style="' . implode( ';', map_deep( $css_styles, 'esc_attr' ) ) . '" ' . $this->get_atts_as_html_data_attributes( $atts ) . '>'; | |
| 566 | 712 | } |
| 567 | 713 | |
| 568 | 714 | // Start list. |
| 569 | 715 | $html .= '<ul class="convertkit-broadcasts-list">'; |