get_title(), $this -> get_title(), 'manage_options', $this -> get_id(), array( &$this, 'load_view' ), 'dashicons-image-filter' ); return; } /** * enqueue scripts and styles when init() */ public function enqueue_scripts( $hook ) { if ( $hook != 'toplevel_page_' . $this -> get_id() ) { // check if on aidm home page // this script is available everywhere in the admin, except aidm home page $user_theme_preference = get_user_admin_ide_theme_preference(); wp_enqueue_script( 'aidmAdmin', plugin_dir_url( __DIR__ ) . 'js/aidmAdmin.js', ); wp_enqueue_style( 'aidm-admin', plugin_dir_url( __DIR__ ) . 'code-mirror-css-themes/' . $user_theme_preference . '.css', ); wp_localize_script( 'jquery', 'aidm_theme_preference', array( 'theme_name' => $user_theme_preference ) ); return; } // the part below is only available for aidm plugin home page // load codeMirror editor with type == php $cm_settings[ 'codeEditor' ] = wp_enqueue_code_editor( array( 'type' => 'text/x-php' ) ); wp_localize_script( 'jquery', 'cm_settings', array( $cm_settings ) ); // enqueue all codeMirror css themes $cm_themes_folder = plugin_dir_path( __DIR__ ) . 'code-mirror-css-themes/'; foreach( glob( $cm_themes_folder . '*.css' ) as $filename ) { wp_enqueue_style( basename( $filename, '.css' ), plugin_dir_url( __DIR__ ) . 'code-mirror-css-themes/' . basename( $filename ), ); } // plugin js wp_enqueue_script( 'aidmHome', plugin_dir_url( __DIR__ ) . 'js/aidmHome.js', ); // plugin css wp_enqueue_style( 'aidm-home', plugin_dir_url( __DIR__ ) . 'css/aidm-home.css', ); return; } /** * load plugin html view */ public function load_view() { require_once( plugin_dir_path( __DIR__ ) . 'views/aidm-home-views.php' ); return; } /** * process the form to admin_post.php and save preference in the database */ public function save_changes() { $nonce = sanitize_text_field( $_POST[ $this -> get_nonce_key() ]); $action = sanitize_text_field( $_POST[ 'action' ] ); $theme_preference = sanitize_text_field( $_POST[ 'theme' ] ); $redirect_to = sanitize_url( $_POST[ 'redirect_to_url' ] ); if ( ! isset( $nonce ) || ! wp_verify_nonce( $nonce, $action ) ) { print 'Sorry, someting went wrong.'; exit; } if ( ! current_user_can( 'manage_options' ) ) { print 'You can\'t manage options'; exit; } $this -> db_update_ide_theme_preference( $theme_preference ); if( $redirect_to ) { wp_safe_redirect( $redirect_to ); exit; } return; } /** * update the database */ public function db_update_ide_theme_preference( $theme_preference ) { $user_id = get_current_user_id(); $meta_key = 'aidm-theme-preference'; $meta_value = $theme_preference; update_user_meta( $user_id, $meta_key, $meta_value ); return; } }