Plugin Directory

Changeset 3481598


Ignore:
Timestamp:
03/13/2026 12:37:50 AM (3 weeks ago)
Author:
dibwp
Message:

Version 1.0.3: Enqueue external scripts via wp_enqueue_script for better compatibility with caching and optimization plugins

Location:
dropinblog/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • dropinblog/trunk/classes/api.php

    r3426487 r3481598  
    5555    public function get_rendered_head(){
    5656        if( $this->is_api_error ){
    57             return '';   
     57            return '';
    5858        }
    5959        if( !$this->response_body->success ){
     
    6161        }
    6262        return $this->response_body->data->head_html;
     63    }
     64
     65    public function get_head_data() {
     66        if ( $this->is_api_error ) {
     67            return null;
     68        }
     69        if ( ! $this->response_body->success ) {
     70            return null;
     71        }
     72        return $this->response_body->data->head_data;
    6373    }
    6474
  • dropinblog/trunk/classes/rendering.php

    r3481565 r3481598  
    1111    var $current_url_parts;
    1212    var $is_full_type = false;
     13    private array $async_handles = [];
    1314
    1415    function __construct()
     
    6263            add_action('wp', [$this, 'cleanup']);
    6364            add_filter('wp_head', [$this, 'render_head'], 1000);
     65            add_action('wp_enqueue_scripts', [$this, 'enqueue_scripts']);
     66            add_filter('script_loader_tag', [$this, 'add_async_attribute'], 10, 2);
    6467            add_filter('the_content', [$this, 'render_content']);
    6568            add_filter('template_redirect', [$this, 'render_redirect']);
     
    9497        $html = (string) $this->api_content->get_rendered_head();
    9598
     99        // Strip external script tags (they're enqueued via wp_enqueue_script now).
     100        // Keep inline scripts (JSON-LD schema, etc.) in the output.
     101        $html = preg_replace( '#<script\b[^>]*\bsrc\b[^>]*>.*?</script>#is', '', $html );
     102
    96103        $allowed['title'] = [];
    97         $allowed['link'] = ['rel' => true, 'href' => true, 'type' => true];
    98         $allowed['meta'] = ['charset' => true, 'name' => true, 'content' => true, 'property' => true];
    99         $allowed['script'] = ['type' => true, 'async' => true, 'src' => true, 'data-jetpack-boost' => true, 'defer' => true];
     104        $allowed['link']  = ['rel' => true, 'href' => true, 'type' => true];
     105        $allowed['meta']  = ['charset' => true, 'name' => true, 'content' => true, 'property' => true];
     106        $allowed['script'] = ['type' => true];
    100107        $allowed['style'] = ['id' => true];
    101108        $allowed_protocols = wp_allowed_protocols();
     
    255262        switch ($path) {
    256263            case "main":
    257                 $this->api_content = new DropInBlog_Api("/blog/{$this->blog_id}/rendered/list");
     264                $this->api_content = new DropInBlog_Api($this->api_endpoint("/blog/{$this->blog_id}/rendered/list"));
    258265                break;
    259266            case "main_pagination":
    260                 $this->api_content = new DropInBlog_Api("/blog/{$this->blog_id}/rendered/list?page={$this->current_url_parts[1]}");
     267                $this->api_content = new DropInBlog_Api($this->api_endpoint("/blog/{$this->blog_id}/rendered/list?page={$this->current_url_parts[1]}"));
    261268                break;
    262269            case "single_post":
    263                 $this->api_content = new DropInBlog_Api("/blog/{$this->blog_id}/rendered/post/{$this->current_url_parts[0]}");
     270                $this->api_content = new DropInBlog_Api($this->api_endpoint("/blog/{$this->blog_id}/rendered/post/{$this->current_url_parts[0]}"));
    264271                break;
    265272            case "category":
    266                 $this->api_content = new DropInBlog_Api("/blog/{$this->blog_id}/rendered/list/category/{$this->current_url_parts[1]}");
     273                $this->api_content = new DropInBlog_Api($this->api_endpoint("/blog/{$this->blog_id}/rendered/list/category/{$this->current_url_parts[1]}"));
    267274                break;
    268275            case "category_pagination":
    269                 $this->api_content = new DropInBlog_Api("/blog/{$this->blog_id}/rendered/list/category/{$this->current_url_parts[1]}?page={$this->current_url_parts[3]}");
     276                $this->api_content = new DropInBlog_Api($this->api_endpoint("/blog/{$this->blog_id}/rendered/list/category/{$this->current_url_parts[1]}?page={$this->current_url_parts[3]}"));
    270277                break;
    271278            case "author":
    272                 $this->api_content = new DropInBlog_Api("/blog/{$this->blog_id}/rendered/list/author/{$this->current_url_parts[1]}");
     279                $this->api_content = new DropInBlog_Api($this->api_endpoint("/blog/{$this->blog_id}/rendered/list/author/{$this->current_url_parts[1]}"));
    273280                break;
    274281            case "author_pagination":
    275                 $this->api_content = new DropInBlog_Api("/blog/{$this->blog_id}/rendered/list/author/{$this->current_url_parts[1]}?page={$this->current_url_parts[3]}");
     282                $this->api_content = new DropInBlog_Api($this->api_endpoint("/blog/{$this->blog_id}/rendered/list/author/{$this->current_url_parts[1]}?page={$this->current_url_parts[3]}"));
    276283                break;
    277284            case "feed":
     
    299306    }
    300307
     308    private function api_endpoint( string $path ): string {
     309        $fields = 'fields=head_html,body_html,head_data';
     310        $separator = str_contains( $path, '?' ) ? '&' : '?';
     311        return $path . $separator . $fields;
     312    }
     313
     314    public function enqueue_scripts() {
     315        if ( ! $this->api_content ) {
     316            return;
     317        }
     318
     319        $head_data = $this->api_content->get_head_data();
     320
     321        $index = 0;
     322
     323        if ( ! empty( $head_data->js ) ) {
     324            $this->enqueue_head_item_scripts( $head_data->js, 'dib-js', $index );
     325        }
     326
     327        if ( ! empty( $head_data->js_search ) ) {
     328            $this->enqueue_head_item_scripts( $head_data->js_search, 'dib-js-search', $index );
     329        }
     330    }
     331
     332    private function enqueue_head_item_scripts( $items, string $prefix, int &$index ): void {
     333        if ( ! is_array( $items ) ) {
     334            $items = [ $items ];
     335        }
     336
     337        foreach ( $items as $item ) {
     338            $handle = $prefix . '-' . $index;
     339            $index++;
     340
     341            $src   = is_string( $item ) ? $item : ( $item->src ?? '' );
     342            $async = is_object( $item ) && ! empty( $item->async );
     343
     344            if ( empty( $src ) ) {
     345                continue;
     346            }
     347
     348            wp_enqueue_script( $handle, $src, [], null, false );
     349
     350            if ( $async ) {
     351                $this->async_handles[] = $handle;
     352            }
     353        }
     354    }
     355
     356    public function add_async_attribute( string $tag, string $handle ): string {
     357        if ( in_array( $handle, $this->async_handles, true ) ) {
     358            return str_replace( ' src=', ' async src=', $tag );
     359        }
     360        return $tag;
     361    }
     362
    301363    public function get_url_subsegments($base_slug = '')
    302364    {
  • dropinblog/trunk/dropinblog.php

    r3481565 r3481598  
    44Plugin URI: https://dropinblog.com
    55Description: Embed and display your DropInBlog seamlessly within WordPress with full SEO support.
    6 Version: 1.0.2
     6Version: 1.0.3
    77License: GPL v2 or later
    88License URI: https://www.gnu.org/licenses/gpl-2.0.html
  • dropinblog/trunk/readme.txt

    r3481565 r3481598  
    44Tested up to: 6.9
    55Requires at least PHP: 8.0
    6 Stable tag: 1.0.2
     6Stable tag: 1.0.3
    77License: GPLv2 or later
    88License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    4343== Changelog ==
    4444
     45= 1.0.3 =
     46* Enqueue external scripts via wp_enqueue_script for better compatibility with caching and optimization plugins
     47
    4548= 1.0.2 =
    4649* Fixed conflict with Jetpack
     
    5356
    5457== Upgrade Notice ==
     58
     59= 1.0.3 =
     60Improved compatibility with caching and optimization plugins.
    5561
    5662= 1.0.2 =
Note: See TracChangeset for help on using the changeset viewer.