mirror of
https://github.com/WenPai-org/wpmind.git
synced 2025-08-04 03:29:33 +08:00
added Welcome page
changed settings API and page to React + rest changed structure of scripts - different folders for editor and admin scripts
This commit is contained in:
parent
5f489c933e
commit
f1c3c698dc
66 changed files with 978 additions and 1286 deletions
|
@ -18,6 +18,8 @@ class Mind_Admin {
|
|||
*/
|
||||
public function __construct() {
|
||||
add_action( 'admin_menu', [ $this, 'register_admin_menu' ], 20 );
|
||||
|
||||
add_filter( 'admin_body_class', [ $this, 'admin_body_class' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -35,11 +37,60 @@ class Mind_Admin {
|
|||
esc_html__( 'Mind', 'mind' ),
|
||||
'manage_options',
|
||||
'mind',
|
||||
[ 'Mind_Settings', 'print_settings_page' ],
|
||||
[ $this, 'print_admin_page' ],
|
||||
// phpcs:ignore
|
||||
'data:image/svg+xml;base64,' . base64_encode( file_get_contents( mind()->plugin_path . 'assets/images/admin-icon.svg' ) ),
|
||||
'58.7'
|
||||
);
|
||||
|
||||
add_submenu_page(
|
||||
'mind',
|
||||
'',
|
||||
esc_html__( 'Welcome', 'mind' ),
|
||||
'manage_options',
|
||||
'mind'
|
||||
);
|
||||
add_submenu_page(
|
||||
'mind',
|
||||
'',
|
||||
esc_html__( 'Settings', 'mind' ),
|
||||
'manage_options',
|
||||
'admin.php?page=mind&sub_page=settings'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print admin page.
|
||||
*/
|
||||
public function print_admin_page() {
|
||||
?>
|
||||
<div class="mind-admin-root"></div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Add page class to body.
|
||||
*
|
||||
* @param string $classes - body classes.
|
||||
*/
|
||||
public function admin_body_class( $classes ) {
|
||||
$screen = get_current_screen();
|
||||
|
||||
if ( 'toplevel_page_mind' !== $screen->id ) {
|
||||
return $classes;
|
||||
}
|
||||
|
||||
$page_name = 'welcome';
|
||||
|
||||
// phpcs:ignore
|
||||
if ( isset( $_GET['sub_page'] ) && $_GET['sub_page'] ) {
|
||||
// phpcs:ignore
|
||||
$page_name = $_GET['sub_page'];
|
||||
}
|
||||
|
||||
$classes .= ' mind-admin-page mind-admin-page-' . esc_attr( $page_name );
|
||||
|
||||
return $classes;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@ class Mind_Assets {
|
|||
*/
|
||||
public function __construct() {
|
||||
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_editor_assets' ] );
|
||||
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -45,12 +46,14 @@ class Mind_Assets {
|
|||
* Enqueue editor assets
|
||||
*/
|
||||
public function enqueue_block_editor_assets() {
|
||||
$openai_key = Mind_Settings::get_option( 'openai_key', 'mind_general' );
|
||||
$asset_data = $this->get_asset_file( 'build/index' );
|
||||
$settings = get_option( 'mind_settings', array() );
|
||||
|
||||
$openai_key = $settings['openai_api_key'] ?? '';
|
||||
$asset_data = $this->get_asset_file( 'build/editor' );
|
||||
|
||||
wp_enqueue_script(
|
||||
'mind-editor',
|
||||
mind()->plugin_url . 'build/index.js',
|
||||
mind()->plugin_url . 'build/editor.js',
|
||||
$asset_data['dependencies'],
|
||||
$asset_data['version'],
|
||||
true
|
||||
|
@ -59,15 +62,52 @@ class Mind_Assets {
|
|||
wp_localize_script(
|
||||
'mind-editor',
|
||||
'mindData',
|
||||
array(
|
||||
[
|
||||
'connected' => ! ! $openai_key,
|
||||
'settingsPageURL' => admin_url( 'admin.php?page=mind' ),
|
||||
)
|
||||
'settingsPageURL' => admin_url( 'admin.php?page=mind&sub_page=settings' ),
|
||||
]
|
||||
);
|
||||
|
||||
wp_enqueue_style(
|
||||
'mind-editor',
|
||||
mind()->plugin_url . 'build/style-index.css',
|
||||
mind()->plugin_url . 'build/style-editor.css',
|
||||
[],
|
||||
$asset_data['version']
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Enqueue admin pages assets.
|
||||
*/
|
||||
public function admin_enqueue_scripts() {
|
||||
$screen = get_current_screen();
|
||||
|
||||
if ( 'toplevel_page_mind' !== $screen->id ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$asset_data = $this->get_asset_file( 'build/admin' );
|
||||
|
||||
wp_enqueue_script(
|
||||
'mind-admin',
|
||||
mind()->plugin_url . 'build/admin.js',
|
||||
$asset_data['dependencies'],
|
||||
$asset_data['version'],
|
||||
true
|
||||
);
|
||||
|
||||
wp_localize_script(
|
||||
'mind-admin',
|
||||
'mindAdminData',
|
||||
[
|
||||
'settings' => get_option( 'mind_settings', array() ),
|
||||
]
|
||||
);
|
||||
|
||||
wp_enqueue_style(
|
||||
'mind-admin',
|
||||
mind()->plugin_url . 'build/style-admin.css',
|
||||
[],
|
||||
$asset_data['version']
|
||||
);
|
||||
|
|
|
@ -40,7 +40,18 @@ class Mind_Rest extends WP_REST_Controller {
|
|||
public function register_routes() {
|
||||
$namespace = $this->namespace . $this->version;
|
||||
|
||||
// Get layouts list.
|
||||
// Update Settings.
|
||||
register_rest_route(
|
||||
$namespace,
|
||||
'/update_settings/',
|
||||
[
|
||||
'methods' => [ 'POST' ],
|
||||
'callback' => [ $this, 'update_settings' ],
|
||||
'permission_callback' => [ $this, 'update_settings_permission' ],
|
||||
]
|
||||
);
|
||||
|
||||
// Request OpenAI API.
|
||||
register_rest_route(
|
||||
$namespace,
|
||||
'/request_ai/',
|
||||
|
@ -52,6 +63,19 @@ class Mind_Rest extends WP_REST_Controller {
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get edit options permissions.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function update_settings_permission() {
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
return $this->error( 'user_dont_have_permission', __( 'User don\'t have permissions to change options.', 'mind' ), true );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get permissions for OpenAI api request.
|
||||
*
|
||||
|
@ -65,6 +89,24 @@ class Mind_Rest extends WP_REST_Controller {
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update Settings.
|
||||
*
|
||||
* @param WP_REST_Request $req request object.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function update_settings( WP_REST_Request $req ) {
|
||||
$new_settings = $req->get_param( 'settings' );
|
||||
|
||||
if ( is_array( $new_settings ) ) {
|
||||
$current_settings = get_option( 'mind_settings', [] );
|
||||
update_option( 'mind_settings', array_merge( $current_settings, $new_settings ) );
|
||||
}
|
||||
|
||||
return $this->success( true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Send request to OpenAI.
|
||||
*
|
||||
|
@ -73,9 +115,11 @@ class Mind_Rest extends WP_REST_Controller {
|
|||
* @return mixed
|
||||
*/
|
||||
public function request_ai( WP_REST_Request $req ) {
|
||||
$openai_key = Mind_Settings::get_option( 'openai_key', 'mind_general' );
|
||||
$request = $req->get_param( 'request' ) ?? '';
|
||||
$context = $req->get_param( 'context' ) ?? '';
|
||||
$settings = get_option( 'mind_settings', array() );
|
||||
$openai_key = $settings['openai_api_key'] ?? '';
|
||||
|
||||
$request = $req->get_param( 'request' ) ?? '';
|
||||
$context = $req->get_param( 'context' ) ?? '';
|
||||
|
||||
if ( ! $openai_key ) {
|
||||
return $this->error( 'no_openai_key_found', __( 'Provide OpenAI key in the plugin settings.', 'mind' ) );
|
||||
|
|
|
@ -1,209 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Plugin Settings
|
||||
*
|
||||
* @package mind
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
require_once mind()->plugin_path . 'vendors/class-settings-api.php';
|
||||
|
||||
/**
|
||||
* Mind Settings Class
|
||||
*/
|
||||
class Mind_Settings {
|
||||
/**
|
||||
* Settings API instance
|
||||
*
|
||||
* @var object
|
||||
*/
|
||||
public static $settings_api;
|
||||
|
||||
/**
|
||||
* Cached settings fields. We call settings fields method a lot of times to get default values.
|
||||
* So, for performance reasons we need to cache the output.
|
||||
*
|
||||
* @var object
|
||||
*/
|
||||
public static $cached_settings_fields;
|
||||
|
||||
/**
|
||||
* Mind_Settings constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
self::init_actions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Option Value
|
||||
*
|
||||
* @param string $option - option name.
|
||||
* @param string $section - section name.
|
||||
*
|
||||
* @return bool|string
|
||||
*/
|
||||
public static function get_option( $option, $section ) {
|
||||
$options = get_option( $section );
|
||||
$result = '';
|
||||
|
||||
if ( isset( $options[ $option ] ) ) {
|
||||
$result = $options[ $option ];
|
||||
} else {
|
||||
// find default.
|
||||
$fields = self::get_settings_fields();
|
||||
|
||||
if ( isset( $fields[ $section ] ) && is_array( $fields[ $section ] ) ) {
|
||||
foreach ( $fields[ $section ] as $field_data ) {
|
||||
if ( $option === $field_data['name'] && isset( $field_data['default'] ) ) {
|
||||
$result = $field_data['default'];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 'off' === $result ? false : ( 'on' === $result ? true : $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update Option Value
|
||||
*
|
||||
* @param string $option - option name.
|
||||
* @param string $section - section name.
|
||||
* @param string $value - new option value.
|
||||
*/
|
||||
public static function update_option( $option, $section, $value ) {
|
||||
$options = get_option( $section );
|
||||
|
||||
if ( ! is_array( $options ) ) {
|
||||
$options = [];
|
||||
}
|
||||
|
||||
$options[ $option ] = $value;
|
||||
|
||||
update_option( $section, $options );
|
||||
}
|
||||
|
||||
/**
|
||||
* Init actions
|
||||
*/
|
||||
public static function init_actions() {
|
||||
self::$settings_api = new Mind_Settings_API();
|
||||
|
||||
add_action( 'admin_init', [ __CLASS__, 'admin_init' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the settings
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function admin_init() {
|
||||
// set the settings.
|
||||
self::$settings_api->set_sections( self::get_settings_sections() );
|
||||
self::$settings_api->set_fields( self::get_settings_fields() );
|
||||
|
||||
// initialize settings.
|
||||
self::$settings_api->admin_init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Plugin settings sections
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function get_settings_sections() {
|
||||
$sections = [
|
||||
[
|
||||
'id' => 'mind_general',
|
||||
'title' => esc_html__( 'General', 'mind' ),
|
||||
'icon' => '<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6V4m0 2a2 2 0 100 4m0-4a2 2 0 110 4m-6 8a2 2 0 100-4m0 4a2 2 0 110-4m0 4v2m0-6V4m6 6v10m6-2a2 2 0 100-4m0 4a2 2 0 110-4m0 4v2m0-6V4" /></svg>',
|
||||
],
|
||||
];
|
||||
|
||||
return apply_filters( 'mind_settings_sections', $sections );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all the settings fields
|
||||
*
|
||||
* @return array settings fields
|
||||
*/
|
||||
public static function get_settings_fields() {
|
||||
if ( ! empty( self::$cached_settings_fields ) ) {
|
||||
return self::$cached_settings_fields;
|
||||
}
|
||||
|
||||
// retrieve openai key from the DB.
|
||||
// we can't use Mind Settings API as it will result a stack trace error.
|
||||
$openai_key = false;
|
||||
$general_settings = get_option( 'mind_general' );
|
||||
if ( isset( $general_settings['openai_key'] ) ) {
|
||||
$openai_key = $general_settings['openai_key'];
|
||||
}
|
||||
|
||||
$settings_fields = [
|
||||
'mind_general' => [
|
||||
[
|
||||
'name' => 'openai_key',
|
||||
'label' => esc_html__( 'OpenAI API Key', 'mind' ),
|
||||
'desc' => esc_html__( 'This setting is required, since our plugin works with OpenAI.', 'mind' ) . ' <a href="https://platform.openai.com/account/api-keys" target="_blank">Create API key</a>',
|
||||
'type' => $openai_key ? 'password' : 'text',
|
||||
'default' => '',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
self::$cached_settings_fields = apply_filters( 'mind_settings_fields', $settings_fields );
|
||||
|
||||
return self::$cached_settings_fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* The plugin page handler
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function print_settings_page() {
|
||||
self::$settings_api->admin_enqueue_scripts();
|
||||
|
||||
echo '<div class="wrap">';
|
||||
echo '<h2>' . esc_html__( 'Settings', 'mind' ) . '</h2>';
|
||||
|
||||
self::$settings_api->show_navigation();
|
||||
self::$settings_api->show_forms();
|
||||
|
||||
echo '</div>';
|
||||
|
||||
?>
|
||||
<script>
|
||||
(function( $ ) {
|
||||
// Don't allow adding input number values that > then max attribute and < min attribute.
|
||||
$('form').on('input', '[type="number"]', function(e) {
|
||||
var current = parseFloat( this.value );
|
||||
var min = parseFloat(this.min);
|
||||
var max = parseFloat(this.max);
|
||||
|
||||
if ('' !== this.value) {
|
||||
if (!Number.isNaN(min) && current < min) {
|
||||
this.value = min;
|
||||
}
|
||||
if (!Number.isNaN(max) && current > max) {
|
||||
this.value = max;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
<?php if ( ! class_exists( 'Mind_Pro' ) ) : ?>
|
||||
// disable pro inputs.
|
||||
$('.mind-settings-control-pro').find('input, textarea').attr('disabled', 'disabled');
|
||||
<?php endif; ?>
|
||||
})(jQuery);
|
||||
</script>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
|
||||
new Mind_Settings();
|
Loading…
Add table
Add a link
Reference in a new issue