templates = $templates; $this->template = $template; $this->router = $router; } public function hooks() { add_filter( 'aooshop_admin_tabs', array( $this, 'tab' ) ); add_action( 'aooshop_admin_tab_' . self::TAB, array( $this, 'render' ) ); add_action( 'admin_post_aooshop_tpl_save', array( $this, 'action_save' ) ); add_action( 'admin_post_aooshop_tpl_sample', array( $this, 'action_sample' ) ); } public function tab( $tabs ) { $tabs[ self::TAB ] = __( 'Storefront', 'aoo-shop' ); return $tabs; } public function page_url( array $args = array() ) { $url = add_query_arg( array_merge( array( 'page' => AooShop_Admin::tab_slug( self::TAB ), 'tab' => self::TAB ), $args ), admin_url( 'admin.php' ) ); return (string) apply_filters( 'aooshop_admin_storefront_url', $url, $args ); } /** Capability gate for every admin-post action; each action verifies its own nonce right after. */ private function require_cap() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to do this.', 'aoo-shop' ), 403 ); } } public function action_save() { $this->require_cap(); check_admin_referer( 'aooshop_tpl_save' ); $raw = isset( $_POST['aoo_tpl'] ) && is_array( $_POST['aoo_tpl'] ) ? wp_unslash( $_POST['aoo_tpl'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- absint per key in save(). $this->templates->save( array_map( 'absint', (array) $raw ) ); wp_safe_redirect( $this->page_url( array( 'aoo_msg' => 'tpl_saved' ) ) ); exit; } public function action_sample() { $this->require_cap(); check_admin_referer( 'aooshop_tpl_sample' ); $type = isset( $_POST['type'] ) ? sanitize_key( wp_unslash( $_POST['type'] ) ) : ''; $id = $this->templates->create_sample( $type ); if ( is_wp_error( $id ) ) { wp_safe_redirect( $this->page_url( array( 'aoo_msg' => 'tpl_error' ) ) ); exit; } wp_safe_redirect( $this->page_url( array( 'aoo_msg' => 'tpl_created', 'aoo_tpl_id' => $id ) ) ); exit; } /** Sample URL to look at a type with its template (first real slug when known). */ private function sample_url( $type ) { if ( in_array( $type, array( 'index', 'search' ), true ) ) { return 'search' === $type ? $this->router->url( 'search', 'a' ) : $this->router->url( 'index' ); } return ''; } private function notice() { // phpcs:disable WordPress.Security.NonceVerification.Recommended -- message display only. $msg = isset( $_GET['aoo_msg'] ) ? sanitize_key( wp_unslash( $_GET['aoo_msg'] ) ) : ''; $id = isset( $_GET['aoo_tpl_id'] ) ? absint( $_GET['aoo_tpl_id'] ) : 0; // phpcs:enable if ( 'tpl_saved' === $msg ) { echo '
' . esc_html__( 'Templates saved.', 'aoo-shop' ) . '
' . esc_html__( 'Sample template created and selected.', 'aoo-shop' ) . ' — ' . esc_html__( 'Edit this page', 'aoo-shop' ) . '
' . esc_html__( 'Could not create the template.', 'aoo-shop' ) . '
[aooshop_content part="header"] | |
[aooshop_content part="filters"] | |
[aooshop_content part="grid"] | |
[aooshop_content] |