Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions admin/class-convertkit-admin-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

/**
* Registers a metabox on Posts, Pages and public facing Custom Post Types
* and saves its settings when the Post is saved in the WordPress Administration
* interface.
* that do not use the block editor, saving settings when the Post is saved
* in the WordPress Administration interface.
*
* @package ConvertKit
* @author ConvertKit
Expand All @@ -27,7 +27,7 @@ public function __construct() {
add_filter( 'views_edit-page', array( $this, 'output_wp_list_table_buttons' ) );

add_action( 'post_submitbox_misc_actions', array( $this, 'output_pre_publish_actions' ) );
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 10, 2 );
add_action( 'save_post', array( $this, 'save_post_meta' ) );

}
Expand Down Expand Up @@ -181,16 +181,22 @@ public function output_pre_publish_actions( $post ) {
*
* @since 1.9.6
*
* @param string $post_type Post Type.
* @param string $post_type Post Type.
* @param WP_Post $post Post.
*/
public function add_meta_boxes( $post_type ) {
public function add_meta_boxes( $post_type, $post ) {

// Don't register the meta box if this Post Type isn't supported.
$supported_post_types = convertkit_get_supported_post_types();
if ( ! in_array( $post_type, $supported_post_types, true ) ) {
return;
}

// Don't register the meta box if the block editor is being used, as register_post_meta() handles saving post meta.
if ( function_exists( 'use_block_editor_for_post' ) && use_block_editor_for_post( $post ) ) {
return;
}

// Register Meta Box.
add_meta_box( 'wp-convertkit-meta-box', __( 'Kit', 'convertkit' ), array( $this, 'display_meta_box' ), $post_type, 'normal' );

Expand Down Expand Up @@ -261,6 +267,11 @@ public function save_post_meta( $post_id ) {
return;
}

// Bail if the block editor is being used, as register_post_meta() handles saving post meta.
if ( function_exists( 'use_block_editor_for_post' ) && use_block_editor_for_post( $post_id ) ) {
return;
}

// Bail if no nonce field exists.
if ( ! isset( $_POST['wp-convertkit-save-meta-nonce'] ) ) {
return;
Expand Down
71 changes: 71 additions & 0 deletions includes/class-convertkit-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public function __construct() {
// Register Gutenberg Blocks.
add_action( 'init', array( $this, 'add_blocks' ) );

// Register Gutenberg Plugin Sidebars.
add_action( 'init', array( $this, 'add_plugin_sidebars' ) );

// Register REST API routes.
add_action( 'rest_api_init', array( $this, 'register_routes' ) );

Expand Down Expand Up @@ -187,6 +190,63 @@ public function add_blocks() {

}

/**
* Registers post meta for any registered plugin sidebars using register_post_meta(),
* so data is saved when using the Gutenberg editor.
*
* @since 3.3.0
*/
public function add_plugin_sidebars() {

// Get plugin sidebars.
$plugin_sidebars = convertkit_get_plugin_sidebars();

// Bail if no plugin sidebars are available.
if ( ! count( $plugin_sidebars ) ) {
return;
}

foreach ( $plugin_sidebars as $plugin_sidebar ) {
register_post_meta(
'',
$plugin_sidebar['meta_key'],
array(
'show_in_rest' => array(
'schema' => array(
'type' => 'object',
'properties' => $plugin_sidebar['attributes'],
),
),
'single' => true,
'type' => 'object',
'default' => $plugin_sidebar['default_values'],
'sanitize_callback' => function ( $value ) use ( $plugin_sidebar ) {

// If the value is not an array, return the default values.
if ( ! is_array( $value ) ) {
return $plugin_sidebar['default_values'];
}

// Iterate through the attributes and sanitize the value.
foreach ( $plugin_sidebar['attributes'] as $key => $attribute ) {
$value[ $key ] = sanitize_text_field( $value[ $key ] ?? $value['default'] );
}

// Return the sanitized value.
return $value;

},
'auth_callback' => function () use ( $plugin_sidebar ) {

return current_user_can( $plugin_sidebar['minimum_capability'] );

},
)
);
}

}

/**
* Determines the block API version to use for registering blocks.
*
Expand Down Expand Up @@ -214,6 +274,7 @@ public function get_block_api_version() {
return absint( $block_api_version );

}

/**
* Enqueues scripts for Gutenberg blocks in the editor view.
*
Expand All @@ -233,13 +294,23 @@ public function enqueue_scripts() {
$blocks = convertkit_get_blocks();
$block_formatters = convertkit_get_block_formatters();
$pre_publish_actions = convertkit_get_pre_publish_actions();
$plugin_sidebars = convertkit_get_plugin_sidebars();

// Enqueue Gutenberg Javascript, and set the blocks data.
wp_enqueue_script( 'convertkit-gutenberg', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
wp_localize_script( 'convertkit-gutenberg', 'convertkit_blocks', $blocks );

// If pre-publish actions are available, set the data.
if ( count( $pre_publish_actions ) ) {
wp_localize_script( 'convertkit-gutenberg', 'convertkit_pre_publish_actions', $pre_publish_actions );
}

// If plugin sidebars are available, set the data.
if ( count( $plugin_sidebars ) ) {
wp_localize_script( 'convertkit-gutenberg', 'convertkit_plugin_sidebars', $plugin_sidebars );
}

// Set the Gutenberg data.
wp_localize_script(
'convertkit-gutenberg',
'convertkit_gutenberg',
Expand Down
1 change: 1 addition & 0 deletions includes/class-wp-convertkit.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ private function initialize_global() {
$this->classes['block_formatter_form_link'] = new ConvertKit_Block_Formatter_Form_Link();
$this->classes['block_formatter_product_link'] = new ConvertKit_Block_Formatter_Product_Link();
$this->classes['pre_publish_action_broadcast_export'] = new ConvertKit_Pre_Publish_Action_Broadcast_Export();
$this->classes['plugin_sidebar_post_settings'] = new ConvertKit_Plugin_Sidebar_Post_Settings();
$this->classes['broadcasts_exporter'] = new ConvertKit_Broadcasts_Exporter();
$this->classes['broadcasts_importer'] = new ConvertKit_Broadcasts_Importer();
$this->classes['elementor'] = new ConvertKit_Elementor();
Expand Down
24 changes: 24 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,30 @@ function convertkit_get_block_formatters() {

}

/**
* Helper method to get registered plugin sidebars.
*
* @since 3.3.0
*
* @return array Plugin sidebars
*/
function convertkit_get_plugin_sidebars() {

$plugin_sidebars = array();

/**
* Registers plugin sidebars for the WordPress block editor.
*
* @since 3.3.0
*
* @param array $plugin_sidebars Plugin sidebars.
*/
$plugin_sidebars = apply_filters( 'convertkit_plugin_sidebars', $plugin_sidebars );

return $plugin_sidebars;

}

/**
* Helper method to get registered pre-publish actions.
*
Expand Down
Loading