modules
3 years ago
admin.php
3 years ago
ai.php
3 years ago
answer.php
3 years ago
core.php
3 years ago
init.php
3 years ago
openai.php
3 years ago
query.php
3 years ago
queryimage.php
3 years ago
querytext.php
3 years ago
rest.php
3 years ago
ui.php
3 years ago
admin.php
134 lines
| 1 | <?php |
| 2 | class Meow_MWAI_Admin extends MeowCommon_Admin { |
| 3 | |
| 4 | public $core; |
| 5 | |
| 6 | public function __construct( $core ) { |
| 7 | $this->core = $core; |
| 8 | parent::__construct( MWAI_PREFIX, MWAI_ENTRY, MWAI_DOMAIN, class_exists( 'MeowPro_MWAI_Core' ) ); |
| 9 | if ( is_admin() ) { |
| 10 | add_action( 'admin_menu', array( $this, 'app_menu' ) ); |
| 11 | |
| 12 | // Load the scripts only if they are needed by the current screen |
| 13 | $page = isset( $_GET["page"] ) ? sanitize_text_field( $_GET["page"] ) : null; |
| 14 | |
| 15 | // We can check if the screen is used by AI Engine |
| 16 | $is_mwai_screen = in_array( $page, [ 'mwai_settings', 'mwai_dashboard' ] ); |
| 17 | $is_meowapps_dashboard = $page === 'meowapps-main-menu'; |
| 18 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
| 19 | |
| 20 | $this->core = $core; |
| 21 | add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
| 22 | add_filter( 'post_row_actions', [ $this, 'post_row_actions' ], 10, 2 ); |
| 23 | add_action( 'admin_footer', [ $this, 'admin_footer' ] ); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | function admin_menu() { |
| 28 | // Under Posts: |
| 29 | add_submenu_page( 'edit.php', 'Generate New', 'Generate New', 'manage_options', 'mwai_content_generator', |
| 30 | array( $this, 'ai_content_generator' ), 2 ); |
| 31 | add_management_page( 'AI Playground', __( 'AI Playground', 'ai-engine' ), 'manage_options', |
| 32 | 'mwai_dashboard', array( $this, 'ai_playground' ) ); |
| 33 | |
| 34 | // Under tools: |
| 35 | add_management_page( 'Content Generator', 'Content Generator', 'manage_options', 'mwai_content_generator', |
| 36 | array( $this, 'ai_content_generator' ) ); |
| 37 | add_management_page( 'Image Generator', 'Image Generator', 'manage_options', 'mwai_image_generator', |
| 38 | array( $this, 'ai_image_generator' ) ); |
| 39 | |
| 40 | // In the Admin Bar: |
| 41 | add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ), 100 ); |
| 42 | } |
| 43 | |
| 44 | function admin_bar_menu( $wp_admin_bar ) { |
| 45 | $url = MWAI_URL . "/images/wand.png"; |
| 46 | $image_html = "<img style='height: 22px; margin-bottom: -5px; margin-right: 10px;' src='${url}' alt='UI Engine' />"; |
| 47 | |
| 48 | $args = array( |
| 49 | 'id' => 'mwai-playground', |
| 50 | 'title' => $image_html . __( 'AI Playground', 'ai-engine' ), |
| 51 | 'href' => admin_url( 'tools.php?page=mwai_dashboard' ), |
| 52 | 'meta' => array( 'class' => 'mwai-playground' ), |
| 53 | ); |
| 54 | $wp_admin_bar->add_node( $args ); |
| 55 | } |
| 56 | |
| 57 | public function ai_playground() { |
| 58 | echo '<div id="mwai-playground"></div>'; |
| 59 | } |
| 60 | |
| 61 | public function ai_content_generator() { |
| 62 | echo '<div id="mwai-content-generator"></div>'; |
| 63 | } |
| 64 | |
| 65 | public function ai_image_generator() { |
| 66 | echo '<div id="mwai-image-generator"></div>'; |
| 67 | } |
| 68 | |
| 69 | function post_row_actions( $actions, $post ) { |
| 70 | //if ( $post->post_type === 'post' ) { |
| 71 | $actions['ai_titles'] = '<a class="mwai-link-title" href="#" data-id="' . |
| 72 | $post->ID . '" data-title="' . $post->post_title . '"> |
| 73 | <span class="dashicons dashicons-update"></span> Generate Titles</a>'; |
| 74 | //} |
| 75 | return $actions; |
| 76 | } |
| 77 | |
| 78 | function admin_footer() { |
| 79 | echo '<div id="mwai-admin-postsList"></div>'; |
| 80 | } |
| 81 | |
| 82 | function admin_enqueue_scripts() { |
| 83 | |
| 84 | // Load the scripts |
| 85 | $physical_file = MWAI_PATH . '/app/index.js'; |
| 86 | $cache_buster = file_exists( $physical_file ) ? filemtime( $physical_file ) : MWAI_VERSION; |
| 87 | |
| 88 | wp_register_script( 'mwai_meow_plugin-vendor', MWAI_URL . 'app/vendor.js', |
| 89 | ['wp-element', 'wp-i18n'], $cache_buster |
| 90 | ); |
| 91 | wp_register_script( 'mwai_meow_plugin', MWAI_URL . 'app/index.js', |
| 92 | ['mwai_meow_plugin-vendor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-edit-post', |
| 93 | 'wp-editor', |
| 94 | 'wp-element', 'wp-i18n', 'wp-plugins'], $cache_buster |
| 95 | ); |
| 96 | register_block_type( 'ai-engine/input-field', array( 'editor_script' => 'mwai_meow_plugin' )); |
| 97 | wp_set_script_translations( 'mwai_meow_plugin', 'ai-engine' ); |
| 98 | wp_enqueue_script('mwai_meow_plugin' ); |
| 99 | |
| 100 | // Load the fonts |
| 101 | // wp_register_style( 'meow-neko-ui-lato-font', |
| 102 | // '//fonts.googleapis.com/css2?family=Lato:wght@100;300;400;700;900&display=swap'); |
| 103 | // wp_enqueue_style( 'meow-neko-ui-lato-font' ); |
| 104 | |
| 105 | // Localize and options |
| 106 | wp_localize_script( 'mwai_meow_plugin', 'mwai_meow_plugin', [ |
| 107 | 'api_url' => rest_url( 'ai-engine/v1' ), |
| 108 | 'rest_url' => rest_url(), |
| 109 | 'plugin_url' => MWAI_URL, |
| 110 | 'prefix' => MWAI_PREFIX, |
| 111 | 'domain' => MWAI_DOMAIN, |
| 112 | 'is_pro' => class_exists( 'MeowPro_MWAI_Core' ), |
| 113 | 'is_registered' => !!$this->is_registered(), |
| 114 | 'rest_nonce' => wp_create_nonce( 'wp_rest' ), |
| 115 | 'session' => uniqid(), |
| 116 | 'options' => $this->core->get_all_options(), |
| 117 | ] ); |
| 118 | } |
| 119 | |
| 120 | function is_registered() { |
| 121 | return apply_filters( MWAI_PREFIX . '_meowapps_is_registered', false, MWAI_PREFIX ); |
| 122 | } |
| 123 | |
| 124 | function app_menu() { |
| 125 | add_submenu_page( 'meowapps-main-menu', 'AI Engine', 'AI Engine', 'manage_options', |
| 126 | 'mwai_settings', array( $this, 'admin_settings' ) ); |
| 127 | } |
| 128 | |
| 129 | function admin_settings() { |
| 130 | echo '<div id="mwai-admin-settings"></div>'; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | ?> |