menu_title ) ) { $this->menu_title = $this->page_title; } $this->hooks(); } /** * Add hooks to register the page and output content. * * @return void */ public function hooks() { add_action( 'admin_menu', array( $this, 'add_page' ) ); $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended // Only load if we are actually on the desired page. if ( $this->page_slug !== $page ) { return; } if ( ! current_user_can( $this->capability ) ) { wp_die( esc_html__( 'You do not have permission to access this page.', 'wpconsent-cookies-banner-privacy-suite' ) ); } remove_all_actions( 'admin_notices' ); remove_all_actions( 'all_admin_notices' ); add_action( 'wpconsent_admin_page', array( $this, 'output' ) ); add_action( 'wpconsent_admin_page', array( $this, 'output_footer' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'page_scripts' ) ); add_filter( 'admin_body_class', array( $this, 'page_specific_body_class' ) ); add_action( 'in_admin_footer', array( $this, 'wpconsent_footer' ) ); $this->setup_views(); $this->page_hooks(); $this->set_current_view(); } /** * If the page has views, this is where you should assign them to $this->views. * * @return void */ protected function setup_views() { } /** * Set the current view from the query param also checking it's a registered view for this page. * * @return void */ protected function set_current_view() { // phpcs:disable WordPress.Security.NonceVerification.Recommended if ( ! isset( $_GET['view'] ) ) { return; } $view = sanitize_text_field( wp_unslash( $_GET['view'] ) ); // phpcs:enable WordPress.Security.NonceVerification.Recommended if ( array_key_exists( $view, $this->views ) ) { $this->view = $view; } } /** * Override in child class to define page-specific hooks that will run only * after checks have been passed. * * @return void */ public function page_hooks() { } /** * Add the submenu page. * * @return void */ public function add_page() { add_submenu_page( 'wpconsent', $this->page_title, $this->menu_title, $this->capability, $this->page_slug, array( wpconsent()->admin_page_loader, 'admin_menu_page', ) ); } /** * Output the page content. * * @return void */ public function output() { $this->output_header(); ?>
output_content(); do_action( "wpconsent_admin_page_content_{$this->page_slug}", $this ); ?>
output_header_left(); ?>
output_header_right(); ?>
output_header_bottom(); ?>
maybe_output_message(); ?> logo_image(); } /** * Logo image. * * @return void */ public function logo_image() { wpconsent_icon( 'logo-text', 194, 34, '0 0 395 66' ); } /** * Top right area of the header, by default the notifications and help icons. * * @return void */ public function output_header_right() { $notifications_count = wpconsent()->notifications->get_count(); $dismissed_count = wpconsent()->notifications->get_dismissed_count(); $data_count = ''; if ( $notifications_count > 0 ) { $data_count = sprintf( 'data-count="%d"', absint( $notifications_count ) ); } $this->language_picker_button(); ?>

page_title ); ?>

get_error_message(); $success_message = $this->get_success_message(); ?>

