[ 'name' => esc_html__( 'Addronix Addons Items', 'addronix-addons-for-elementor' ), 'singular_name' => esc_html__( 'Addronix Addons Item', 'addronix-addons-for-elementor' ), 'add_new' => esc_html__( 'Add New Template', 'addronix-addons-for-elementor' ), 'add_new_item' => esc_html__( 'Add New Template', 'addronix-addons-for-elementor' ), 'edit_item' => esc_html__( 'Edit Template', 'addronix-addons-for-elementor' ), 'view_item' => esc_html__( 'View Template', 'addronix-addons-for-elementor' ), 'search_items' => esc_html__( 'Search Templates', 'addronix-addons-for-elementor' ), 'not_found' => esc_html__( 'No templates found.', 'addronix-addons-for-elementor' ), 'not_found_in_trash' => esc_html__( 'No templates found in Trash.', 'addronix-addons-for-elementor' ), ], 'public' => true, 'publicly_queryable' => true, 'rewrite' => [ 'slug' => 'aafe-template', 'with_front' => false ], 'show_ui' => true, 'show_in_menu' => false, 'show_in_nav_menus' => false, 'exclude_from_search' => true, 'has_archive' => false, 'capability_type' => 'page', 'show_in_rest' => true, 'hierarchical' => false, 'supports' => [ 'title', 'editor', 'elementor' ], ] ); register_post_meta( self::CPT, self::META_TYPE, [ 'show_in_rest' => true, 'single' => true, 'type' => 'string', 'auth_callback' => function() { return current_user_can( 'edit_posts' ); }, ] ); // AAFE FIX: Force Elementor support for our templates so "Edit with Elementor" works. $cpt_support = get_option( 'elementor_cpt_support', [ 'page', 'post' ] ); if ( ! in_array( self::CPT, $cpt_support, true ) ) { $cpt_support[] = self::CPT; update_option( 'elementor_cpt_support', $cpt_support ); } } /** * Add "Template Type" meta box to the editor sidebar. */ public function add_type_meta_box() { add_meta_box( 'aafe_template_type_box', esc_html__( 'Template Type', 'addronix-addons-for-elementor' ), [ $this, 'render_type_meta_box' ], self::CPT, 'side', 'high' ); } /** * Render the "Template Type" meta box. * * @param \WP_Post $post */ public function render_type_meta_box( $post ) { wp_nonce_field( 'aafe_template_type_nonce', 'aafe_template_type_nonce_field' ); $current = get_post_meta( $post->ID, self::META_TYPE, true ); // Pre-select from URL param when creating a new post. $post_type = isset( $_GET['post_type'] ) ? sanitize_key( wp_unslash( $_GET['post_type'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading from GET for admin UI state. if ( self::CPT === $post_type && isset( $_GET['aafe_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading from GET for admin UI state. $current = sanitize_key( wp_unslash( $_GET['aafe_type'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading from GET for admin UI state. } $types = [ self::TYPE_HEADER => __( '🔝 Header', 'addronix-addons-for-elementor' ), self::TYPE_FOOTER => __( '🔻 Footer', 'addronix-addons-for-elementor' ), self::TYPE_LOOP => __( '🔁 Loop Item', 'addronix-addons-for-elementor' ), self::TYPE_MEGA_MENU => __( '🚀 Mega Menu', 'addronix-addons-for-elementor' ), ]; ?> register_document_type( self::CPT, \Elementor\Core\DocumentTypes\Page::get_class_full_name() ); } /** * Filter the admin list to show only templates of a specific type * when the `aafe_type` URL parameter is present. * * @param \WP_Query $query */ public function filter_admin_list( $query ) { if ( ! is_admin() || ! $query->is_main_query() ) { return; } $post_type = isset( $_GET['post_type'] ) ? sanitize_key( wp_unslash( $_GET['post_type'] ) ) : ( isset( $query->query['post_type'] ) ? $query->query['post_type'] : '' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading from GET for admin UI state. if ( self::CPT !== $post_type || empty( $_GET['aafe_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading from GET for admin UI state. return; } $type = sanitize_key( wp_unslash( $_GET['aafe_type'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading from GET for admin UI state. if ( 'header-footer' === $type ) { $query->set( 'meta_query', [ [ 'key' => self::META_TYPE, 'value' => [ self::TYPE_HEADER, self::TYPE_FOOTER ], 'compare' => 'IN', ], ] ); } else { $query->set( 'meta_key', self::META_TYPE ); $query->set( 'meta_value', $type ); } } /** * Add a "Type" column to the CPT admin list table. * * @param array $columns * @return array */ public function add_type_column( $columns ) { $new = []; foreach ( $columns as $key => $label ) { $new[ $key ] = $label; if ( 'title' === $key ) { $new['aafe_type'] = esc_html__( 'Type', 'addronix-addons-for-elementor' ); } } return $new; } /** * Render the "Type" column value. * * @param string $column * @param int $post_id */ public function render_type_column( $column, $post_id ) { if ( 'aafe_type' !== $column ) { return; } $type = get_post_meta( $post_id, self::META_TYPE, true ); $labels = [ self::TYPE_HEADER => '🔝 ' . __( 'Header', 'addronix-addons-for-elementor' ), self::TYPE_FOOTER => '🔻 ' . __( 'Footer', 'addronix-addons-for-elementor' ), self::TYPE_LOOP => '🔁 ' . __( 'Loop Item', 'addronix-addons-for-elementor' ), self::TYPE_MEGA_MENU => '🚀 ' . __( 'Mega Menu', 'addronix-addons-for-elementor' ), ]; echo isset( $labels[ $type ] ) ? esc_html( $labels[ $type ] ) : '—'; } /** * Append the `aafe_type` parameter to the view links (All, Published, etc.) * * @param array $views * @return array */ public function filter_views_links( $views ) { if ( isset( $_GET['aafe_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading from GET for admin UI state. $type = sanitize_key( wp_unslash( $_GET['aafe_type'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading from GET for admin UI state. foreach ( $views as $key => $view ) { $views[ $key ] = preg_replace( '/href=([\'"])(.*?)\1/', 'href=$1$2&aafe_type=' . $type . '$1', $view ); } } return $views; } /** * Add hidden input for `aafe_type` in the admin list filter form. * * @param string $post_type */ public function add_hidden_type_input( $post_type ) { if ( self::CPT === $post_type && isset( $_GET['aafe_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading from GET for admin UI state. echo ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading from GET for admin UI state. } } /** * JS: Fix the "Add New" button on filtered admin list pages * to carry the `aafe_type` URL parameter into the new post URL. */ public function admin_footer_js() { $screen = get_current_screen(); if ( ! $screen || 'edit' !== $screen->base || self::CPT !== $screen->post_type ) { return; } $type = isset( $_GET['aafe_type'] ) ? sanitize_key( wp_unslash( $_GET['aafe_type'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading from GET for admin UI state. if ( ! $type ) { return; } // Use wp_add_inline_script instead of raw