registry = $registry; $this->settings = $settings; } /** * Registers hooks. * * @return void */ public function boot() { add_action( 'admin_menu', array( $this, 'menu' ) ); add_action( 'admin_init', array( $this, 'register' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'assets' ) ); add_filter( 'plugin_action_links_' . plugin_basename( CCTWEAKS_FILE ), array( $this, 'action_links' ) ); add_action( 'admin_post_cctweaks_import', array( $this, 'handle_import' ) ); } /** * Adds the Settings submenu. * * @return void */ public function menu() { $this->hook = add_options_page( __( 'CodingCush Tweaks', 'codingcush-tweaks' ), __( 'Tweaks', 'codingcush-tweaks' ), 'manage_options', self::SLUG, array( $this, 'render' ) ); } /** * Registers the option with the Settings API. * * @return void */ public function register() { register_setting( 'cctweaks', CCTWEAKS_OPTION, array( 'type' => 'array', 'sanitize_callback' => array( $this->settings, 'sanitize' ), 'default' => Registry::defaults(), ) ); } /** * Adds a Settings link on the Plugins screen. * * @param string[] $links Links. * @return string[] */ public function action_links( $links ) { $url = admin_url( 'options-general.php?page=' . self::SLUG ); array_unshift( $links, '' . esc_html__( 'Settings', 'codingcush-tweaks' ) . '' ); return $links; } /** * Loads CSS/JS on our screen only. * * @param string $hook Current hook suffix. * @return void */ public function assets( $hook ) { if ( $hook !== $this->hook ) { return; } wp_enqueue_style( 'cctweaks-admin', CCTWEAKS_URL . 'assets/css/admin.css', array(), CCTWEAKS_VERSION ); wp_enqueue_script( 'cctweaks-admin', CCTWEAKS_URL . 'assets/js/admin.js', array(), CCTWEAKS_VERSION, true ); wp_localize_script( 'cctweaks-admin', 'cctweaksL10n', array( /* translators: 1: enabled count, 2: total count */ 'enabled' => __( '%1$s of %2$s enabled', 'codingcush-tweaks' ), 'copied' => __( 'Copied to clipboard', 'codingcush-tweaks' ), 'noResults' => __( 'No tweaks match your search.', 'codingcush-tweaks' ), 'unsaved' => __( 'You have unsaved changes.', 'codingcush-tweaks' ), ) ); } /** * Renders the screen. * * @return void */ public function render() { if ( ! current_user_can( 'manage_options' ) ) { return; } $registry = $this->registry; $settings = $this->settings; $server = $this->server_limits(); require CCTWEAKS_DIR . 'includes/admin/view-settings.php'; } /** * PHP upload limits, for the upload-size tweak hint. * * @return array{upload:string, post:string} */ private function server_limits() { return array( 'upload' => size_format( wp_convert_hr_to_bytes( ini_get( 'upload_max_filesize' ) ) ), 'post' => size_format( wp_convert_hr_to_bytes( ini_get( 'post_max_size' ) ) ), ); } /** * Export payload (JSON) for the Tools tab. * * @return string */ public function export_json() { return wp_json_encode( array( 'plugin' => 'codingcush-tweaks', 'version' => CCTWEAKS_VERSION, 'settings' => $this->settings->all(), ), JSON_PRETTY_PRINT ); } /** * Handles the import form (admin-post). * * @return void */ public function handle_import() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'codingcush-tweaks' ) ); } check_admin_referer( 'cctweaks_import' ); $raw = isset( $_POST['cctweaks_import'] ) ? wp_unslash( $_POST['cctweaks_import'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- JSON decoded and fully sanitised below. $data = json_decode( is_string( $raw ) ? $raw : '', true ); $code = 'import_failed'; if ( is_array( $data ) && isset( $data['settings'] ) && is_array( $data['settings'] ) ) { update_option( CCTWEAKS_OPTION, $this->settings->sanitize( $data['settings'] ) ); $code = 'imported'; } wp_safe_redirect( add_query_arg( array( 'page' => self::SLUG, 'cctweaks_msg' => $code, 'tab' => 'tools', ), admin_url( 'options-general.php' ) ) ); exit; } }