should_load() ) { $instance->setup_hooks(); } } /** * Check if TinyMCE is active. * * @return bool */ protected function is_editor_active() { global $pagenow; return in_array( $pagenow, array( 'post.php', 'post-new.php' ), true ) && user_can_richedit(); } /** * Setup hooks. */ private function setup_hooks() { add_filter( 'mce_buttons', array( $this, 'add_button' ) ); add_filter( 'mce_external_plugins', array( $this, 'add_plugin' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); } /** * Add button to TinyMCE. * * @param array $buttons Existing buttons. * @return array */ public function add_button( $buttons ) { $current_post_type = $this->get_current_post_type(); $supported_post_types = AIIL_Core::get_supported_post_types(); $is_supported = $this->is_supported_post_type(); if ( ! $is_supported ) { return $buttons; } $buttons[] = 'aiil_generate_links'; return $buttons; } /** * Add TinyMCE plugin. * * @param array $plugins Existing plugins. * @return array */ public function add_plugin( $plugins ) { $current_post_type = $this->get_current_post_type(); $is_supported = $this->is_supported_post_type(); if ( ! $is_supported ) { return $plugins; } $plugins['aiil_generate_links'] = AIIL_PLUGIN_URL . 'assets/js/tinymce-plugin.js'; return $plugins; } /** * Enqueue editor assets. */ public function enqueue_assets() { $current_post_type = $this->get_current_post_type(); $is_supported = $this->is_supported_post_type(); if ( ! $is_supported ) { return; } // Localize script for TinyMCE. wp_localize_script( 'jquery', 'aiilTinyMCE', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'aiil_generate_links' ), 'postId' => get_the_ID(), 'isPremium' => AIIL_Core::is_licence_active(), 'i18n' => array( 'buttonText' => __( 'AI Internal Links', 'ai-internal-links' ), 'generating' => __( 'Generating links...', 'ai-internal-links' ), 'success' => __( 'Links generated successfully!', 'ai-internal-links' ), 'error' => __( 'Error generating links. Please try again.', 'ai-internal-links' ), 'noApiKey' => __( 'Please configure your API key in Settings > AI Internal Links', 'ai-internal-links' ), 'noContent' => __( 'Please add some content before generating links.', 'ai-internal-links' ), ), ) ); } }