public function init() { if (!is_admin() && BCE_Utilis::is_enabled_editor()) { wp_enqueue_media(); } $this->type = 'wpimage'; $this->class = 'wpimage'; $this->param = array('url', 'alt'); $this->admin_javascript = array(plugin_dir_url(__FILE__) . 'wpimage.js'); $this->template = ' <div class="bce-block bce-block__image"> <img src="%url%" alt="%alt%"/> </div> '; }
/** * 管理画面の静的ファイルを登録 * * @param $hook * @return bool */ public function admin_enqueue_script($hook) { if (!BCE_Utilis::is_enabled_editor()) { return false; } if (isset($this->admin_javascript) && is_array($this->admin_javascript)) { foreach ($this->admin_javascript as $i => $js_path) { wp_enqueue_script($this->type . '_' . $i . '_' . 'js', $js_path, array(), null, true); if ($this->type == 'tinymce') { wp_localize_script('tinymce_1_js', 'bce_tinymce_url', array('url' => plugin_dir_url(__FILE__) . 'tinymce/')); } } } if (isset($this->admin_css) && is_array($this->admin_css)) { foreach ($this->admin_css as $i => $css_path) { wp_enqueue_style($this->type . '_' . $i . '_' . 'css', $css_path, array()); } } }
/** * 記事を保存するタイミングで、カスタムフィールドとしてブロックエディタのコンテンツを保存する * @param $post_id * * @return bool */ public function block_content_update($post_id) { remove_action('save_post', array($this, 'block_content_update')); if (!BCE_Utilis::is_enabled_editor()) { return false; } $block_content = isset($_REQUEST['block_content']) ? $_REQUEST['block_content'] : ''; if (!$block_content || strlen($block_content) < 16) { return false; } update_post_meta($post_id, 'block_content', $block_content); /** * 強制保存が有効な場合は投稿コンテンツに保存。 * それ以外の場合には、カスタムフィールドに値を保存する **/ if ($this->force_post_content_save == true) { wp_update_post(array('ID' => $post_id, 'post_content' => $this->get_contents($post_id))); } else { update_post_meta($post_id, 'block_content_html', $this->get_contents($post_id)); } add_action('save_post', array($this, 'block_content_update')); }
/** * 投稿エディタにブロックコンテンツエディタ用のマークアップを追加 * @param $content * @return string */ public function the_editor($content) { if (!BCE_Utilis::is_enabled_editor()) { return $content; } $post_id = isset($_REQUEST['post']) ? intval($_REQUEST['post']) : ''; $block_content = get_post_meta($post_id, 'block_content', true); return $content . '<div id="block-content-editor-container" style="display: none; background: #fff;"><textarea name="block_content" id="block-content-editor">' . $block_content . '</textarea></div>'; }