save_optimized_css( 'base' . $key, isset( $data['optimized_styles'] ) ? $data['optimized_styles'] : '' ); Js::get_instance( $post_id, $key )->save_js_modules( isset( $data['js_modules'] ) ? $data['js_modules'] : [] ); static::optimized_advanced_assets( $post_id, $data, $key ); } /** * Handles the save of advanced assets, for now Woocommerce and Gutenberg * * @param $post_id * @param $data */ public static function optimized_advanced_assets( $post_id, $data, $key = '' ) { if ( \TCB\Integrations\WooCommerce\Main::active() ) { Js::get_instance( $post_id, '_woo' . $key )->save_js_modules( isset( $data['woo_modules'] ) ? $data['woo_modules'] : [] ); } update_post_meta( $post_id, Gutenberg::HAS_GUTENBERG, isset( $data['gutenberg_modules'] ) ? $data['gutenberg_modules'] : [] ); } /** * @param $id */ public static function preload_assets( $id ) { if ( is_editor_page_raw( true ) ) { return; } $assets_to_preload = get_post_meta( $id, static::ASSETS_TO_PRELOAD, true ); if ( ! empty( $assets_to_preload ) ) { foreach ( $assets_to_preload as $asset ) { switch ( $asset['type'] ) { case 'image': echo static::get_preload_link( $asset['url'], 'image' ); break; case 'dynamic': if ( $asset['url'] === 'featured' && has_post_thumbnail() ) { echo static::get_preload_link( \TCB_Post_List_Shortcodes::shortcode_function_content( 'the_post_thumbnail_url' ), 'image' ); } break; default: break; } } } do_action( 'tve_lightspeed_preload_assets', $id ); } /** * Function called to make sure the lightspeed optimization is active when a plugin is activated */ public static function first_time_enable_lightspeed() { $lightspeed = get_option( '_tve_enable_lightspeed' ); if ( $lightspeed === false ) { update_option( '_tve_enable_lightspeed', 1 ); } } /** * @param $url * @param $type * * @return string */ public static function get_preload_link( $url, $type ) { return ''; } /** * Get the excluded post types * * @return mixed|void */ public static function get_excluded_post_types() { /** * Filter posts we don't want to optimize * * @param array $post_types * * @return array */ return apply_filters( 'tcb_lightspeed_excluded_post_types', [ 'attachment', \TVD\Login_Editor\Post_Type::NAME, 'product_variation', //TODO: this is just temporary 'tva_module', 'tva_lesson', 'tvo_capture', 'tvo_display', 'tcb_content_template', 'shortcodesultimate', /* 'Shortcodes Ultimate' plugin */ 'tm_global_cp', /* 'Extra Checkout Options for WooCommerce TM Extra Product Options' plugin */ ] ); } /** * Return all posts that have architect content saved * * @return array */ public static function get_architect_posts_for_optimization() { $excluded_post_types = static::get_excluded_post_types(); $all_post_types = array_filter( array_values( get_post_types() ), static function ( $post_type ) use ( $excluded_post_types ) { return ! in_array( $post_type, $excluded_post_types, true ); } ); $posts = get_posts( [ 'posts_per_page' => - 1, 'post_type' => $all_post_types, 'fields' => 'ids', /* exclude blog page */ 'post__not_in' => [ get_option( 'page_for_posts' ) ], 'update_post_meta_cache' => false, 'meta_query' => [ 'relation' => 'OR', [ 'key' => 'tve_custom_css', 'compare' => 'EXISTS', ], [ 'key' => 'tve_landing_page', 'compare' => 'EXISTS', ], ], ] ); $groups = []; foreach ( $posts as $post_id ) { $post = get_post( $post_id ); if ( empty( $groups[ $post->post_type ] ) ) { $groups[ $post->post_type ] = static::prepare_post_type( $post->post_type ); } $groups[ $post->post_type ]['items'][] = [ 'id' => $post->ID, 'name' => $post->post_title, 'optimized' => (int) $post->{static::OPTIMIZATION_VERSION_META} === static::LIGHTSPEED_VERSION ? 1 : 0, 'url' => get_permalink( $post->ID ), ]; } return $groups; } /** * This content includes everything but LPs * * @return array|void */ public static function get_content_for_optimization( $request ) { $excluded_post_types = static::get_excluded_post_types(); $excluded_post_types[] = 'tve_landing_page'; $all_post_types = array_filter( array_values( get_post_types() ), static function ( $post_type ) use ( $excluded_post_types ) { return ! in_array( $post_type, $excluded_post_types, true ); } ); $posts = get_posts( [ 'posts_per_page' => - 1, 'post_type' => $all_post_types, 'fields' => 'ids', /* exclude blog page */ 'post__not_in' => [ get_option( 'page_for_posts' ) ], 'update_post_meta_cache' => false, 'meta_query' => [ [ 'key' => 'tve_custom_css', 'compare' => 'EXISTS', ], ], ] ); $groups = static::get_advanced_groups( $posts ); return array_merge( $groups, apply_filters( 'tve_lightspeed_items_to_optimize', $groups, $request ) ); } public static function get_lp_for_optimize() { $posts = get_posts( [ 'posts_per_page' => - 1, 'post_type' => 'page', 'fields' => 'ids', /* exclude blog page */ 'post__not_in' => [ get_option( 'page_for_posts' ) ], 'update_post_meta_cache' => false, 'meta_query' => [ [ 'key' => 'tve_landing_page', 'compare' => 'EXISTS', ], ], ] ); return static::get_advanced_groups( $posts ); } /** * Get the advanced(that have woo or gutenberg) groups to be optimized * * @param $posts * * @return array */ public static function get_advanced_groups( $posts ) { $groups = []; foreach ( $posts as $post_id ) { $post = get_post( $post_id ); if ( empty( $groups[ $post->post_type ] ) ) { $groups[ $post->post_type ] = static::prepare_post_type( $post->post_type ); } $item = [ 'id' => $post->ID, 'name' => $post->post_title, 'gutenberg_optimized' => metadata_exists( 'post', $post->ID, Gutenberg::HAS_GUTENBERG ), 'url' => get_permalink( $post->ID ), ]; if ( \TCB\Integrations\WooCommerce\Main::active() ) { $item['woo_optimized'] = metadata_exists( 'post', $post->ID, Woocommerce::WOO_MODULE_META_NAME ); } $groups[ $post->post_type ]['items'][] = $item; } return $groups; } public static function prepare_post_type( $post_type ) { $post_type_object = get_post_type_object( $post_type ); return [ 'type' => $post_type, 'label' => $post_type_object === null ? $post_type : $post_type_object->label, 'items' => [], ]; } }