plugin = $plugin; } /** * Registers hooks. */ public function init() { add_action( 'admin_menu', array( $this, 'add_menu' ) ); add_action( 'admin_init', array( $this, 'register_settings' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) ); add_filter( 'plugin_action_links_' . TOOLVANE_BASENAME, array( $this, 'action_links' ) ); add_action( 'update_option_' . Plugin::MODULES_OPTION, array( $this, 'modules_updated' ), 10, 2 ); add_action( 'add_option_' . Plugin::MODULES_OPTION, array( $this, 'modules_added' ), 10, 2 ); add_action( 'admin_post_toolvane_export', array( $this, 'handle_export' ) ); add_action( 'admin_post_toolvane_import', array( $this, 'handle_import' ) ); add_action( 'admin_post_toolvane_reset', array( $this, 'handle_reset' ) ); foreach ( $this->plugin->get_modules() as $module ) { add_action( 'update_option_' . $module->option_name(), array( $module, 'on_settings_saved' ) ); add_action( 'add_option_' . $module->option_name(), array( $module, 'on_settings_saved' ) ); } } /** * URL of a Toolvane tab. * * @param string $tab Tab slug. * @param array $args Extra query arguments. * @return string */ public static function url( $tab = 'dashboard', array $args = array() ) { return add_query_arg( array_merge( array( 'page' => self::SLUG, 'tab' => $tab, ), $args ), admin_url( 'admin.php' ) ); } /** * Registers the top-level menu. */ public function add_menu() { $this->hook = add_menu_page( __( 'Toolvane', 'toolvane' ), __( 'Toolvane', 'toolvane' ), 'manage_options', self::SLUG, array( $this, 'render' ), 'dashicons-admin-tools', 80 ); } /** * Registers option groups with the Settings API. */ public function register_settings() { register_setting( 'toolvane_modules', Plugin::MODULES_OPTION, array( 'type' => 'array', 'sanitize_callback' => array( $this, 'sanitize_modules' ), 'default' => array(), ) ); foreach ( $this->plugin->get_modules() as $id => $module ) { if ( ! $module->get_fields() ) { continue; } register_setting( 'toolvane_' . $id, $module->option_name(), array( 'type' => 'array', 'sanitize_callback' => array( $module, 'sanitize' ), 'default' => array(), ) ); } } /** * Sanitizes the module on/off map. * * @param mixed $input Raw input. * @return array */ public function sanitize_modules( $input ) { if ( null === $this->previous_states ) { $this->previous_states = array(); foreach ( array_keys( $this->plugin->get_modules() ) as $id ) { $this->previous_states[ $id ] = $this->plugin->is_enabled( $id ); } } $input = is_array( $input ) ? $input : array(); $clean = array(); foreach ( array_keys( $this->plugin->get_modules() ) as $id ) { $clean[ $id ] = ! empty( $input[ $id ] ); } return $clean; } /** * Notifies modules that were switched on or off. * * @param mixed $old_value Previous value. * @param mixed $value New value. */ public function modules_updated( $old_value, $value ) { $value = is_array( $value ) ? $value : array(); foreach ( $this->plugin->get_modules() as $id => $module ) { $before = is_array( $this->previous_states ) && isset( $this->previous_states[ $id ] ) ? $this->previous_states[ $id ] : $module->enabled_by_default(); if ( is_array( $old_value ) && array_key_exists( $id, $old_value ) ) { $before = (bool) $old_value[ $id ]; } $after = ! empty( $value[ $id ] ); if ( $before !== $after ) { $module->on_toggle( $after ); } } } /** * Handles the very first save of the module map. * * @param string $option Option name. * @param mixed $value New value. */ public function modules_added( $option, $value ) { $this->modules_updated( array(), $value ); } /** * Enqueues assets on the Toolvane screen only. * * @param string $hook Current admin page hook. */ public function enqueue( $hook ) { if ( $hook !== $this->hook ) { return; } wp_enqueue_media(); wp_enqueue_style( 'wp-color-picker' ); wp_enqueue_style( 'toolvane-admin', TOOLVANE_URL . 'assets/css/admin.css', array(), TOOLVANE_VERSION ); wp_enqueue_script( 'toolvane-admin', TOOLVANE_URL . 'assets/js/admin.js', array( 'jquery', 'wp-color-picker' ), TOOLVANE_VERSION, true ); wp_localize_script( 'toolvane-admin', 'toolvaneAdmin', array( 'chooseImage' => __( 'Choose image', 'toolvane' ), 'useImage' => __( 'Use this image', 'toolvane' ), 'confirm' => __( 'Are you sure? This cannot be undone.', 'toolvane' ), ) ); } /** * Adds a "Settings" link on the Plugins screen. * * @param array $links Existing links. * @return array */ public function action_links( $links ) { array_unshift( $links, '' . esc_html__( 'Settings', 'toolvane' ) . '' ); return $links; } /** * Category labels. * * @return array */ private function categories() { return array( 'content' => __( 'Content', 'toolvane' ), 'seo' => __( 'SEO', 'toolvane' ), 'security' => __( 'Security', 'toolvane' ), 'performance' => __( 'Performance', 'toolvane' ), 'design' => __( 'Design', 'toolvane' ), 'admin' => __( 'Admin', 'toolvane' ), ); } /** * Page output. */ public function render() { if ( ! current_user_can( 'manage_options' ) ) { return; } $modules = $this->plugin->get_modules(); $tabs = array( 'dashboard' => __( 'Dashboard', 'toolvane' ) ); foreach ( $modules as $id => $module ) { if ( $this->plugin->is_enabled( $id ) && $module->has_settings() ) { $tabs[ $id ] = $module->get_title(); } } $tabs['tools'] = __( 'Import / Export', 'toolvane' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only tab switch. $current = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'dashboard'; if ( ! isset( $tabs[ $current ] ) ) { $current = 'dashboard'; } ?>
' . esc_html( $modules[ $current ]->get_description() ) . '
'; self::render_settings_form( $modules[ $current ] ); } ?>' . wp_kses_post( $field['description'] ) . '
'; } } /* * Import / export / reset. */ /** * Option names that hold settings (logs and caches are left out). * * @return string[] */ private function setting_options() { $options = array( Plugin::MODULES_OPTION, Modules\Redirects::RULES_OPTION ); foreach ( $this->plugin->get_modules() as $module ) { $options[] = $module->option_name(); } return array_unique( $options ); } /** * Shows the result of an import or reset. */ private function render_tools_notice() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Display only. $msg = isset( $_GET['toolvane_tools'] ) ? sanitize_key( wp_unslash( $_GET['toolvane_tools'] ) ) : ''; $messages = array( 'imported' => array( 'success', __( 'Settings imported.', 'toolvane' ) ), 'reset' => array( 'success', __( 'All Toolvane settings were reset to their defaults.', 'toolvane' ) ), 'invalid' => array( 'error', __( 'The file is not a valid Toolvane settings export.', 'toolvane' ) ), ); if ( isset( $messages[ $msg ] ) ) { printf( '%2$s