Example #1
0
/**
 * @function wpb_block_area
 * @since 0.3.0
 */
function wpb_block_area($area_id)
{
    $block = Block::get_current();
    if ($block == null) {
        return;
    }
    $page_id = $block->get_page_id();
    $post_id = $block->get_post_id();
    echo '<ul class="blocks" data-area-id="' . $area_id . '">';
    $page_blocks = get_post_meta($page_id, '_page_blocks', true);
    if ($page_blocks) {
        foreach ($page_blocks as $page_block) {
            if (!isset($page_block['buid']) || !isset($page_block['page_id']) || !isset($page_block['post_id']) || !isset($page_block['area_id'])) {
                continue;
            }
            if ($page_block['into_id'] == $post_id && $page_block['area_id'] == $area_id) {
                wpb_block_render_preview($page_block['buid'], $page_block['post_id'], $page_block['page_id']);
            }
        }
    }
    echo '</ul>';
    echo '<div class="button block-picker-modal-show" data-area-id="' . $area_id . '">Add block</div>';
}
    $buid = $_POST['buid'];
    $page_id = $_POST['page_id'];
    $into_id = $_POST['into_id'];
    $area_id = $_POST['area_id'];
    if (wpb_block_template_by_buid($buid) == null) {
        return;
    }
    $post_id = wp_insert_post(array('post_parent' => $page_id, 'post_type' => 'block', 'post_title' => sprintf('Page %s : Block %s', $page_id, $buid), 'post_content' => '', 'post_status' => 'publish'));
    $page_blocks = get_post_meta($page_id, '_page_blocks', true);
    if ($page_blocks == null) {
        $page_blocks = array();
    }
    $page_block = array('buid' => $buid, 'area_id' => $area_id, 'post_id' => (int) $post_id, 'page_id' => (int) $page_id, 'into_id' => (int) $into_id);
    $page_blocks[] = $page_block;
    update_post_meta($page_id, '_page_blocks', $page_blocks);
    wpb_block_render_preview($buid, $post_id, $page_id);
    exit;
});
/**
 * Removes a block from a page.
 * @action wp_ajax_remove_page_block
 * @since 0.1.0
 */
add_action('wp_ajax_remove_page_block', function () {
    $post_id = $_POST['post_id'];
    $page_id = $_POST['page_id'];
    $page_blocks = get_post_meta($page_id, '_page_blocks', true);
    if ($page_blocks == null) {
        return;
    }
    $page_blocks = array_filter($page_blocks, function ($page_block) use($post_id) {