add_filter( 'use_block_editor_for_post_type', $this, 'filter_post_type', 999, 2 ); $loader->add_filter( 'use_block_editor_for_post', $this, 'filter_post', 999, 2 ); $loader->add_action( 'add_meta_boxes', $this, 'add_meta_box' ); $loader->add_action( 'admin_post_acc_switch_editor', $this, 'handle_switch' ); $loader->add_filter( 'post_row_actions', $this, 'row_actions', 10, 2 ); } public static function uses_avada( int $post_id ): bool { $choice = (string) get_post_meta( $post_id, self::META, true ); if ( 'avada' === $choice ) { return true; } if ( 'blocks' === $choice ) { return false; } return 'active' === get_post_meta( $post_id, 'fusion_builder_status', true ) || false !== strpos( (string) get_post_field( 'post_content', $post_id ), '[fusion_builder_container' ); } public function filter_post_type( bool $use, string $post_type ): bool { return in_array( $post_type, self::POST_TYPES, true ) ? true : $use; } public function filter_post( bool $use, $post ): bool { if ( ! $post instanceof \WP_Post || ! in_array( $post->post_type, self::POST_TYPES, true ) ) { return $use; } return ! self::uses_avada( (int) $post->ID ); } private function switch_url( int $post_id, string $to ): string { return wp_nonce_url( add_query_arg( [ 'action' => 'acc_switch_editor', 'post' => $post_id, 'to' => $to ], admin_url( 'admin-post.php' ) ), 'acc_switch_editor_' . $post_id ); } public function add_meta_box(): void { foreach ( self::POST_TYPES as $post_type ) { add_meta_box( 'academiccrew_editor_switch', __( 'Editor', 'academiccrew' ), [ $this, 'render_meta_box' ], $post_type, 'side', 'high' ); } } public function render_meta_box( \WP_Post $post ): void { if ( 'auto-draft' === $post->post_status ) { echo '

' . esc_html__( 'Save a draft first, then you can switch between the block editor and Avada Builder.', 'academiccrew' ) . '

'; return; } $avada = self::uses_avada( (int) $post->ID ); if ( $avada ) { echo '

' . esc_html__( 'This page is edited with Avada Builder.', 'academiccrew' ) . '

'; $to = 'blocks'; $label = __( 'Switch to the block editor', 'academiccrew' ); $confirm = __( 'The Avada layout will show as raw shortcodes in the block editor and cannot be edited with Avada Builder while you use blocks. Continue?', 'academiccrew' ); } else { echo '

' . esc_html__( 'This page is edited with the block editor.', 'academiccrew' ) . '

'; $to = 'avada'; $label = __( 'Switch to Avada Builder', 'academiccrew' ); $confirm = __( 'Avada Builder cannot edit content written with blocks: it will open the text as plain content. Continue?', 'academiccrew' ); } printf( '%s', esc_url( $this->switch_url( (int) $post->ID, $to ) ), esc_attr( wp_json_encode( $confirm ) ), esc_html( $label ) ); echo '

' . esc_html__( 'Save your changes before switching.', 'academiccrew' ) . '

'; } public function handle_switch(): void { $post_id = isset( $_GET['post'] ) ? (int) $_GET['post'] : 0; check_admin_referer( 'acc_switch_editor_' . $post_id ); if ( $post_id <= 0 || ! in_array( get_post_type( $post_id ), self::POST_TYPES, true ) || ! current_user_can( 'edit_post', $post_id ) ) { wp_die( esc_html__( 'Not allowed.', 'academiccrew' ) ); } if ( 'avada' === ( $_GET['to'] ?? '' ) ) { update_post_meta( $post_id, self::META, 'avada' ); update_post_meta( $post_id, 'fusion_builder_status', 'active' ); } else { update_post_meta( $post_id, self::META, 'blocks' ); update_post_meta( $post_id, 'fusion_builder_status', '' ); } wp_safe_redirect( admin_url( 'post.php?post=' . $post_id . '&action=edit' ) ); exit; } public function row_actions( array $actions, \WP_Post $post ): array { if ( in_array( $post->post_type, self::POST_TYPES, true ) && current_user_can( 'edit_post', $post->ID ) ) { $avada = self::uses_avada( (int) $post->ID ); $actions['acc_editor'] = sprintf( '%s', esc_url( $this->switch_url( (int) $post->ID, $avada ? 'blocks' : 'avada' ) ), esc_html( $avada ? __( 'Edit with blocks', 'academiccrew' ) : __( 'Edit with Avada', 'academiccrew' ) ) ); } return $actions; } }