label. */ public static function get_grid_choices( $include_placeholder = true ) { $limit = apply_filters( 'gridmaster_embed_grid_choices_limit', 500 ); $limit = max( 10, absint( $limit ) ); $grids_result = Grid_Repository::list_grids( array( 'per_page' => $limit, 'orderby' => 'id', 'order' => 'DESC', ) ); $choices = array(); if ( $include_placeholder ) { $choices[0] = __( '— Select a Grid —', 'ajax-filter-posts' ); } if ( ! empty( $grids_result['items'] ) ) { foreach ( $grids_result['items'] as $grid ) { $status_tag = ''; if ( isset( $grid->migration_status ) && 'legacy' === $grid->migration_status ) { $status_tag = ' (' . __( 'Legacy', 'ajax-filter-posts' ) . ')'; } $display_title = ! empty( $grid->title ) ? $grid->title : sprintf( __( 'Grid #%d', 'ajax-filter-posts' ), $grid->id ); $choices[ (int) $grid->id ] = sprintf( '#%d — %s%s', (int) $grid->id, $display_title, $status_tag ); } } return $choices; } /** * Gets the direct editor URL to modify the specified grid. * * Only returns a URL if the current user has manage_options capability. * * @param int $grid_id Grid ID. * @return string Admin URL or empty string if unauthorized / not found. */ public static function get_edit_url( $grid_id ) { if ( ! current_user_can( 'manage_options' ) ) { return ''; } $grid_id = absint( $grid_id ); if ( ! $grid_id ) { return ''; } $grid = Grid_Repository::get( $grid_id ); if ( ! $grid ) { return ''; } $is_legacy = ( isset( $grid->migration_status ) && 'legacy' === $grid->migration_status ); if ( $is_legacy ) { return admin_url( 'admin.php?page=gridmaster&path=build-grid&id=' . $grid_id ); } return admin_url( 'admin.php?page=gridmaster-builder&id=' . $grid_id ); } /** * Gets the URL to create a new grid. * * @return string Admin URL or empty string. */ public static function get_create_url() { if ( ! current_user_can( 'manage_options' ) ) { return ''; } return admin_url( 'admin.php?page=gridmaster-builder' ); } /** * Gets the URL to manage all grids. * * @return string Admin URL or empty string. */ public static function get_manage_url() { if ( ! current_user_can( 'manage_options' ) ) { return ''; } return admin_url( 'admin.php?page=gridmaster' ); } /** * Determines whether the current request is being evaluated inside a page builder editor / preview canvas. * * @return bool */ public static function is_builder_preview() { // Elementor edit or preview mode. if ( did_action( 'elementor/loaded' ) && class_exists( '\Elementor\Plugin' ) ) { if ( \Elementor\Plugin::$instance->editor && \Elementor\Plugin::$instance->editor->is_edit_mode() ) { return true; } if ( \Elementor\Plugin::$instance->preview && \Elementor\Plugin::$instance->preview->is_preview_mode() ) { return true; } } // Beaver Builder active canvas. if ( class_exists( 'FLBuilderModel' ) && method_exists( 'FLBuilderModel', 'is_builder_active' ) ) { if ( \FLBuilderModel::is_builder_active() ) { return true; } } // Bricks Builder canvas. if ( function_exists( 'bricks_is_builder' ) && bricks_is_builder() ) { return true; } if ( function_exists( 'bricks_is_builder_main' ) && bricks_is_builder_main() ) { return true; } if ( function_exists( 'bricks_is_builder_call' ) && bricks_is_builder_call() ) { return true; } // WPBakery frontend editor. if ( function_exists( 'vc_is_page_editable' ) && vc_is_page_editable() ) { return true; } // Gutenberg REST embed preview. if ( defined( 'REST_REQUEST' ) && REST_REQUEST && isset( $_SERVER['REQUEST_URI'] ) && false !== strpos( $_SERVER['REQUEST_URI'], 'embed-preview' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput return true; } return false; } /** * Renders a grid by numeric ID with builder-aware empty states and edit links. * * @param int $grid_id Grid ID. * @param array $args Optional rendering arguments. * @return string HTML output. */ public static function render( $grid_id, $args = array() ) { $grid_id = absint( $grid_id ); $is_preview = self::is_builder_preview(); if ( ! $grid_id ) { if ( $is_preview ) { $create_url = self::get_create_url(); $btn_html = $create_url ? sprintf( ' %s', esc_url( $create_url ), esc_html__( 'Create New Grid', 'ajax-filter-posts' ) ) : ''; return sprintf( '
%s
%s%s