post_content; $backup = self::backup( $id ); if ( $backup ) { $source = $backup['content']; } $conv = new Converter(); return $conv->convert( $source, Context::for_post( $id ) ); } /** * Convert a post and save it. * * @param int $id Post ID. * @param bool $dry_run Only report what would happen. * @return array{report:array}|\WP_Error */ public static function convert( $id, $dry_run = false ) { $id = (int) $id; if ( ! current_user_can( 'edit_post', $id ) && ! self::cli() ) { return new \WP_Error( 'bakeout_cap', __( 'You are not allowed to edit this post.', 'bakeout' ) ); } if ( self::backup( $id ) ) { return new \WP_Error( 'bakeout_done', __( 'This post is already converted. Undo it first to convert it again.', 'bakeout' ) ); } $post = get_post( $id ); if ( ! $post ) { return new \WP_Error( 'bakeout_missing', __( 'That post no longer exists.', 'bakeout' ) ); } $result = self::preview( $id ); if ( is_wp_error( $result ) || $dry_run ) { return $result; } $meta = array(); foreach ( self::WPB_META as $key ) { if ( metadata_exists( 'post', $id, $key ) ) { $meta[ $key ] = get_post_meta( $id, $key, true ); } } $backup = array( 'content' => (string) $post->post_content, 'meta' => $meta, 'time' => time(), 'user' => get_current_user_id(), 'version' => BAKEOUT_VERSION, ); // Store the backup first: if the update fails, nothing is lost and Undo still works. update_post_meta( $id, self::BACKUP, wp_slash( $backup ) ); $saved = wp_update_post( wp_slash( array( 'ID' => $id, 'post_content' => $result['markup'], ) ), true ); if ( is_wp_error( $saved ) ) { delete_post_meta( $id, self::BACKUP ); return $saved; } // WPBakery would otherwise open its own editor on this post. update_post_meta( $id, '_wpb_vc_js_status', 'false' ); update_post_meta( $id, self::REPORT, wp_slash( $result['report'] ) ); Scanner::refresh( $id ); /** * A post was converted. * * @param int $id Post ID. * @param array $result Conversion result. */ do_action( 'bakeout_converted', $id, $result ); return $result; } /** * Restore a converted post. * * @param int $id Post ID. * @return true|\WP_Error */ public static function undo( $id ) { $id = (int) $id; if ( ! current_user_can( 'edit_post', $id ) && ! self::cli() ) { return new \WP_Error( 'bakeout_cap', __( 'You are not allowed to edit this post.', 'bakeout' ) ); } $backup = self::backup( $id ); if ( ! $backup ) { return new \WP_Error( 'bakeout_nobackup', __( 'There is nothing to undo for this post.', 'bakeout' ) ); } $saved = wp_update_post( wp_slash( array( 'ID' => $id, 'post_content' => $backup['content'], ) ), true ); if ( is_wp_error( $saved ) ) { return $saved; } foreach ( self::WPB_META as $key ) { if ( array_key_exists( $key, $backup['meta'] ) ) { update_post_meta( $id, $key, wp_slash( $backup['meta'][ $key ] ) ); } else { delete_post_meta( $id, $key ); } } delete_post_meta( $id, self::BACKUP ); delete_post_meta( $id, self::REPORT ); Scanner::refresh( $id ); do_action( 'bakeout_undone', $id ); return true; } /** * Running from WP-CLI with no user set (the command line has full access). * * @return bool */ private static function cli() { return defined( 'WP_CLI' ) && WP_CLI && 0 === get_current_user_id(); } /** * The backup of a converted post, or null. * * @param int $id Post ID. * @return array{content:string,meta:array,time:int}|null */ public static function backup( $id ) { $b = get_post_meta( (int) $id, self::BACKUP, true ); return ( is_array( $b ) && isset( $b['content'] ) ) ? $b + array( 'meta' => array(), 'time' => 0, ) : null; } }