*/ public static function tabs() { return array( 'general' => __( 'General', 'mm-post-grid' ), 'content' => __( 'Content', 'mm-post-grid' ), 'templates' => __( 'Templates', 'mm-post-grid' ), 'styling' => __( 'Styling', 'mm-post-grid' ), 'advanced' => __( 'Advanced', 'mm-post-grid' ), ); } /** * Returns the requested tab, falling back to the first one. * * @since 0.1 * @return string */ public static function current_tab() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Navigation only, no state is changed. $requested = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : ''; $tabs = self::tabs(); return isset( $tabs[ $requested ] ) ? $requested : 'general'; } /** * Adds the top level menu. * * @since 0.1 * @return void */ public function add_menu() { $this->hook_suffix = (string) add_menu_page( __( 'MM Post Grid', 'mm-post-grid' ), __( 'MM Post Grid', 'mm-post-grid' ), self::CAPABILITY, self::MENU_SLUG, array( $this, 'render_page' ), 'dashicons-grid-view', 58 ); } /** * Adds a settings link on the plugins screen. * * @since 0.1 * * @param string[] $links Existing action links. * @return string[] */ public function action_links( $links ) { if ( ! current_user_can( self::CAPABILITY ) ) { return $links; } $link = sprintf( '%2$s', esc_url( admin_url( 'admin.php?page=' . self::MENU_SLUG ) ), esc_html__( 'Settings', 'mm-post-grid' ) ); return array_merge( array( 'settings' => $link ), (array) $links ); } /** * Loads the admin assets on the settings screen only. * * @since 0.1 * * @param string $hook_suffix Current admin page. * @return void */ public function enqueue( $hook_suffix ) { if ( '' === $this->hook_suffix || $hook_suffix !== $this->hook_suffix ) { return; } wp_enqueue_style( 'wp-color-picker' ); wp_enqueue_style( 'mm-post-grid-admin', MM_POST_GRID_URL . 'assets/css/mmpg-admin.css', array( 'wp-color-picker' ), MM_POST_GRID_VERSION ); wp_enqueue_script( 'mm-post-grid-admin', MM_POST_GRID_URL . 'assets/js/mmpg-admin.js', array( 'jquery', 'wp-color-picker', 'wp-a11y' ), MM_POST_GRID_VERSION, true ); wp_localize_script( 'mm-post-grid-admin', 'mmpgAdmin', array( 'copied' => __( 'Copied to the clipboard.', 'mm-post-grid' ), 'copyError' => __( 'Copying failed, please select the text and copy it.', 'mm-post-grid' ), 'unsaved' => __( 'You have unsaved changes.', 'mm-post-grid' ), ) ); } /** * Restores the default settings. * * @since 0.1 * @return void */ public function handle_reset() { if ( ! current_user_can( self::CAPABILITY ) ) { wp_die( esc_html__( 'You are not allowed to change these settings.', 'mm-post-grid' ), esc_html__( 'Permission denied', 'mm-post-grid' ), array( 'response' => 403 ) ); } check_admin_referer( 'mmpg_reset_settings' ); update_option( Settings::OPTION, Settings::defaults() ); Settings::flush_cache(); $tab = isset( $_POST['tab'] ) ? sanitize_key( wp_unslash( $_POST['tab'] ) ) : 'general'; $tabs = self::tabs(); wp_safe_redirect( add_query_arg( array( 'page' => self::MENU_SLUG, 'tab' => isset( $tabs[ $tab ] ) ? $tab : 'general', 'mmpg-reset' => '1', ), admin_url( 'admin.php' ) ) ); exit; } /** * Applies a styling template. * * @since 0.1 * @return void */ public function handle_apply_template() { if ( ! current_user_can( self::CAPABILITY ) ) { wp_die( esc_html__( 'You are not allowed to change these settings.', 'mm-post-grid' ), esc_html__( 'Permission denied', 'mm-post-grid' ), array( 'response' => 403 ) ); } check_admin_referer( 'mmpg_apply_template' ); $slug = isset( $_POST['template'] ) ? sanitize_key( wp_unslash( $_POST['template'] ) ) : ''; $applied = Templates::apply( $slug ); wp_safe_redirect( add_query_arg( array( 'page' => self::MENU_SLUG, 'tab' => 'templates', 'mmpg-template' => $applied ? $slug : 'mmpg-unknown', ), admin_url( 'admin.php' ) ) ); exit; } /** * Renders the settings screen. * * @since 0.1 * @return void */ public function render_page() { if ( ! current_user_can( self::CAPABILITY ) ) { wp_die( esc_html__( 'You are not allowed to view this page.', 'mm-post-grid' ) ); } $tabs = self::tabs(); $tab = self::current_tab(); $this->queue_notices(); ?>
[mm_post_grid]