get_editor_settings() ); } /** * Add Classic Editor assets on post editing screens. * * @param string $hook Current admin hook. */ public function enqueue_classic_editor_assets($hook) { if (!in_array($hook, array('post.php', 'post-new.php'), true)) { return; } $script_path = SKYINPO_PLUGIN_PATH . 'assets/js/classic-editor.js'; $style_path = SKYINPO_PLUGIN_PATH . 'assets/css/classic-editor.css'; wp_register_script( 'skyinpo-internal-post-linker-classic-editor', SKYINPO_PLUGIN_URL . 'assets/js/classic-editor.js', array(), file_exists($script_path) ? filemtime($script_path) : SKYINPO_VERSION, false ); wp_add_inline_script( 'skyinpo-internal-post-linker-classic-editor', 'window.skyinpo_InternalPostLinkerClassic = ' . wp_json_encode($this->get_editor_settings()) . ';', 'before' ); wp_enqueue_script('skyinpo-internal-post-linker-classic-editor'); wp_enqueue_style( 'skyinpo-internal-post-linker-classic-editor', SKYINPO_PLUGIN_URL . 'assets/css/classic-editor.css', array(), file_exists($style_path) ? filemtime($style_path) : SKYINPO_VERSION ); } /** * Register the TinyMCE plugin script. * * @param array $plugins TinyMCE plugins. * @return array */ public function register_tinymce_plugin($plugins) { if (!current_user_can('edit_posts') && !current_user_can('edit_pages')) { return $plugins; } $plugins['skyinpo_internal_post_linker'] = SKYINPO_PLUGIN_URL . 'assets/js/classic-editor.js'; return $plugins; } /** * Register the TinyMCE toolbar button. * * @param string[] $buttons TinyMCE buttons. * @return string[] */ public function register_tinymce_button($buttons) { if (!current_user_can('edit_posts') && !current_user_can('edit_pages')) { return $buttons; } $buttons[] = 'skyinpo_internal_post_linker'; return $buttons; } /** * Return shared editor settings. * * @return array */ private function get_editor_settings() { return array( 'searchPath' => '/' . $this->namespace . '/search', 'ajaxUrl' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('skyinpo_search_posts'), 'currentPostId' => get_the_ID() ? absint(get_the_ID()) : 0, 'minSearchLength' => 2, 'searchTimeoutMs' => 8000, ); } /** * Register REST routes used by the editor UI. */ public function register_rest_routes() { register_rest_route( $this->namespace, '/search', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array($this, 'search_posts'), 'permission_callback' => array($this, 'can_search_posts'), 'args' => array( 'term' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'default' => '', ), 'post_id' => array( 'type' => 'integer', 'sanitize_callback' => 'absint', 'default' => 0, ), ), ) ); } /** * Confirm the current user can use the post editor. * * @return bool */ public function can_search_posts() { return current_user_can('edit_posts'); } /** * Search linkable posts over admin-ajax for editor requests. */ public function ajax_search_posts() { check_ajax_referer('skyinpo_search_posts', 'nonce'); if (!$this->can_search_posts()) { wp_send_json_error( array('message' => __('You are not allowed to search posts.', '7sky-internal-post-linker')), 403 ); } $results = $this->get_search_results( isset($_GET['term']) ? sanitize_text_field(wp_unslash($_GET['term'])) : '', isset($_GET['post_id']) ? absint($_GET['post_id']) : 0 ); wp_send_json_success($results); } /** * Search linkable internal content. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function search_posts(WP_REST_Request $request) { $term = trim((string) $request->get_param('term')); $current_post_id = absint($request->get_param('post_id')); return rest_ensure_response($this->get_search_results($term, $current_post_id)); } /** * Get search results for REST and admin-ajax requests. * * @param string $term Search term. * @param int $current_post_id Current editor post ID. * @return array> */ private function get_search_results($term, $current_post_id) { $term = trim((string) $term); $current_post_id = absint($current_post_id); if (strlen($term) < 2) { return array(); } $post_types = $this->get_searchable_post_types(); if (empty($post_types)) { return array(); } $post_statuses = array('publish'); if (current_user_can('read_private_posts')) { $post_statuses[] = 'private'; } $query = new WP_Query( array( 's' => $term, 'post_type' => $post_types, 'post_status' => $post_statuses, 'posts_per_page' => 8, 'orderby' => 'modified', 'order' => 'DESC', 'no_found_rows' => true, 'ignore_sticky_posts' => true, 'search_columns' => array('post_title', 'post_name'), ) ); $results = array(); foreach ($query->posts as $post) { $results[] = array( 'id' => (int) $post->ID, 'title' => html_entity_decode(get_the_title($post), ENT_QUOTES, get_bloginfo('charset')), 'type' => $this->get_post_type_label($post->post_type), 'postType' => $post->post_type, 'url' => get_permalink($post), 'status' => $post->post_status, 'isCurrent' => $current_post_id === (int) $post->ID, 'date' => get_the_date('', $post), 'excerpt' => $this->get_result_excerpt($post), ); } wp_reset_postdata(); return $results; } /** * Return public post types that are useful as internal link targets. * * @return string[] */ private function get_searchable_post_types() { $post_types = get_post_types( array( 'public' => true, 'show_ui' => true, ), 'names' ); unset($post_types['attachment']); return array_values($post_types); } /** * Get a readable post type label. * * @param string $post_type Post type name. * @return string */ private function get_post_type_label($post_type) { $object = get_post_type_object($post_type); if (!$object || empty($object->labels->singular_name)) { return $post_type; } return $object->labels->singular_name; } /** * Build a compact excerpt for search results. * * @param WP_Post $post Post object. * @return string */ private function get_result_excerpt(WP_Post $post) { $raw_excerpt = has_excerpt($post) ? $post->post_excerpt : $post->post_content; $excerpt = wp_strip_all_tags(strip_shortcodes($raw_excerpt)); return html_entity_decode(wp_trim_words($excerpt, 18, '...'), ENT_QUOTES, get_bloginfo('charset')); } } skyinpo_Internal_Post_Linker::init();