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'; } ?>

render_tools_notice(); ?>
render_dashboard( $modules ); } elseif ( 'tools' === $current ) { $this->render_tools(); } elseif ( $modules[ $current ]->has_custom_page() ) { $modules[ $current ]->render_custom_page(); } else { echo '

' . esc_html( $modules[ $current ]->get_description() ) . '

'; self::render_settings_form( $modules[ $current ] ); } ?>
categories(); ?>

$label ) : ?>
$module ) : $enabled = $this->plugin->is_enabled( $id ); $category = $module->get_category(); ?>

get_title() ); ?>

get_description() ); ?>

has_settings() ) : ?> →
get_settings(); ?>
get_id() ); ?> get_fields() as $key => $field ) : ?>
option_name() . '[' . $key . ']'; $id = 'toolvane-' . $module->get_id() . '-' . $key; $placeholder = isset( $field['placeholder'] ) ? $field['placeholder'] : ''; switch ( $field['type'] ) { case 'checkbox': printf( '', esc_attr( $id ), esc_attr( $name ), checked( ! empty( $value ), true, false ), isset( $field['checkbox_label'] ) ? esc_html( $field['checkbox_label'] ) : esc_html__( 'Enabled', 'toolvane' ) ); break; case 'checkboxes': printf( '
%2$s', esc_attr( $id ), esc_html( $field['label'] ) ); foreach ( $field['options'] as $opt_value => $opt_label ) { printf( '', esc_attr( $name ), esc_attr( $opt_value ), checked( in_array( (string) $opt_value, array_map( 'strval', (array) $value ), true ), true, false ), esc_html( $opt_label ) ); } echo '
'; break; case 'select': echo ''; break; case 'textarea': case 'plaintext': case 'lines': printf( '', esc_attr( $id ), esc_attr( $name ), isset( $field['rows'] ) ? absint( $field['rows'] ) : 5, 'textarea' === $field['type'] ? '' : 'code', esc_attr( $placeholder ), esc_textarea( (string) $value ) ); break; case 'color': printf( '', esc_attr( $id ), esc_attr( $name ), esc_attr( (string) $value ) ); break; case 'number': printf( ' %6$s', esc_attr( $id ), esc_attr( $name ), esc_attr( (string) $value ), isset( $field['min'] ) ? esc_attr( (string) $field['min'] ) : '', isset( $field['max'] ) ? esc_attr( (string) $field['max'] ) : '', isset( $field['unit'] ) ? esc_html( $field['unit'] ) : '' ); break; case 'image': printf( ' ', esc_attr( $id ), esc_attr( $name ), esc_attr( (string) $value ), esc_html__( 'Select', 'toolvane' ) ); break; case 'url': printf( '', esc_attr( $id ), esc_attr( $name ), esc_attr( (string) $value ), esc_attr( $placeholder ) ); break; default: printf( '', esc_attr( $id ), esc_attr( $name ), esc_attr( (string) $value ), esc_attr( $placeholder ) ); } if ( ! empty( $field['description'] ) ) { echo '

' . 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

', esc_attr( $messages[ $msg ][0] ), esc_html( $messages[ $msg ][1] ) ); } } /** * Tools tab. */ private function render_tools() { $post_url = admin_url( 'admin-post.php' ); ?>

403 ) ); } check_admin_referer( $action ); } /** * Streams the settings export. */ public function handle_export() { $this->verify_tools_request( 'toolvane_export' ); $data = array( 'plugin' => 'toolvane', 'version' => TOOLVANE_VERSION, 'exported' => gmdate( 'c' ), 'options' => array(), ); foreach ( $this->setting_options() as $option ) { $value = get_option( $option, null ); if ( null !== $value ) { $data['options'][ $option ] = $value; } } $host = (string) wp_parse_url( home_url(), PHP_URL_HOST ); $filename = 'toolvane-settings-' . sanitize_file_name( $host ) . '-' . gmdate( 'Y-m-d' ) . '.json'; nocache_headers(); header( 'Content-Type: application/json; charset=utf-8' ); header( 'Content-Disposition: attachment; filename="' . $filename . '"' ); echo wp_json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); exit; } /** * Imports a settings file. */ public function handle_import() { $this->verify_tools_request( 'toolvane_import' ); $back = self::url( 'tools', array( 'toolvane_tools' => 'invalid' ) ); $tmp = isset( $_FILES['toolvane_file']['tmp_name'] ) ? (string) $_FILES['toolvane_file']['tmp_name'] : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Nonce checked in verify_tools_request(); the path is validated with is_uploaded_file() and must not be unslashed (Windows paths). if ( '' === $tmp || ! is_uploaded_file( $tmp ) || filesize( $tmp ) > MB_IN_BYTES * 2 ) { wp_safe_redirect( $back ); exit; } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Reading a local uploaded file. $data = json_decode( (string) file_get_contents( $tmp ), true ); if ( ! is_array( $data ) || ! isset( $data['plugin'], $data['options'] ) || 'toolvane' !== $data['plugin'] || ! is_array( $data['options'] ) ) { wp_safe_redirect( $back ); exit; } $options = $data['options']; $modules = $this->plugin->get_modules(); if ( isset( $options[ Plugin::MODULES_OPTION ] ) ) { update_option( Plugin::MODULES_OPTION, $this->sanitize_modules( $options[ Plugin::MODULES_OPTION ] ) ); } if ( isset( $options[ Modules\Redirects::RULES_OPTION ] ) ) { update_option( Modules\Redirects::RULES_OPTION, Modules\Redirects::sanitize_rules( $options[ Modules\Redirects::RULES_OPTION ] ), true ); } foreach ( $modules as $module ) { $name = $module->option_name(); if ( isset( $options[ $name ] ) && is_array( $options[ $name ] ) && $module->get_fields() ) { // The option is registered with sanitize_option_{name}, so update_option() validates the values. update_option( $name, $options[ $name ] ); $module->flush_settings(); } } Cache::purge_all(); wp_safe_redirect( self::url( 'tools', array( 'toolvane_tools' => 'imported' ) ) ); exit; } /** * Resets all settings. */ public function handle_reset() { $this->verify_tools_request( 'toolvane_reset' ); foreach ( $this->setting_options() as $option ) { delete_option( $option ); } // Without the option every module falls back to its default state. Cache::purge_all(); wp_safe_redirect( self::url( 'tools', array( 'toolvane_tools' => 'reset' ) ) ); exit; } }