Image optimization & Lazy Load by Optimole - Version 1.0.2

Version Description

  • 2018-09-25
Download this release

Release Info

Developer optimole
Plugin Icon 128x128 Image optimization & Lazy Load by Optimole
Version 1.0.2
Comparing to
See all releases

Code changes from version 1.0.1 to 1.0.2

Files changed (8) hide show
  1. CHANGELOG.md +5 -2
  2. README.md +0 -1
  3. inc/admin.php +18 -4
  4. inc/replacer.php +161 -55
  5. optimole-wp.php +2 -2
  6. readme.md +7 -0
  7. readme.txt +7 -0
  8. themeisle-hash.json +1 -1
CHANGELOG.md CHANGED
@@ -1,7 +1,10 @@
1
 
2
- ### v1.0.1 - 2018-09-23
3
  **Changes:**
4
-
 
 
 
5
  ### v1.0.1 - 2018-09-23
6
  **Changes:**
7
  * Tag first stable version for wordpress.org.
1
 
2
+ ### v1.0.2 - 2018-09-25
3
  **Changes:**
4
+ * Improve compatibility with elementor external css files.
5
+ * Adds generator tag.
6
+ * Improve replacer handler hook register.
7
+
8
  ### v1.0.1 - 2018-09-23
9
  **Changes:**
10
  * Tag first stable version for wordpress.org.
README.md DELETED
@@ -1 +0,0 @@
1
- # optimole-wp
 
