Changeset 2847523
- Timestamp:
- 01/12/2023 04:13:31 PM (2 years ago)
- Location:
- fcp-first-screen-css/trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
fcp-first-screen-css/trunk/README.md
r2831743 r2847523 1 1 # FCP First Screen CSS 2 2 3 Insert inline CSS to the head of the website, so the first screen renders with no jumps, which might improve the CLS and FCP web vitals. Or for any other reason.3 Insert the inline CSS to the head of the website, disable existing styles and scripts, defer loading of not-first-screen style, apply to a single post or bulk. 4 4 5 5 ## Features 6 6 7 7 * Apply to any single post / page / custom post-type 8 * Apply to all posts of a particular p ublic post-type9 * Apply to the blog or the archive page of a post-type with archive support8 * Apply to all posts of a particular post-type 9 * Apply to the blog or the archive page of a post-type 10 10 * It minifies the css before printing 11 * Deregister enqueued styles by name 12 * Apply non-first-screen css separately 11 * Deregister enqueued styles and scripts by name 12 * Apply not-first-screen CSS separately 13 * Defer the not-first-screen CSS loading 13 14 14 15 ## Usage 15 16 16 17 * Install and activate the plugin 17 * Go to "FirstScreen CSS" menu item in the left sidebar of your wp-admin18 * Go to the "First Screen CSS" menu item in the left sidebar of your wp-admin 18 19 * Add New, insert your CSS 19 * To apply the CSS, pick where to at the bottom of the page 20 * To apply to a single post / page - go to that post editor, and select your created FirstScreen CSS in the box in the right sidebar 20 * Pick where to apply and other options -
fcp-first-screen-css/trunk/changelog.txt
r2831743 r2847523 1 = 1.3 = 2 3 * Added the development mode, visible only to admins 4 * Added the option to deregister existing styles and scripts 5 * Added the option to defer the not-first-screen CSS loading 6 1 7 = 1.2 = 2 8 -
fcp-first-screen-css/trunk/first-screen.php
r2845082 r2847523 3 3 Plugin Name: FCP First Screen CSS 4 4 Description: Insert inline CSS to the head of the website, so the first screen renders with no jumps, which might improve the CLS web vital. Or for any other reason. 5 Version: 1. 2.15 Version: 1.3.0 6 6 Requires at least: 5.8 7 7 Tested up to: 6.1 … … 47 47 unset( $css_id ); 48 48 // get css by post-type 49 if ( (int) get_option('page_on_front') !== (int) $qo->ID ) { // exclude the front-page, as they are mostly stand out49 if ( (int) get_option('page_on_front') !== (int) $qo->ID ) { // exclude the front-page, as they stand out, mostly 50 50 $csss = array_merge( $csss, get_css_ids( FCPFSC_PREF.'post-types', $post_type ) ); 51 51 } … … 62 62 if ( empty( $csss ) ) { return; } 63 63 64 // filter by p ublished & post-type64 // filter by post_status, post_type, development-mode 65 65 $csss = filter_csss( $csss ); 66 66 … … 80 80 // deregister existing styles 81 81 $deregister_styles = function() use ( $csss ) { 82 foreach ( get_css_to_deregister( $csss ) as $v ) { 82 list( $styles, $scripts ) = get_to_deregister( $csss ); 83 foreach ( $styles as $v ) { 83 84 wp_deregister_style( $v ); 85 } 86 foreach ( $scripts as $v ) { 87 wp_deregister_script( $v ); 84 88 } 85 89 }; … … 102 106 ); 103 107 } 108 109 // defer loading 110 $defer_csss = get_to_defer( $csss ); 111 add_filter( 'style_loader_tag', function ($tag, $handle) use ($defer_csss) { 112 if ( strpos( $handle, 'first-screen-css-rest-' ) === false || !in_array( str_replace( 'first-screen-css-rest-', '', $handle ), $defer_csss ) ) { 113 return $tag; 114 } 115 return 116 str_replace( [ 'rel="stylesheet"', "rel='stylesheet'" ], [ 117 'rel="preload" as="style" onload="this.onload=null;this.rel=\'stylesheet\'"', 118 "rel='preload' as='style' onload='this.onload=null;this.rel=\"stylesheet\"'" 119 ], $tag ). 120 '<noscript>'.str_replace( // remove doubling id 121 [ ' id="'.$handle.'-css"', " id='".$handle."-css'" ], 122 [ '', '' ], 123 substr( $tag, 0, -1 ) 124 ).'</noscript>' . "\n" 125 ; 126 }, 10, 2); 104 127 }, 10 ); 105 128 … … 169 192 add_meta_box( 170 193 'first-screen-css-bulk', 171 'Bulk apply the First Screen CSS',194 'Bulk apply', 172 195 'FCP\FirstScreenCSS\fcpfsc_meta_bulk_apply', 173 196 FCPFSC_SLUG, … … 177 200 add_meta_box( 178 201 'first-screen-css-disable', 179 'Disable enqueued styles',202 'Disable enqueued', 180 203 'FCP\FirstScreenCSS\fcpfsc_meta_disable_styles', 181 204 FCPFSC_SLUG, … … 192 215 ); 193 216 217 if ( !current_user_can( 'administrator' ) ) { return; } 194 218 list( 'public' => $public_post_types ) = get_all_post_types(); 195 219 add_meta_box( … … 269 293 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } 270 294 if ( !wp_verify_nonce( $_POST[ FCPFSC_PREF.'nounce-name' ], FCPFSC_PREF.'nounce-action' ) ) { return; } 271 if ( !current_user_can( 'edit_post', $postID ) ) { return; } 295 //if ( !current_user_can( 'edit_post', $postID ) ) { return; } 296 if ( !current_user_can( 'administrator' ) ) { return; } 272 297 273 298 $post = get_post( $postID ); … … 276 301 277 302 if ( $post->post_type === FCPFSC_SLUG ) { 278 $fields = [ 'post-types', 'post-archives', 'de register-style-names', 'rest-css' ];303 $fields = [ 'post-types', 'post-archives', 'development-mode', 'deregister-style-names', 'deregister-script-names', 'rest-css', 'rest-css-defer' ]; 279 304 } else { 280 305 $fields = [ 'id', 'id-exclude' ]; … … 351 376 $field = ( strpos( $field, FCPFSC_PREF ) === 0 ) ? substr( $field, strlen( FCPFSC_PREF ) ) : $field; 352 377 378 $onoff = function($value) { 379 return $value[0] === 'on' ? ['on'] : []; 380 }; 381 353 382 switch( $field ) { 354 case ( 'post-types' ):383 case ( 'post-types' ): 355 384 return array_intersect( $value, array_keys( get_all_post_types()['public'] ) ); 356 385 break; 357 case ( 'post-archives' ):386 case ( 'post-archives' ): 358 387 return array_intersect( $value, array_keys( get_all_post_types()['archive'] ) ); 359 388 break; 360 case( 'deregister-style-names' ): 389 case ( 'development-mode' ): 390 return $onoff( $value ); 391 break; 392 case ( 'deregister-style-names' ): 361 393 return $value; // ++preg_replace not letters ,space-_, lowercase?, 362 394 break; 363 case( 'rest-css' ): 395 case ( 'deregister-script-names' ): 396 return $value; // ++preg_replace not letters ,space-_, lowercase?, 397 break; 398 case ( 'rest-css' ): 364 399 365 400 list( $errors, $filtered ) = sanitize_css( wp_unslash( $value ) ); //++ move it all to a separate filter / actions, organize better with errors? … … 375 410 return $value; 376 411 break; 377 case( 'id' ): 412 case ( 'rest-css-defer' ): 413 return $onoff( $value ); 414 break; 415 case ( 'id' ): 378 416 if ( !is_numeric( $value ) ) { return ''; } // ++to a function 379 417 if ( !( $post = get_post( $value ) ) || $post->post_type !== FCPFSC_SLUG ) { return ''; } 380 418 return $value; 381 419 break; 382 case ( 'id-exclude' ):420 case ( 'id-exclude' ): 383 421 if ( !is_numeric( $value ) ) { return ''; } 384 422 if ( !( $post = get_post( $value ) ) || $post->post_type !== FCPFSC_SLUG ) { return ''; } … … 506 544 global $wpdb; 507 545 508 $metas = $wpdb->get_col( $wpdb->prepare(' 546 // filter by post_status & post_type 547 $filtered_ids = $wpdb->get_col( $wpdb->prepare(' 509 548 510 549 SELECT `ID` … … 514 553 ', array_merge( [ 'publish', FCPFSC_SLUG ], $ids ) ) ); 515 554 516 return $metas; 555 // filter by development mode 556 if ( current_user_can( 'administrator' ) ) { return $filtered_ids; } 557 558 $dev_mode = $wpdb->get_col( $wpdb->remove_placeholder_escape( $wpdb->prepare(' 559 560 SELECT `post_id` 561 FROM `'.$wpdb->postmeta.'` 562 WHERE `meta_key` = %s AND `meta_value` = %s AND `post_id` IN ( '.implode( ',', array_fill( 0, count( $ids ), '%s' ), ).' ) 563 564 ', array_merge( [ FCPFSC_PREF.'development-mode', serialize(['on']) ], $ids ) ) ) ); 565 566 567 return array_values( array_diff( $filtered_ids, $dev_mode ) ); 517 568 } 518 569 … … 521 572 global $wpdb; 522 573 523 $ metas = $wpdb->get_col( $wpdb->remove_placeholder_escape( $wpdb->prepare('574 $ids = $wpdb->get_col( $wpdb->remove_placeholder_escape( $wpdb->prepare(' 524 575 525 576 SELECT `post_id` … … 529 580 ', $key, $wpdb->add_placeholder_escape( '%"'.$type.'"%' ) ) ) ); 530 581 531 return $metas; 582 return $ids; 583 } 584 585 function get_to_defer( $ids ) { 586 587 global $wpdb; 588 589 $defer_ids = $wpdb->get_col( $wpdb->remove_placeholder_escape( $wpdb->prepare(' 590 591 SELECT `post_id` 592 FROM `'.$wpdb->postmeta.'` 593 WHERE `meta_key` = %s AND `meta_value` = %s AND `post_id` IN ( '.implode( ',', array_fill( 0, count( $ids ), '%s' ), ).' ) 594 595 ', array_merge( [ FCPFSC_PREF.'rest-css-defer', serialize(['on']) ], $ids ) ) ) ); 596 597 598 return $defer_ids; 532 599 } 533 600 … … 549 616 } 550 617 551 function get_ css_to_deregister( $ids ) {618 function get_to_deregister( $ids ) { 552 619 553 620 if ( empty( $ids ) ) { return []; } … … 555 622 global $wpdb; 556 623 557 $metas = $wpdb->get_col( $wpdb->remove_placeholder_escape( $wpdb->prepare(' 558 559 SELECT `meta_value` 624 $wpdb->query( $wpdb->remove_placeholder_escape( $wpdb->prepare(' 625 626 SELECT 627 IF ( STRCMP( `meta_key`, %s ) = 0, `meta_value`, "" ) AS "styles", 628 IF ( STRCMP( `meta_key`, %s ) = 0, `meta_value`, "" ) AS "scripts" 560 629 FROM `'.$wpdb->postmeta.'` 561 WHERE `meta_key` = %s AND `post_id` IN ( '.implode( ',', array_fill( 0, count( $ids ), '%s' ), ).' ) 562 563 ', array_merge( [ FCPFSC_PREF.'deregister-style-names' ], $ids ) ) ) ); 564 565 $metas = array_values( array_unique( array_filter( array_map( 'trim', explode( ',', implode( ', ', $metas ) ) ) ) ) ); 566 567 return $metas; 630 WHERE ( `meta_key` = %s OR `meta_key` = %s ) AND `post_id` IN ( '.implode( ',', array_fill( 0, count( $ids ), '%s' ), ).' ) 631 632 ', array_merge( 633 [ FCPFSC_PREF.'deregister-style-names', FCPFSC_PREF.'deregister-script-names', FCPFSC_PREF.'deregister-style-names', FCPFSC_PREF.'deregister-script-names' ], 634 $ids 635 ) ) ) ); 636 637 $clear = function($a) { return array_values( array_unique( array_filter( array_map( 'trim', explode( ',', implode( ', ', $a ) ) ) ) ) ); }; 638 639 $styles = $clear( $wpdb->get_col( null, 0 ) ); 640 $scripts = $clear( $wpdb->get_col( null, 1 ) ); 641 642 return [ $styles, $scripts ]; 568 643 } 569 644 … … 630 705 631 706 ?> 632 <p>You can apply this styling to a separate post. Every public post type now has a special select box in the right sidebar to pick from the list of the first-screen-css posts, like this one.</p> 633 <p>CSS will be minified before printing.</p> 707 <p>You can apply this styling to a separate post. Every public post type has a special select box in the right sidebar to pick this or any other first-screen-css.</p> 634 708 <p>You can grab the first screen css of a page with the script: <a href="https://github.com/VVolkov833/first-screen-css-grabber" target="_blank" rel="noopener">github.com/VVolkov833/first-screen-css-grabber</a></p> 635 709 <?php 636 710 711 checkboxes( (object) [ 712 'name' => 'development-mode', 713 'options' => ['on' => 'Development mode (apply only if the post is visited as the admin)'], 714 'value' => get_post_meta( $post->ID, FCPFSC_PREF.'development-mode' )[0], 715 ]); 716 637 717 wp_nonce_field( FCPFSC_PREF.'nounce-action', FCPFSC_PREF.'nounce-name' ); 638 718 } … … 641 721 global $post; 642 722 643 ?><p><strong>List the names of stylesto deregister, separate by comma</strong></p><?php723 ?><p><strong>List the names of STYLES to deregister, separate by comma</strong></p><?php 644 724 645 725 input( (object) [ … … 647 727 'placeholder' => 'my-theme-style, some-plugin-style', 648 728 'value' => get_post_meta( $post->ID, FCPFSC_PREF.'deregister-style-names' )[0], 729 ]); 730 731 ?><p><strong>List the names of SCRIPTS to deregister, separate by comma</strong></p><?php 732 733 input( (object) [ 734 'name' => 'deregister-script-names', 735 'placeholder' => 'my-theme-script, some-plugin-script', 736 'value' => get_post_meta( $post->ID, FCPFSC_PREF.'deregister-script-names' )[0], 649 737 ]); 650 738 … … 664 752 ]); 665 753 754 checkboxes( (object) [ 755 'name' => 'rest-css-defer', 756 'options' => ['on' => 'Defer the not-first-screen CSS (avoid render-blicking)'], 757 'value' => get_post_meta( $post->ID, FCPFSC_PREF.'rest-css-defer' )[0], 758 ]); 666 759 } 667 760 … … 709 802 // svn upload 710 803 804 // ++limit meta boxes to admins too!!! 711 805 // ++add formatting button like https://codemirror.net/2/demo/formatting.html 806 // ++add the bigger height button and save it 712 807 // ++switch selects to checkboxes or multiples 713 808 // ++maybe limit the id-exclude to the fitting post types … … 715 810 // ++get the list of css to unload with jQuery.html() && regexp, or ?query in url to print loaded scripts 716 811 // ++!!??add small textarea to every public post along with css like for a unique background-image in hero 717 // ++limit meta boxes to admins too!!! 718 // ++load in footer - the last argument in enqueue script 812 // ++list of styles to defer like with deregister -
fcp-first-screen-css/trunk/readme.txt
r2831794 r2847523 1 1 === FCP First Screen CSS === 2 2 Contributors: Firmcatalyst 3 Tags: inline, css, firstscreen, style, web vitals, cls, fcp 3 Tags: inline, css, firstscreen, style, web vitals, cls, fcp, defer, dequeue, deregister 4 4 Requires at least: 5.8 5 5 Tested up to: 6.1 6 6 Requires PHP: 7.4 7 Stable tag: 1. 27 Stable tag: 1.3 8 8 Author: Firmcatalyst, Vadim Volkov 9 9 Author URI: https://firmcatalyst.com … … 15 15 == Description == 16 16 17 Insert inline CSS to the head of the website, so the first screen renders with no jumps, which might improve the CLS and FCP web vitals. Or for any other reason.17 Insert the inline CSS to the head of the website, disable existing styles and scripts, defer loading of not-first-screen style, apply to a single post or bulk. 18 18 19 19 = Features = 20 20 21 21 * Apply to any single post / page / custom post-type 22 * Apply to all posts of a particular p ublic post-type23 * Apply to the blog or any archive page of a post-type with archive support22 * Apply to all posts of a particular post-type 23 * Apply to the blog or the archive page of a post-type 24 24 * It minifies the css before printing 25 * Deregister enqueued styles by name 26 * Apply non-first-screen css separately 25 * Deregister enqueued styles and scripts by name 26 * Apply not-first-screen CSS separately 27 * Defer the not-first-screen CSS loading 27 28 28 29 = Usage = 29 30 30 31 * Install and activate the plugin 31 * Go to "FirstScreen CSS" menu item in the left sidebar of your wp-admin32 * Go to the "First Screen CSS" menu item in the left sidebar of your wp-admin 32 33 * Add New, insert your CSS 33 * To apply the CSS, pick where to at the bottom of the page 34 * To apply to a single post / page - go to that post editor, and select your created FirstScreen CSS in the box in the right sidebar 34 * Pick where to apply and other options 35 35 36 36 == Installation == … … 49 49 == Upgrade Notice == 50 50 51 = 1.3 = 52 53 * Added the development mode, visible only to admins 54 * Added the option to deregister existing styles and scripts 55 * Added the option to defer the not-first-screen CSS loading 56 51 57 = 1.2 = 52 58
Note: See TracChangeset
for help on using the changeset viewer.