hook = add_submenu_page( Constants::ADPRESSO_SLUG, __( 'AdPresso Settings', 'adpresso' ), __( 'Settings', 'adpresso' ), adpresso_get_capability( 'manage_settings' ), $this->get_id(), [ $this, 'render' ] ); } /** * Render the settings screen. * * @return void */ public function render(): void { if ( ! current_user_can( adpresso_get_capability( 'manage_settings' ) ) ) { wp_die( esc_html( 'You do not have permission to access this page.' ), 403 ); } $tabs = $this->get_tabs(); usort( $tabs, function( $a, $b ) { return $b->get_order() <=> $a->get_order(); } ); if ( empty( $tabs ) ) { return; } $logo_url = ADPRESSO_BASE_URL . 'admin/assets/img/adpresso-logo.svg'; echo '
'; echo '
'; $current_tab_id = Params::get()->getKey( 'tab', 'general' ); $current_tab = $this->get_tab( $current_tab_id ); // Fallback if no tab is specified in the URL. if ( is_null( $current_tab ) ) { $current_tab_id = array_key_first( $tabs ); $current_tab = $this->get_tab( $current_tab_id ); } $has_form = $current_tab->has_form(); // Not an h1/h2: WP moves admin notices after the first .wrap h1/h2 (the hidden h2 stays that anchor). printf( '
%s
', /* translators: %s: settings tab title */ esc_html( sprintf( __( 'AdPresso Settings: %s', 'adpresso' ), $current_tab->get_title() ) ) ); ?>
render( $has_form ); } ?>
tabs[ $tab->get_id() ] = $tab; Settings::get_instance()->register_options_from_tab( $tab->get_id(), $tab->get_options() ); do_action( 'adpresso_after_settings_tab_registered', $tab ); } /** * Get all registered tabs. * * @return array */ public function get_tabs(): array { return apply_filters( 'adpresso_settings_tabs', $this->tabs ); } /** * Get a specific tab by its ID. * * @param string $id * * @return SettingsTabInterface|null */ public function get_tab( string $id ): ?SettingsTabInterface { return $this->get_tabs()[ $id ] ?? null; } /** * Register sections and options for all tabs. * * @return void */ public function register_settings(): void { $tabs = $this->get_tabs(); usort( $tabs, function( $a, $b ) { return $b->get_order() <=> $a->get_order(); } ); foreach ( $tabs as $tab ) { /* @var SettingsTabInterface $tab */ if ( method_exists( $tab, 'register_settings' ) ) { $tab->register_settings(); } $tab_id = $tab->get_id(); $option_group = 'adpresso_settings_' . $tab_id; register_setting( $option_group, $option_group, [ 'sanitize_callback' => [ $tab, 'sanitize_settings' ] ] ); $sections = $tab->get_sections(); foreach ( $sections as $section_id => $section ) { add_settings_section( $section_id, null, [ $this, 'render_section_header' ], $option_group, [ 'section' => $section_id, 'tab_id' => $tab_id, 'args' => $section, ] ); } foreach ( $tab->get_options() as $key => $option ) { add_settings_field( $key, $option['label'] ?? '', [ $this, 'render_settings_field' ], $option_group, $option['section'], [ 'key' => $key, 'tab_id' => $tab_id, 'option_config' => $option, // The row class hides it from the first paint, backend.js toggles the same class later. 'class' => FieldRenderer::is_hidden_by_dependency( $option ) ? 'adpresso-field-hidden-by-dependency' : '', ] ); } } } /** * Callback for add_settings_section. * Displays the section header and description. * Adds data attributes for the accordion logic. * * @param array $args * * @return void */ public function render_section_header( array $args ): void { $section_id = $args['id']; $section = $args['args'] ?? []; $title = $section['title'] ?? ''; $description = $section['description'] ?? ''; $link = $section['link'] ?? ''; $controlled_by = $section['controlled_by'] ?? ''; $premium = $section['premium'] ?? false; $classes = [ 'adpresso-section-title' ]; $attributes = ''; if ( ! empty( $controlled_by ) ) { $attributes .= ' data-controlled-by="' . esc_attr( $controlled_by ) . '"'; } $attributes .= ' data-controls-content="section-' . esc_attr( $section_id ) . '-content"'; $classes[] = 'adpresso-accordion-trigger'; if ( $premium ) { $classes[] = 'is-premium'; } if ( ! empty( $title ) ) { // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Attributes are predefined and has been escaped before. echo '

' . esc_html( $title ) . '

'; } if ( ! empty( $description ) ) { if ( $premium ) { echo '

'; echo wp_kses_post( $description ); require ADPRESSO_ABSPATH . 'views/admin/premium_label.php'; } else { echo '

' . wp_kses_post( $description ); } if ( $link ) { echo Util::create_manual_link( $link ); } echo '

'; } // Important for JS: The section ID is used to identify the section in the accordion logic. // WordPress generates a for the settings fields. // We provide a marker for the JS to find the start of the content. echo ''; } /** * Renders a settings field on the settings page. * * @param array $args * * @return void */ public function render_settings_field( array $args ): void { $key = $args['key']; $tab_id = $args['tab_id']; $option_config = $args['option_config']; $option_name = Constants::META_PREFIX . 'settings_' . $tab_id; $section_config = []; // Some modules (e.g. TrafficGuardAdmin, HeadlessDeliveryAdmin) register their fields under a self-qualified "real_option[sub_key]" key instead of a plain one. if ( preg_match( '/^([^\[]+)\[([^\]]+)\]$/', $key, $m ) ) { [ , $real_option, $sub_key ] = $m; $value = $option_config['value'] ?? ( get_option( $real_option, [] )[ $sub_key ] ?? ( $option_config['default'] ?? null ) ); $name = $key; } else { $value = $option_config['value'] ?? Settings::get_setting( $key, $option_config['default'] ?? null ); $name = $option_name . '[' . $key . ']'; } // check if enabled/disabled if ( ! empty( $option_config['section'] ) ) { $section_config = $this->get_tab( $tab_id )?->get_sections()[ $option_config['section'] ] ?? null; } $render_args = $option_config; $render_args['name'] = $name; $render_args['id'] = $key; $render_args['value'] = $value; $render_args['section_config'] = $section_config; FieldRenderer::render( $render_args ); } /** * Enqueues scripts and styles for the Settings page. * * @param WP_Screen|null $current_screen The current WordPress screen object. */ public function enqueue_assets( ?WP_Screen $current_screen = null ): void { if ( ! $current_screen ) { return; } /* @var Assets $assets_manager */ $assets_manager = Plugin::get()->globals[ Assets::class ] ?? null; if ( ! $assets_manager ) { return; } if ( $current_screen->id === $this->hook ) { $assets_manager->enqueue_script( 'header' ); $assets_manager->enqueue_style( 'header' ); wp_set_script_translations( 'adpresso-header', 'adpresso' ); } } }