message_error ) ? $this->message_error : false; } /** * If no message is set return false otherwise return the message string. * * @return false|string */ public function get_success_message() { return ! empty( $this->message_success ) ? $this->message_success : false; } /** * This is the main page content and you can't get away without it. * * @return void */ abstract public function output_content(); /** * Output footer markup, mostly used for overlays that are fixed. * * @return void */ public function output_footer() { $support_url = wpconsent_utm_url( 'https://wpconsent.com/contact/', 'help-overlay', 'support-url' ); ?>
    get_categories_accordion(); ?>

    ' . absint( wpconsent()->notifications->get_count() ) . '' ); ?>

    ' . absint( wpconsent()->notifications->get_dismissed_count() ) . '' ); ?>

    class_exists( 'WPConsent_Premium' ) ? wpconsent_utm_url( 'https://wpconsent.com/my-account/support/', 'plugin-footer', 'contact-support' ) : 'https://wordpress.org/support/plugin/wpconsent-cookies-banner-privacy-suite/', 'text' => __( 'Support', 'wpconsent-cookies-banner-privacy-suite' ), 'target' => '_blank', ), array( 'url' => function_exists( 'wpconsent_utm_url' ) ? wpconsent_utm_url( 'https://wpconsent.com/docs/', 'plugin-footer', 'documentation' ) : 'https://wpconsent.com/docs/', 'text' => __( 'Docs', 'wpconsent-cookies-banner-privacy-suite' ), 'target' => '_blank', ), ); $heart = '♥'; $team = 'WPConsent'; ?>
  • message_success = $message; } /** * Set an error message to display it in the appropriate place. * Let's use a function so if we decide to display multiple messages in the * same instance it's easy to change the variable to an array. * * @param string $message The message to store as error message. * * @return void */ public function set_error_message( $message ) { $this->message_error = $message; } /** * Add a page-specific body class using the page slug variable.. * * @param string $body_class The body class to append. * * @return string */ public function page_specific_body_class( $body_class ) { $body_class .= ' wpconsent-admin-page'; $body_class .= ' ' . $this->page_slug; return $body_class; } /** * Get the page url to be used in a form action. * * @return string */ public function get_page_action_url() { $args = array( 'page' => $this->page_slug, ); if ( ! empty( $this->view ) ) { $args['view'] = $this->view; } return add_query_arg( $args, $this->admin_url( 'admin.php' ) ); } /** * Get an admin URL. * * @param string $path The path to append to the admin URL. * * @return string */ public function admin_url( $path ) { return admin_url( $path ); } /** * Metabox-style layout for admin pages. * * @param string $title The metabox title. * @param string $content The metabox content. * @param string $help The helper text (optional) - if set, a help icon will show up next to the title. * @param string $id The metabox id attribute (optional). * * @return void */ public function metabox( $title, $content, $help = '', $id = '' ) { // translators: %s is the title of the metabox. $button_title = sprintf( __( 'Collapse Metabox %s', 'wpconsent-cookies-banner-privacy-suite' ), $title ) ?>
    >
    help_icon( $help ); ?>
    >
    '; } ?>

    %2$s →', esc_url( $link_url ), esc_html( $link_text ) ); } return sprintf( '
    %1$s%2$s
    ', wp_kses_post( $message ), $link // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ); } /** * Get a checkbox wrapped with markup to be displayed as a toggle. * * @param bool $checked Is it checked or not. * @param string $name The name for the input. * @param string $description Field description (optional). * @param string|int $value Field value (optional). * @param string $label Field label (optional). * @param string $target Target element to update when the checkbox is checked. * * @return string */ public function get_checkbox_toggle( $checked, $name, $description = '', $value = '', $label = '', $target = '' ) { $markup = ''; if ( ! empty( $label ) ) { $markup .= ''; } if ( ! empty( $description ) ) { $markup .= '

    ' . wp_kses_post( $description ) . '

    '; } return $markup; } /** * Get the full URL for a view of an admin page. * * @param string $view The view slug. * * @return string */ public function get_view_link( $view ) { return add_query_arg( array( 'page' => $this->page_slug, 'view' => $view, ), $this->admin_url( 'admin.php' ) ); } /** * Get a textarea field. * * @param string $id The id of the select field. * @param string $value The value of the textarea field. * @param string $description The description of the textarea field. * @param bool $wide Whether the textarea field should be wide. * @param array $attributes Additional attributes for the textarea field. * * @return string */ public function get_input_textarea( $id, $value = '', $description = '', $wide = false, $attributes = array() ) { $class = 'wpconsent-input-textarea wpconsent-regular-text'; if ( $wide ) { $class .= ' wpconsent-wide-text'; } $attributes_string = ''; foreach ( $attributes as $key => $attribute_value ) { $attributes_string .= sprintf( ' %s="%s"', esc_attr( $key ), esc_attr( $attribute_value ) ); } $markup = ''; if ( ! empty( $description ) ) { $markup .= '

    ' . wp_kses_post( $description ) . '

    '; } return $markup; } /** * Get an email field. * * @param string $id The id of the text field. * @param string $value The value of the text field. * @param string $description The description of the text field. * @param bool $wide Whether the text field should be wide. * * @return string */ public function get_input_email( $id, $value = '', $description = '', $wide = false ) { return $this->get_input_text( $id, $value, $description, $wide, 'email' ); } /** * Get a number field. * * @param string $id The id of the text field. * @param string $value The value of the text field. * @param string $description The description of the text field. * @param bool $wide Whether the text field should be wide. * * @return string */ public function get_input_number( $id, $value = '', $description = '', $wide = false ) { return $this->get_input_text( $id, $value, $description, $wide, 'number' ); } /** * Get a text field markup. * * @param string $id The id of the text field. * @param string $value The value of the text field. * @param string $description The description of the text field. * @param bool $wide Whether the text field should be wide. * @param string $type The type of the text field. * @param array $attributes Additional attributes for the input field. * * @return string */ public function get_input_text( $id, $value = '', $description = '', $wide = false, $type = 'text', $attributes = array() ) { $allowed_types = array( 'text', 'email', 'url', 'number', 'password', ); if ( in_array( $type, $allowed_types, true ) ) { $type = esc_attr( $type ); } else { $type = 'text'; } $class = 'wpconsent-regular-text'; if ( $wide ) { $class .= ' wpconsent-wide-text'; } $class .= ' wpconsent-input-' . $type; $attributes_string = ''; foreach ( $attributes as $key => $attribute_value ) { $attributes_string .= sprintf( ' %s="%s"', esc_attr( $key ), esc_attr( $attribute_value ) ); } $markup = ''; if ( ! empty( $description ) ) { $markup .= '

    ' . wp_kses_post( $description ) . '

    '; } return $markup; } /** * Get the page URL for any wpconsent page by its slug. * * @param string $slug The page slug. * @param string $view The view slug. * * @return string */ public function get_page_url( $slug, $view = '' ) { $args = array( 'page' => $slug, ); if ( ! empty( $view ) ) { $args['view'] = $view; } return add_query_arg( $args, $this->admin_url( 'admin.php' ) ); } /** * Output a separator row in a metabox. * * @return void */ public function metabox_row_separator() { ?>
    ', esc_attr( implode( ' ', $container_class ) ) ); $html .= '
    '; $html .= sprintf( '

    %s

    ', wp_kses_post( $title ) ); if ( ! empty( $text ) ) { $html .= sprintf( '
    %s
    ', wp_kses_post( $text ) ); } if ( ! empty( $features ) ) { $html .= ''; } $button_1 = wp_parse_args( $button_1, array( 'tag' => 'a', 'text' => '', 'url' => wpconsent_utm_url( 'https://wpconsent.com/lite/' ), 'class' => 'wpconsent-button wpconsent-button-orange wpconsent-button-large', 'attributes' => array( 'target' => '_blank', ), ) ); $button_2 = wp_parse_args( $button_2, array( 'tag' => 'a', 'text' => '', 'url' => wpconsent_utm_url( 'https://wpconsent.com/lite/' ), 'class' => 'wpconsent-upsell-button-text', 'attributes' => array( 'target' => '_blank', ), ) ); $html .= '
    '; // .wpconsent-upsell-text-content $html .= '
    '; if ( ! empty( $button_1['text'] ) ) { $html .= self::get_list_item_button( $button_1, false ); } if ( ! empty( $button_2['text'] ) ) { $html .= '
    '; $html .= self::get_list_item_button( $button_2, false ); } $html .= '
    '; // .wpconsent-upsell-buttons $html .= ''; return $html; } /** * Get a button for the list of items. * * @param array $args Arguments for the button. * @param bool $echo_output (optional) Whether to echo the button or return it. * * @return void|string */ public static function get_list_item_button( $args, $echo_output = true ) { $button_settings = wp_parse_args( $args, array( 'tag' => 'button', 'url' => '', 'text' => '', 'class' => 'wpconsent-button', 'attributes' => array(), ) ); if ( empty( $button_settings['text'] ) ) { return; } $button_settings['class'] = esc_attr( $button_settings['class'] ); $parsed_attributes = "class='{$button_settings['class']}' "; if ( ! empty( $button_settings['url'] ) && 'a' === $button_settings['tag'] ) { $parsed_attributes .= 'href="' . esc_url( $button_settings['url'] ) . '" '; } if ( ! empty( $button_settings['attributes'] ) ) { foreach ( $button_settings['attributes'] as $key => $value ) { $parsed_attributes .= esc_attr( $key ) . '="' . esc_attr( $value ) . '"'; } } if ( $echo_output ) { printf( '<%1$s %2$s>%3$s', $button_settings['tag'], // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped $parsed_attributes, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped wp_kses( $button_settings['text'], wpconsent_get_icon_allowed_tags() ) ); } else { return sprintf( '<%1$s %2$s>%3$s', $button_settings['tag'], // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped $parsed_attributes, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped wp_kses( $button_settings['text'], wpconsent_get_icon_allowed_tags() ) ); } } }