self::SLUG ), $args ), admin_url( 'tools.php' ) ); } /** * Menu entry. * * @return void */ public static function menu() { add_management_page( __( 'Bakeout: move off WPBakery', 'bakeout' ), __( 'Bakeout', 'bakeout' ), self::CAP, self::SLUG, array( __CLASS__, 'render' ) ); } /** * Settings link on the plugins screen. * * @param string[] $links Links. * @return string[] */ public static function action_links( $links ) { array_unshift( $links, '' . esc_html__( 'Scan the site', 'bakeout' ) . '' ); return $links; } /** * Styles and scripts on our screen only. * * @param string $hook Hook suffix. * @return void */ public static function assets( $hook ) { if ( 'tools_page_' . self::SLUG !== $hook ) { return; } wp_enqueue_style( 'bakeout-admin', BAKEOUT_URL . 'assets/admin.css', array(), BAKEOUT_VERSION ); wp_enqueue_script( 'bakeout-admin', BAKEOUT_URL . 'assets/admin.js', array(), BAKEOUT_VERSION, true ); wp_localize_script( 'bakeout-admin', 'bakeoutAdmin', array( 'ajax' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'bakeout_scan' ), /* translators: 1: pages scanned, 2: pages to scan. */ 'scanning' => __( 'Scanning… %1$d of %2$d', 'bakeout' ), 'failed' => __( 'The scan stopped. Reload the page to continue.', 'bakeout' ), ) ); } /* ----- Actions ----- */ /** * Convert one post. * * @return void */ public static function handle_convert() { $id = isset( $_POST['post'] ) ? absint( $_POST['post'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- checked next. check_admin_referer( 'bakeout_convert_' . $id ); if ( ! current_user_can( self::CAP ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'bakeout' ) ); } $result = Store::convert( $id ); self::back( is_wp_error( $result ) ? $result->get_error_message() : 'converted', $id ); } /** * Undo one post. * * @return void */ public static function handle_undo() { $id = isset( $_POST['post'] ) ? absint( $_POST['post'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- checked next. check_admin_referer( 'bakeout_undo_' . $id ); if ( ! current_user_can( self::CAP ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'bakeout' ) ); } $result = Store::undo( $id ); self::back( is_wp_error( $result ) ? $result->get_error_message() : 'undone', $id ); } /** * Save settings. * * @return void */ public static function handle_settings() { check_admin_referer( 'bakeout_settings' ); if ( ! current_user_can( self::CAP ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'bakeout' ) ); } Settings::save( array( 'render' => ! empty( $_POST['render'] ) ) ); wp_safe_redirect( self::url( array( 'tab' => 'settings', 'bakeout_msg' => 'saved' ) ) ); exit; } /** * Start or continue a scan without JavaScript. * * @return void */ public static function handle_scan() { check_admin_referer( 'bakeout_scan' ); if ( ! current_user_can( self::CAP ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'bakeout' ) ); } if ( ! empty( $_POST['restart'] ) ) { Scanner::start(); } $state = Scanner::step(); wp_safe_redirect( self::url( $state['done'] ? array() : array( 'scanning' => 1 ) ) ); exit; } /** * One scan batch for the page's script. * * @return void */ public static function ajax_scan() { check_ajax_referer( 'bakeout_scan' ); if ( ! current_user_can( self::CAP ) ) { wp_send_json_error( null, 403 ); } if ( ! empty( $_POST['restart'] ) ) { Scanner::start(); } $state = Scanner::step(); wp_send_json_success( array( 'done' => (bool) $state['done'], 'total' => (int) $state['total'], 'left' => count( $state['queue'] ), ) ); } /** * Redirect back with a message. * * @param string $msg Message key or text. * @param int $id Post ID. * @return void */ private static function back( $msg, $id ) { $view = isset( $_POST['view'] ) ? sanitize_key( wp_unslash( $_POST['view'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- the caller checked the nonce. $args = array( 'bakeout_msg' => rawurlencode( $msg ) ); // add_query_arg() does not encode values. if ( 'preview' === $view ) { $args['view'] = 'preview'; $args['post'] = $id; } wp_safe_redirect( self::url( $args ) ); exit; } /* ----- Screens ----- */ /** * The screen. * * @return void */ public static function render() { if ( ! current_user_can( self::CAP ) ) { return; } // phpcs:disable WordPress.Security.NonceVerification.Recommended -- read-only view switches. $tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'scan'; $view = isset( $_GET['view'] ) ? sanitize_key( wp_unslash( $_GET['view'] ) ) : ''; $post = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : 0; $msg = isset( $_GET['bakeout_msg'] ) ? sanitize_text_field( wp_unslash( $_GET['bakeout_msg'] ) ) : ''; // phpcs:enable $tabs = apply_filters( 'bakeout_admin_tabs', array( 'scan' => __( 'Scan and convert', 'bakeout' ), 'settings' => __( 'Settings', 'bakeout' ), ) ); echo '
'; echo '

' . esc_html__( 'Bakeout', 'bakeout' ) . ' ' . esc_html__( 'move your pages off WPBakery onto blocks', 'bakeout' ) . '

'; self::message( $msg ); if ( 'preview' === $view && $post ) { self::preview_screen( $post ); echo '
'; return; } echo ''; if ( 'settings' === $tab ) { self::settings_screen(); } elseif ( 'scan' === $tab ) { self::scan_screen(); } else { do_action( 'bakeout_admin_tab_' . $tab ); } echo ''; } /** * A notice for the last action. * * @param string $msg Message. * @return void */ private static function message( $msg ) { if ( '' === $msg ) { return; } $known = array( 'converted' => array( 'success', __( 'Converted. The old version is kept: use Undo to restore it.', 'bakeout' ) ), 'undone' => array( 'success', __( 'Restored to the WPBakery version.', 'bakeout' ) ), 'saved' => array( 'success', __( 'Settings saved.', 'bakeout' ) ), ); list( $type, $text ) = isset( $known[ $msg ] ) ? $known[ $msg ] : array( 'error', $msg ); printf( '

%s

', esc_attr( $type ), esc_html( $text ) ); } /** * The scan screen. * * @return void */ private static function scan_screen() { $state = Scanner::state(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- view switch. $scanning = ! empty( $_GET['scanning'] ) || ( $state && ! $state['done'] ); echo '

' . esc_html__( 'Bakeout reads your pages, turns every WPBakery element it knows into the matching block, and keeps anything else as a Shortcode block, so nothing on a page is lost. Each page keeps a backup: Undo puts it back exactly.', 'bakeout' ) . '

'; self::theme_warning(); echo '
'; wp_nonce_field( 'bakeout_scan' ); echo ''; echo ' '; echo '
'; if ( ! $state ) { return; } $sum = Scanner::summary( $state ); self::summary_box( $sum ); do_action( 'bakeout_after_summary', $sum, $state ); self::posts_table( $state ); self::elements_table( $sum ); } /** * The warning about changing theme. * * @return void */ private static function theme_warning() { echo '

' . esc_html__( 'Bakeout moves the content of your pages, not your theme.', 'bakeout' ) . ' ' . esc_html__( 'Your header, footer, sidebar, fonts and colours come from the theme. If you also switch to a new theme, those will change, and elements that belong to the old theme stop working. The scan lists them as theme elements.', 'bakeout' ) . '

'; } /** * Totals. * * @param array $sum Summary. * @return void */ private static function summary_box( array $sum ) { echo '
'; self::stat( (string) $sum['posts'], __( 'pages and posts with WPBakery content', 'bakeout' ) ); self::stat( (string) $sum['converted'], __( 'converted so far', 'bakeout' ) ); self::stat( (string) $sum['builder'], __( 'page-builder elements', 'bakeout' ) ); self::stat( $sum['percent'] . '%', __( 'convert to blocks automatically', 'bakeout' ) ); echo '
'; $lines = array(); if ( $sum['pro'] && ! apply_filters( 'bakeout_pro_active', false ) ) { /* translators: %d: number of elements. */ $lines[] = sprintf( _n( '%d element (tabs, charts, post grids, widgets and similar) is converted by Bakeout Pro. Without it, it stays as a Shortcode block that needs WPBakery to show.', '%d elements (tabs, charts, post grids, widgets and similar) are converted by Bakeout Pro. Without it, they stay as Shortcode blocks that need WPBakery to show.', $sum['pro'], 'bakeout' ), $sum['pro'] ); } foreach ( $sum['theme'] as $theme => $n ) { /* translators: 1: number of elements, 2: theme or add-on name. */ $lines[] = sprintf( _n( '%1$d element belongs to %2$s. It is kept as a Shortcode block and works only while %2$s is active.', '%1$d elements belong to %2$s. They are kept as Shortcode blocks and work only while %2$s is active.', $n, 'bakeout' ), $n, $theme ); } if ( $sum['other'] ) { /* translators: %s: list of shortcode names. */ $lines[] = sprintf( __( 'Shortcodes from other plugins are kept as they are and keep working while those plugins are active: %s.', 'bakeout' ), implode( ', ', array_map( static function ( $t ) { return '[' . $t . ']'; }, array_keys( $sum['other'] ) ) ) ); } if ( $sum['css'] ) { /* translators: %d: number of pages. */ $lines[] = sprintf( _n( '%d page has its own custom CSS in WPBakery. Bakeout Pro moves it with the page.', '%d pages have their own custom CSS in WPBakery. Bakeout Pro moves it with the page.', $sum['css'], 'bakeout' ), $sum['css'] ); } if ( $lines ) { echo ''; } } /** * One figure. * * @param string $value Value. * @param string $label Label. * @return void */ private static function stat( $value, $label ) { echo '
' . esc_html( $value ) . '' . esc_html( $label ) . '
'; } /** * Pages table. * * @param array $state Scan state. * @return void */ private static function posts_table( array $state ) { if ( ! $state['posts'] ) { echo '

' . esc_html__( 'No WPBakery content was found.', 'bakeout' ) . '

'; return; } echo '

' . esc_html__( 'Pages', 'bakeout' ) . '

'; echo ''; foreach ( $state['posts'] as $row ) { $id = (int) $row['id']; $title = '' === $row['title'] ? __( '(no title)', 'bakeout' ) : $row['title']; echo ''; } echo '
' . esc_html__( 'Title', 'bakeout' ) . '' . esc_html__( 'Type', 'bakeout' ) . '' . esc_html__( 'Elements', 'bakeout' ) . '' . esc_html__( 'Converts automatically', 'bakeout' ) . '' . esc_html__( 'State', 'bakeout' ) . '' . esc_html__( 'Actions', 'bakeout' ) . '
' . esc_html( $title ) . ''; if ( 'publish' !== $row['status'] ) { echo ' (' . esc_html( $row['status'] ) . ')'; } if ( $row['theme'] ) { echo ' ' . esc_html( implode( ', ', array_keys( $row['theme'] ) ) ) . ''; } echo '' . esc_html( $row['type'] ) . '' . (int) $row['builder'] . '' . (int) $row['percent'] . '%'; echo $row['converted'] ? '' . esc_html__( 'Converted', 'bakeout' ) . '' : esc_html__( 'Not converted', 'bakeout' ); echo ''; echo '' . esc_html__( 'Preview', 'bakeout' ) . ' '; self::action_button( $id, $row['converted'] ? 'undo' : 'convert', '' ); echo '
'; } /** * Convert or Undo button. * * @param int $id Post ID. * @param string $action convert|undo. * @param string $view View to return to. * @return void */ private static function action_button( $id, $action, $view ) { echo '
'; wp_nonce_field( 'bakeout_' . $action . '_' . $id ); echo ''; if ( 'undo' === $action ) { echo ''; } else { echo ''; } echo '
'; } /** * Elements breakdown. * * @param array $sum Summary. * @return void */ private static function elements_table( array $sum ) { if ( ! $sum['tags'] ) { return; } $conv = new Converter(); $ctx = new Context(); do_action( 'bakeout_context', $ctx ); echo '

' . esc_html__( 'Elements', 'bakeout' ) . '

'; foreach ( $sum['tags'] as $tag => $n ) { if ( $conv->handles( $tag, $ctx ) ) { $what = __( 'Converted to blocks', 'bakeout' ); } elseif ( in_array( $tag, Converter::PRO_TAGS, true ) ) { $what = __( 'Converted by Bakeout Pro; kept as a shortcode otherwise', 'bakeout' ); } elseif ( '' !== Converter::theme_of( $tag ) ) { /* translators: %s: theme or add-on name. */ $what = sprintf( __( 'Kept as a shortcode: belongs to %s', 'bakeout' ), Converter::theme_of( $tag ) ); } elseif ( in_array( $tag, Converter::WP_TAGS, true ) ) { $what = __( 'WordPress shortcode: keeps working', 'bakeout' ); } else { $what = __( 'Kept as a shortcode', 'bakeout' ); } echo ''; } echo '
' . esc_html__( 'Shortcode', 'bakeout' ) . '' . esc_html__( 'Times used', 'bakeout' ) . '' . esc_html__( 'What happens', 'bakeout' ) . '
[' . esc_html( $tag ) . ']' . (int) $n . '' . esc_html( $what ) . '
'; } /** * Before/after preview of one post. * * @param int $id Post ID. * @return void */ private static function preview_screen( $id ) { $post = get_post( $id ); if ( ! $post || ! current_user_can( 'edit_post', $id ) ) { echo '

' . esc_html__( 'That post cannot be shown.', 'bakeout' ) . '

'; return; } $converted = (bool) Store::backup( $id ); $result = Store::preview( $id ); echo '

← ' . esc_html__( 'Back to the scan', 'bakeout' ) . '

'; echo '

' . esc_html( get_the_title( $post ) ) . '

'; echo '
'; self::action_button( $id, $converted ? 'undo' : 'convert', 'preview' ); echo ' '; echo '
'; if ( is_wp_error( $result ) ) { echo '

' . esc_html( $result->get_error_message() ) . '

'; return; } self::report( $result['report'] ); echo '
'; if ( $converted ) { $label = __( 'The live page, now in blocks', 'bakeout' ); echo '
' . esc_html( $label ) . '
'; } else { $before = __( 'Now', 'bakeout' ); $after = __( 'After converting', 'bakeout' ); echo '
' . esc_html( $before ) . '
'; echo '
' . esc_html( $after ) . '
'; } echo '
'; echo '
' . esc_html__( 'Block markup', 'bakeout' ) . '
'; } /** * What the conversion did. * * @param array $report Report. * @return void */ public static function report( array $report ) { $conv = array_sum( $report['converted'] ); $kept = array_sum( $report['kept'] ); $rend = array_sum( $report['rendered'] ); echo '

'; /* translators: %d: number of elements. */ echo esc_html( sprintf( _n( '%d element converted to blocks.', '%d elements converted to blocks.', $conv, 'bakeout' ), $conv ) ) . ' '; if ( $rend ) { /* translators: %d: number of elements. */ echo esc_html( sprintf( _n( '%d element kept as static HTML.', '%d elements kept as static HTML.', $rend, 'bakeout' ), $rend ) ) . ' '; } if ( $kept ) { /* translators: 1: number of elements, 2: list of shortcodes. */ echo esc_html( sprintf( _n( '%1$d element kept as a shortcode: %2$s.', '%1$d elements kept as shortcodes: %2$s.', $kept, 'bakeout' ), $kept, implode( ', ', array_keys( $report['kept'] ) ) ) ); } echo '

'; if ( $report['notes'] ) { echo ''; } echo '
'; } /** * Settings. * * @return void */ private static function settings_screen() { $s = Settings::get(); echo '
'; wp_nonce_field( 'bakeout_settings' ); echo '
' . esc_html__( 'Elements with no block', 'bakeout' ) . '

' . esc_html__( 'Needs WPBakery to be active while converting. Static HTML keeps the look, but is edited as HTML afterwards.', 'bakeout' ) . '

'; do_action( 'bakeout_settings_fields' ); submit_button(); echo '
'; } }