get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_type = %s AND post_status NOT IN ('trash','auto-draft','inherit') AND ( post_content LIKE %s OR ID IN ( SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s ) ) ORDER BY ID", $type, '%' . $wpdb->esc_like( '[vc_' ) . '%', Store::BACKUP ) ); // phpcs:enable $ids = array_merge( $ids, (array) $found ); } return array_map( 'intval', (array) $ids ); } /** * Post types to scan: every type with content except WPBakery's own and attachments. * * @return string[] */ public static function post_types() { $types = array_values( array_diff( get_post_types( array(), 'names' ), array( 'attachment', 'revision', 'nav_menu_item', 'customize_changeset', 'oembed_cache', 'user_request', 'wp_global_styles', 'wp_navigation', 'wp_font_family', 'wp_font_face', 'vc_grid_item', 'custom_css', 'wp_template', 'wp_template_part' ) ) ); /** * Post types the scan looks at. * * @param string[] $types Post types. */ return apply_filters( 'bakeout_post_types', $types ); } /** * Start a scan. * * @return array Scan state. */ public static function start() { $state = array( 'started' => time(), 'done' => false, 'queue' => self::candidate_ids(), 'total' => 0, 'posts' => array(), ); $state['total'] = count( $state['queue'] ); update_option( self::OPTION, $state, false ); return $state; } /** * Scan the next batch. * * @return array Scan state. */ public static function step() { $state = self::state(); if ( ! $state || $state['done'] ) { return $state ? $state : self::start(); } $batch = array_splice( $state['queue'], 0, self::BATCH ); $conv = new Converter(); foreach ( $batch as $id ) { $row = self::scan_post( (int) $id, $conv ); if ( $row ) { $state['posts'][ $id ] = $row; } } $state['done'] = ! $state['queue']; update_option( self::OPTION, $state, false ); return $state; } /** * Re-scan one post (after converting or undoing it). * * @param int $id Post ID. * @return void */ public static function refresh( $id ) { $state = self::state(); if ( ! $state ) { return; } $row = self::scan_post( (int) $id, new Converter() ); if ( $row ) { $state['posts'][ $id ] = $row; } else { unset( $state['posts'][ $id ] ); } update_option( self::OPTION, $state, false ); } /** * The stored scan, or null. * * @return array|null */ public static function state() { $state = get_option( self::OPTION, null ); return is_array( $state ) ? $state : null; } /** * One post's row. * * @param int $id Post ID. * @param Converter $conv Converter. * @return array|null */ public static function scan_post( $id, Converter $conv ) { $post = get_post( $id ); if ( ! $post ) { return null; } // A converted page is described by what it was (its backup), so the figures stay meaningful. $backup = Store::backup( $id ); $converted = (bool) $backup; $row = self::analyse( $converted ? $backup['content'] : (string) $post->post_content, $conv, Context::for_post( $id ) ); if ( ! $converted && 0 === $row['builder'] ) { return null; } $row += array( 'id' => $id, 'title' => get_the_title( $post ), 'type' => $post->post_type, 'status' => $post->post_status, 'converted' => $converted, 'css' => '' !== (string) get_post_meta( $id, '_wpb_post_custom_css', true ), ); return $row; } /** * Count a content's elements. * * @param string $content Content. * @param Converter $conv Converter. * @param Context $ctx Context (decides free or Pro). * @return array{tags:array,builder:int,native:int,pro:int,theme:array,other:array,percent:int} */ public static function analyse( $content, Converter $conv, Context $ctx ) { $row = array( 'tags' => array(), 'builder' => 0, 'native' => 0, 'pro' => 0, 'theme' => array(), 'other' => array(), 'percent' => 100, ); Parser::walk( Parser::parse( $content ), static function ( $node ) use ( &$row, $conv, $ctx ) { $tag = $node['tag']; if ( ! isset( $row['tags'][ $tag ] ) ) { $row['tags'][ $tag ] = 0; } ++$row['tags'][ $tag ]; if ( 0 === strpos( $tag, 'vc_' ) ) { ++$row['builder']; if ( $conv->handles( $tag, $ctx ) ) { ++$row['native']; } elseif ( in_array( $tag, Converter::PRO_TAGS, true ) ) { ++$row['pro']; } } elseif ( '' !== Converter::theme_of( $tag ) ) { ++$row['builder']; $name = Converter::theme_of( $tag ); if ( ! isset( $row['theme'][ $name ] ) ) { $row['theme'][ $name ] = 0; } ++$row['theme'][ $name ]; } elseif ( ! in_array( $tag, Converter::WP_TAGS, true ) ) { if ( ! isset( $row['other'][ $tag ] ) ) { $row['other'][ $tag ] = 0; } ++$row['other'][ $tag ]; } } ); $row['percent'] = $row['builder'] ? (int) floor( 100 * $row['native'] / $row['builder'] ) : 100; return $row; } /** * Totals over the whole scan. * * @param array $state Scan state. * @return array */ public static function summary( array $state ) { $sum = array( 'posts' => 0, 'converted' => 0, 'builder' => 0, 'native' => 0, 'pro' => 0, 'tags' => array(), 'theme' => array(), 'other' => array(), 'css' => 0, 'percent' => 100, ); foreach ( $state['posts'] as $row ) { ++$sum['posts']; $sum['converted'] += $row['converted'] ? 1 : 0; $sum['css'] += ! empty( $row['css'] ) ? 1 : 0; foreach ( array( 'builder', 'native', 'pro' ) as $k ) { $sum[ $k ] += $row[ $k ]; } foreach ( array( 'tags', 'theme', 'other' ) as $k ) { foreach ( $row[ $k ] as $tag => $n ) { $sum[ $k ][ $tag ] = ( isset( $sum[ $k ][ $tag ] ) ? $sum[ $k ][ $tag ] : 0 ) + $n; } } } arsort( $sum['tags'] ); $sum['percent'] = $sum['builder'] ? (int) floor( 100 * $sum['native'] / $sum['builder'] ) : 100; return $sum; } }