-
Notifications
You must be signed in to change notification settings - Fork 0
/
jdm-native-lazy-loading.php
160 lines (129 loc) · 4.65 KB
/
jdm-native-lazy-loading.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
* Plugin Name: JDM Native Lazy Loading
* Plugin URI: https://github.com/jdmdigital/JDM-Native-Lazy-Loading
* Description: This plugin adds the <code>loading="lazy"</code> attribute to IMG tags within your content to support native image lazy loading, coming in Chrome 75.
* Version: 1.2
* Author: JDM Digital
* Author URI: https://jdmdigital.co
*/
// If this file is called directly, abandon ship.
if ( ! defined( 'WPINC' ) ) {
die;
}
define( 'NATIVELAZYLOADING_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
define( 'NATIVELAZYLOADING_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'NATIVELAZYLOADING_DEBUG', false );
/* == Build Settings Page and Declare Options ==
* @since v1.1
*/
require_once(NATIVELAZYLOADING_PLUGIN_PATH . 'settings.php');
// Add Settings Link to Plugins >> Installed Plugins
add_filter('plugin_action_links_'.plugin_basename(__FILE__), 'jdm_add_plugin_settings_link');
function jdm_add_plugin_settings_link( $links ) {
$links[] = '<a href="' .admin_url( 'options-general.php?page=jdm-native-lazy-loading' ) .'">' . __('Settings') . '</a>';
return $links;
}
/*
* Get the current time and set it as an option when the plugin is activated.
* We'll use this later to display a rating request after a week.
* @since 1.2
*/
function jdm_set_activation_date() {
$now = strtotime( "now" );
add_option( 'jdmnll_activation_date', $now );
}
register_activation_hook( __FILE__, 'jdm_set_activation_date' );
/*
* Check date on activation and add to admin notice if it was over 8 days ago.
* @since v1.2
*/
function jdm_check_activation_date() {
$nobug = get_option( 'jdmnll_no_bug');
if(!$nobug && !defined('DISABLE_NAG_NOTICES')){
$install_date = get_option( 'jdmnll_activation_date' );
$past_date = strtotime( '-8 days' );
if ( $past_date >= $install_date ) {
add_action( 'admin_notices', 'jdmnll_review_notice' );
}
}
}
add_action( 'admin_init', 'jdm_check_activation_date' );
// Add Review Notice (dismissable)
// @since v1.2
function jdmnll_review_notice() {
$reviewurl = 'https://wordpress.org/plugins/native-image-lazy-loading/#reviews';
$dismissforeverurl = '?jdmnllnobug=1';
$notice = '
<div class="notice notice-info">
<p>If our little plugin is working great for you, do us a big favor and <a href="'.$reviewurl.'" target="_blank" rel="noopener nofollow" style="font-weight:600">rate us on WordPress.org</a> or don\'t. Your choice. <a href="'.$dismissforeverurl.'">Dismiss Forever</a></p>
</div>';
if( is_admin() && get_option( 'jdmnll_no_bug') && !defined('DISABLE_NAG_NOTICES') ){
echo $notice;
}
}
/**
* Set the plugin to no longer bug users.
* @since v1.2
*/
function jdmnll_set_no_bug() {
$nobug = "";
if ( isset( $_GET['jdmnllnobug'] ) ) {
$nobug = esc_attr( $_GET['jdmnllnobug'] );
}
if ( 1 == $nobug ) {
add_option( 'jdmnll_no_bug', TRUE );
}
}
add_action( 'admin_init', 'jdmnll_set_no_bug', 5 );
/*
* == Pretty Much the Whole Plugin Here ==
*/
// Same (mostly) signature as str_replace(), but only replaces the FIRST match
// @since v1.1
if(!function_exists('str_replace_first')) {
function str_replace_first($search, $replace, $subject) {
$search = '/'.preg_quote($search, '/').'/';
return preg_replace($search, $replace, $subject, 1);
}
}
if(!function_exists('jdm_native_lazy_loading')) {
add_filter('the_content','jdm_native_lazy_loading');
function jdm_native_lazy_loading($content) {
$default_value = 'lazy';
$firstImg = esc_attr(get_option('jdmnll_1stimg', $default_value));
$nthimg = esc_attr(get_option('jdmnll_nthimg', $default_value));
if($firstImg == $nthimg) {
// Replace all matches with the loading setting
$content = str_replace('<img','<img loading="'.$nthimg.'"', $content);
} else {
// Replace all matches with the nth image loading setting
$content = str_replace('<img','<img loading="'.$nthimg.'"', $content);
// Then, Replace just the FIRST image match with the firstImg setting
$content = str_replace_first('<img','<img loading="'.$firstImg.'"', $content);
}
return $content;
}
}
function jdmnll_debug() {
if(defined('NATIVELAZYLOADING_DEBUG') && NATIVELAZYLOADING_DEBUG) {
if(get_option( 'jdmnll_no_bug')){
$nobuggy = 'TRUE';
} else {
$nobuggy = 'FALSE';
}
$default_value = 'lazy';
$firstImg = esc_attr(get_option('jdmnll_1stimg', $default_value));
$nthimg = esc_attr(get_option('jdmnll_nthimg', $default_value));
$html = '
<pre>
Activation Date: '.get_option( 'jdmnll_activation_date' ).'<br/>
Past Date: '.strtotime( '-8 days' ).'<br/>
Never Bug: '.$nobuggy.'<br/>
First Img: '.$firstImg.'<br/>
Nth Img: '.$nthimg.'<br/>
</pre>
';
echo $html;
}
}