inc/admin.php CHANGED
@@ -33,6 +33,7 @@ class Optml_Admin {
33
  add_action( 'admin_bar_menu', array( $this, 'add_traffic_node' ), 9999 );
34
  add_filter( 'wp_resource_hints', array( $this, 'add_dns_prefetch' ), 10, 2 );
35
  add_action( 'optml_daily_sync', array( $this, 'daily_sync' ) );
 
36
  if ( ! is_admin() && $this->settings->is_connected() ) {
37
  if ( ! wp_next_scheduled( 'optml_daily_sync' ) ) {
38
  wp_schedule_event( time() + 10, 'daily', 'optml_daily_sync', array() );
@@ -40,6 +41,19 @@ class Optml_Admin {
40
  }
41
  }
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  /**
44
  * Update daily the quota routine.
45
  */
@@ -63,7 +77,7 @@ class Optml_Admin {
63
  /**
64
  * Adds cdn url for prefetch.
65
  *
66
- * @param array $hints Hints array.
67
  * @param string $relation_type Type of relation.
68
  *
69
  * @return array Altered hints array.
@@ -95,9 +109,9 @@ class Optml_Admin {
95
  */
96
  public function render_dashboard_page() {
97
  ?>
98
- <div id="optimole-app">
99
- <app></app>
100
- </div>
101
  <?php
102
  }
103
 
33
  add_action( 'admin_bar_menu', array( $this, 'add_traffic_node' ), 9999 );
34
  add_filter( 'wp_resource_hints', array( $this, 'add_dns_prefetch' ), 10, 2 );
35
  add_action( 'optml_daily_sync', array( $this, 'daily_sync' ) );
36
+ add_action( 'wp_head', array( $this, 'generator' ) );
37
  if ( ! is_admin() && $this->settings->is_connected() ) {
38
  if ( ! wp_next_scheduled( 'optml_daily_sync' ) ) {
39
  wp_schedule_event( time() + 10, 'daily', 'optml_daily_sync', array() );
41
  }
42
  }
43
 
44
+ /**
45
+ * Output Generator tag.
46
+ */
47
+ public function generator() {
48
+ if ( ! $this->settings->is_connected() ) {
49
+ return;
50
+ }
51
+ if ( ! $this->settings->is_enabled() ) {
52
+ return;
53
+ }
54
+ echo '<meta name="generator" content="Optimole ' . esc_attr( OPTML_VERSION ) . '">';
55
+ }
56
+
57
  /**
58
  * Update daily the quota routine.
59
  */
77
  /**
78
  * Adds cdn url for prefetch.
79
  *
80
+ * @param array $hints Hints array.
81
  * @param string $relation_type Type of relation.
82
  *
83
  * @return array Altered hints array.
109
  */
110
  public function render_dashboard_page() {
111
  ?>
112
+ <div id="optimole-app">
113
+ <app></app>
114
+ </div>
115
  <?php
116
  }
117
 
inc/replacer.php CHANGED
@@ -95,15 +95,10 @@ class Optml_Replacer {
95
  * The initialize method.
96
  */
97
  function init() {
98
- $settings = new Optml_Settings();
99
 
100
- if ( ! $settings->is_connected() ) {
101
- return;
102
- }
103
- if ( ! $settings->is_enabled() ) {
104
  return;
105
  }
106
-
107
  $this->set_properties();
108
 
109
  if ( empty( $this->cdn_url ) ) {
@@ -113,9 +108,35 @@ class Optml_Replacer {
113
  add_filter( 'the_content', array( $this, 'filter_the_content' ), PHP_INT_MAX );
114
  add_filter( 'wp_calculate_image_srcset', array( $this, 'filter_srcset_attr' ), PHP_INT_MAX, 5 );
115
  add_filter( 'init', array( $this, 'filter_options_and_mods' ) );
116
- add_action( 'init', array( $this, 'init_html_replacer' ), PHP_INT_MAX );
 
117
  }
118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
 
120
  /**
121
  * Set the cdn url based on the current connected user.
@@ -151,57 +172,10 @@ class Optml_Replacer {
151
  return;
152
  }
153
  ob_start(
154
- array( &$this, 'filter_raw_content' )
155
  );
156
  }
157
 
158
- /**
159
- * Filter raw content for urls.
160
- *
161
- * @param string $html HTML to filter.
162
- *
163
- * @return mixed Filtered content.
164
- */
165
- public function filter_raw_content( $html ) {
166
- $urls = wp_extract_urls( $html );
167
- $cdn_url = $this->cdn_url;
168
- $site_url = get_site_url();
169
- $urls = array_filter(
170
- $urls, function ( $url ) use ( $cdn_url, $site_url ) {
171
- if ( strpos( $url, $cdn_url ) !== false ) {
172
- return false;
173
- }
174
- if ( strpos( $url, $site_url ) === false ) {
175
- return false;
176
- }
177
-
178
- return $this->check_mimetype( $url );
179
- }
180
- );
181
- $new_urls = array_map( array( $this, 'get_imgcdn_url' ), $urls );
182
-
183
- return str_replace( $urls, $new_urls, $html );
184
- }
185
-
186
- /**
187
- * Check url mimetype.
188
- *
189
- * @param string $url Url to check.
190
- *
191
- * @return bool Is a valid image url or not.
192
- */
193
- private function check_mimetype( $url ) {
194
-
195
- $mimes = self::$extensions;
196
- $type = wp_check_filetype( $url, $mimes );
197
-
198
- if ( ! isset( $type['ext'] ) || empty( $type['ext'] ) ) {
199
- return false;
200
- }
201
-
202
- return true;
203
- }
204
-
205
  /**
206
  * This filter will replace all the images retrieved via "wp_get_image" type of functions.
207
  *
@@ -421,6 +395,25 @@ class Optml_Replacer {
421
  return $new_url;
422
  }
423
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
424
  /**
425
  * Ensures that an url parameter can stand inside an url.
426
  *
@@ -685,6 +678,119 @@ class Optml_Replacer {
685
  return $new_url;
686
  }
687
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
688
  /**
689
  * Throw error on object clone
690
  *
95
  * The initialize method.
96
  */
97
  function init() {
 
98
 
99
+ if ( ! $this->should_replace() ) {
 
 
 
100
  return;
101
  }
 
102
  $this->set_properties();
103
 
104
  if ( empty( $this->cdn_url ) ) {
108
  add_filter( 'the_content', array( $this, 'filter_the_content' ), PHP_INT_MAX );
109
  add_filter( 'wp_calculate_image_srcset', array( $this, 'filter_srcset_attr' ), PHP_INT_MAX, 5 );
110
  add_filter( 'init', array( $this, 'filter_options_and_mods' ) );
111
+ add_action( 'template_redirect', array( $this, 'init_html_replacer' ), PHP_INT_MAX );
112
+ add_action( 'get_post_metadata', array( $this, 'replace_meta' ), PHP_INT_MAX, 4 );
113
  }
114
 
115
+ /**
116
+ * Check if we should rewrite the urls.
117
+ *
118
+ * @return bool If we can replace the image.
119
+ *
120
+ */
121
+ public function should_replace() {
122
+
123
+ if ( is_admin() ) {
124
+ return false;
125
+ }
126
+ $settings = new Optml_Settings();
127
+
128
+ if ( ! $settings->is_connected() ) {
129
+ return false;
130
+ }
131
+ if ( ! $settings->is_enabled() ) {
132
+ false;
133
+ }
134
+ if ( array_key_exists( 'preview', $_GET ) && 'true' == $_GET['preview'] ) {
135
+ return false;
136
+ }
137
+
138
+ return true;
139
+ }
140
 
141
  /**
142
  * Set the cdn url based on the current connected user.
172
  return;
173
  }
174
  ob_start(
175
+ array( &$this, 'replace_urls' )
176
  );
177
  }
178
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
  /**
180
  * This filter will replace all the images retrieved via "wp_get_image" type of functions.
181
  *
395
  return $new_url;
396
  }
397
 
398
+ /**
399
+ * Check url mimetype.
400
+ *
401
+ * @param string $url Url to check.
402
+ *
403
+ * @return bool Is a valid image url or not.
404
+ */
405
+ private function check_mimetype( $url ) {
406
+
407
+ $mimes = self::$extensions;
408
+ $type = wp_check_filetype( $url, $mimes );
409
+
410
+ if ( ! isset( $type['ext'] ) || empty( $type['ext'] ) ) {
411
+ return false;
412
+ }
413
+
414
+ return true;
415
+ }
416
+
417
  /**
418
  * Ensures that an url parameter can stand inside an url.
419
  *
678
  return $new_url;
679
  }
680
 
681
+ /**
682
+ * Replace urls in post meta values.
683
+ *
684
+ * @param mixed $metadata Metadata.
685
+ * @param int $object_id Post id.
686
+ * @param string $meta_key Meta key.
687
+ * @param bool $single Is single?
688
+ *
689
+ * @return mixed Altered meta.
690
+ */
691
+ function replace_meta( $metadata, $object_id, $meta_key, $single ) {
692
+
693
+ $meta_needed = '_elementor_data';
694
+
695
+ if ( isset( $meta_key ) && $meta_needed == $meta_key ) {
696
+ remove_filter( 'get_post_metadata', array( $this, 'replace_meta' ), PHP_INT_MAX );
697
+
698
+ $current_meta = get_post_meta( $object_id, $meta_needed, $single );
699
+ add_filter( 'get_post_metadata', array( $this, 'replace_meta' ), PHP_INT_MAX, 4 );
700
+
701
+ if ( ! is_string( $current_meta ) ) {
702
+ return $current_meta;
703
+ }
704
+
705
+ return $this->replace_urls( $current_meta, 'elementor' );
706
+ }
707
+
708
+ // Return original if the check does not pass
709
+ return $metadata;
710
+ }
711
+
712
+ /**
713
+ * Filter raw content for urls.
714
+ *
715
+ * @param string $html HTML to filter.
716
+ *
717
+ * @return mixed Filtered content.
718
+ */
719
+ public function replace_urls( $html, $context = 'raw' ) {
720
+
721
+ switch ( $context ) {
722
+ case 'elementor':
723
+ $old_urls = $this->extract_slashed_urls( $html );
724
+ $urls = array_map( 'wp_unslash', $old_urls );
725
+ $urls = array_combine( $old_urls, $urls );
726
+ break;
727
+ case 'raw':
728
+ default:
729
+ $urls = wp_extract_urls( $html );
730
+
731
+ $urls = array_combine( $urls, $urls );
732
+ break;
733
+
734
+ }
735
+
736
+ $cdn_url = $this->cdn_url;
737
+ $site_url = get_site_url();
738
+ $urls = array_filter(
739
+ $urls, function ( $url ) use ( $cdn_url, $site_url ) {
740
+ if ( strpos( $url, $cdn_url ) !== false ) {
741
+ return false;
742
+ }
743
+ if ( strpos( $url, $site_url ) === false ) {
744
+ return false;
745
+ }
746
+
747
+ return $this->check_mimetype( $url );
748
+ }
749
+ );
750
+ $urls = array_map( array( $this, 'get_imgcdn_url' ), $urls );
751
+
752
+ return str_replace( array_keys( $urls ), array_values( $urls ), $html );
753
+ }
754
+
755
+ /**
756
+ * Extract slashed urls from content.
757
+ *
758
+ * @param string $content Content to parse.
759
+ *
760
+ * @return array Urls found.
761
+ */
762
+ private function extract_slashed_urls( $content ) {
763
+ /**
764
+ * Regex rule to match slashed urls, i.e -> http:\/\/optimole.com\/wp-content\/uploads\/2018\/09\/picture.jpg
765
+ * Based on the extract_url patter.
766
+ *
767
+ * @var string Regex rule string.
768
+ *
769
+ */
770
+ $regex = "#([\"']?)("
771
+ . "(?:([\w-]+:)?\\\/\\\/?)"
772
+ . "[^\s()<>]+"
773
+ . "[.]"
774
+ . "(?:"
775
+ . "\([\w\d]+\)|"
776
+ . "(?:"
777
+ . "[^`!()\[\]{};:'\".,<>«»“”‘’\s]|"
778
+ . "(?:[:]\d+)?/\\\/?"
779
+ . ")+"
780
+ . ")"
781
+ . ")\\1#";
782
+
783
+ preg_match_all(
784
+ $regex,
785
+ $content,
786
+ $urls
787
+ );
788
+
789
+ $urls = array_unique( array_map( 'html_entity_decode', $urls[2] ) );
790
+
791
+ return array_values( $urls );
792
+ }
793
+
794
  /**
795
  * Throw error on object clone
796
  *
optimole-wp.php CHANGED
@@ -2,7 +2,7 @@
2
  /**
3
  * Plugin Name: Image optimization and CDN by Optimole
4
  * Description: Complete handling of your website images.
5
- * Version: 1.0.1
6
  * Author: Optimole
7
  * Author URI: https://optimole.com
8
  * License: GPL-2.0+
@@ -44,7 +44,7 @@ function optml_autoload( $class ) {
44
  function optml() {
45
  define( 'OPTML_URL', plugin_dir_url( __FILE__ ) );
46
  define( 'OPTML_PATH', plugin_dir_path( __FILE__ ) );
47
- define( 'OPTML_VERSION', '1.0.1' );
48
  define( 'OPTML_NAMESPACE', 'optml' );
49
  if( ! defined( 'OPTML_DEBUG' ) ) {
50
  define( 'OPTML_DEBUG', false );
2
  /**
3
  * Plugin Name: Image optimization and CDN by Optimole
4
  * Description: Complete handling of your website images.
5
+ * Version: 1.0.2
6
  * Author: Optimole
7
  * Author URI: https://optimole.com
8
  * License: GPL-2.0+
44
  function optml() {
45
  define( 'OPTML_URL', plugin_dir_url( __FILE__ ) );
46
  define( 'OPTML_PATH', plugin_dir_path( __FILE__ ) );
47
+ define( 'OPTML_VERSION', '1.0.2' );
48
  define( 'OPTML_NAMESPACE', 'optml' );
49
  if( ! defined( 'OPTML_DEBUG' ) ) {
50
  define( 'OPTML_DEBUG', false );
readme.md CHANGED
@@ -32,6 +32,13 @@ This plugin connects via API to OptiMole [image optimization service](https://op
32
  The plugin will rewrite your image URLs to replace them with OptiMole URLs. Your origin images will be downloaded from your storage, processed by the OptiMole infrastructure and cached in the CDN. NO development needed. Simply set up your account and enjoy faster image loading.
33
 
34
  ## Changelog ##
 
 
 
 
 
 
 
35
  ### 1.0.1 - 2018-09-23 ###
36
 
37
  * Tag first stable version for wordpress.org.
32
  The plugin will rewrite your image URLs to replace them with OptiMole URLs. Your origin images will be downloaded from your storage, processed by the OptiMole infrastructure and cached in the CDN. NO development needed. Simply set up your account and enjoy faster image loading.
33
 
34
  ## Changelog ##
35
+ ### 1.0.2 - 2018-09-25 ###
36
+
37
+ * Improve compatibility with elementor external css files.
38
+ * Adds generator tag.
39
+ * Improve replacer handler hook register.
40
+
41
+
42
  ### 1.0.1 - 2018-09-23 ###
43
 
44
  * Tag first stable version for wordpress.org.
readme.txt CHANGED
@@ -32,6 +32,13 @@ This plugin connects via API to OptiMole [image optimization service](https://op
32
  The plugin will rewrite your image URLs to replace them with OptiMole URLs. Your origin images will be downloaded from your storage, processed by the OptiMole infrastructure and cached in the CDN. NO development needed. Simply set up your account and enjoy faster image loading.
33
 
34
  == Changelog ==
 
 
 
 
 
 
 
35
  = 1.0.1 - 2018-09-23 =
36
 
37
  * Tag first stable version for wordpress.org.
32
  The plugin will rewrite your image URLs to replace them with OptiMole URLs. Your origin images will be downloaded from your storage, processed by the OptiMole infrastructure and cached in the CDN. NO development needed. Simply set up your account and enjoy faster image loading.
33
 
34
  == Changelog ==
35
+ = 1.0.2 - 2018-09-25 =
36
+
37
+ * Improve compatibility with elementor external css files.
38
+ * Adds generator tag.
39
+ * Improve replacer handler hook register.
40
+
41
+
42
  = 1.0.1 - 2018-09-23 =
43
 
44
  * Tag first stable version for wordpress.org.
themeisle-hash.json CHANGED
@@ -1 +1 @@
1
- {"optimole-wp.php":"279efcc7c4f487404f9e3ab32d39789b"}
1
+ {"optimole-wp.php":"53bfb904b384e15db053d5f8804ea798"}