PluginProbe
Plus WebP or AVIF / 1.02
Plus WebP or AVIF v1.02
5.21 5.20 5.12 5.11 trunk 1.00 1.01 1.02 1.03 1.04 1.05 1.06 1.07 1.08 1.09 1.10 1.11 1.12 1.13 2.00 2.01 2.02 2.03 2.04 2.05 All 47 releases
plus-webp / lib / PlusWebpAdmin.php

PlusWebpAdmin.php in Plus WebP or AVIF 1.02, at lib/PlusWebpAdmin.php

318 lines 11.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plus WebP
4 *
5 * @package Plus WebP
6 * @subpackage PlusWebpAdmin Management screen
7 Copyright (c) 2019- Katsushi Kawamori (email : dodesyoswift312@gmail.com)
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; version 2 of the License.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 $pluswebpadmin = new PlusWebpAdmin();
23
24 class PlusWebpAdmin {
25
26 /* ==================================================
27 * Construct
28 * @since 1.00
29 */
30 public function __construct() {
31
32 add_action( 'admin_menu', array($this, 'plugin_menu'));
33 add_action( 'admin_enqueue_scripts', array($this, 'load_custom_wp_admin_style') );
34 add_filter( 'plugin_action_links', array($this, 'settings_link'), 10, 2 );
35
36 add_action( 'AllImagesRegenerateHook', array($this, 'allimages_regenerate'), 10, 1 );
37
38 }
39
40 /* ==================================================
41 * Add a "Settings" link to the plugins page
42 * @since 1.00
43 */
44 public function settings_link( $links, $file ) {
45 static $this_plugin;
46 if ( empty($this_plugin) ) {
47 $this_plugin = 'plus-webp/pluswebp.php';
48 }
49 if ( $file == $this_plugin ) {
50 $links[] = '<a href="'.admin_url('tools.php?page=pluswebp').'">'.__( 'Bulk Generate', 'plus-webp').'</a>';
51 }
52 return $links;
53 }
54
55 /* ==================================================
56 * Settings page
57 * @since 1.00
58 */
59 public function plugin_menu() {
60 add_management_page( 'Plus WebP', 'Plus WebP', 'manage_options', 'pluswebp', array($this, 'plugin_options') );
61 }
62
63 /* ==================================================
64 * Add Css and Script
65 * @since 1.00
66 */
67 public function load_custom_wp_admin_style() {
68 if ($this->is_my_plugin_screen()) {
69 wp_enqueue_style( 'jquery-responsiveTabs', plugin_dir_url( __DIR__ ).'css/responsive-tabs.css' );
70 wp_enqueue_style( 'jquery-responsiveTabs-style', plugin_dir_url( __DIR__ ).'css/style.css' );
71 wp_enqueue_script( 'jquery' );
72 wp_enqueue_script( 'jquery-responsiveTabs', plugin_dir_url( __DIR__ ).'js/jquery.responsiveTabs.min.js' );
73 wp_enqueue_script( 'pluswebp-admin-js', plugin_dir_url( __DIR__ ).'js/jquery.pluswebp.admin.js', array('jquery') );
74 }
75 }
76
77 /* ==================================================
78 * For only admin style
79 * @since 1.00
80 */
81 private function is_my_plugin_screen() {
82 $screen = get_current_screen();
83 if (is_object($screen) && $screen->id == 'tools_page_pluswebp') {
84 return TRUE;
85 } else {
86 return FALSE;
87 }
88 }
89
90 /* ==================================================
91 * Settings page
92 * @since 1.00
93 */
94 public function plugin_options() {
95
96 if ( !current_user_can( 'manage_options' ) ) {
97 wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
98 }
99
100 if ( isset($_POST['Generatewebp']) && $_POST['Generatewebp'] ) {
101 if ( check_admin_referer('pwp_gnt', 'pluswebp_gnt') ) {
102 $user = wp_get_current_user();
103 $to = $user->user_email;
104 $userid = $user->ID;
105 // 30seconds
106 wp_schedule_single_event( time() + 30, 'AllImagesRegenerateHook', array( $to ) );
107 echo '<div class="notice notice-success is-dismissible"><ul><li>'.__('After 30 seconds background generation starts.', 'plus-webp').'</li></ul></div>';
108 }
109 }
110 if ( isset($_POST['Manageset']) && $_POST['Manageset'] ) {
111 if ( check_admin_referer('pwp_set', 'pluswebp_set') ) {
112 $this->options_updated();
113 }
114 }
115
116 $scriptname = admin_url('tools.php?page=pluswebp');
117 $pluswebp_settings = get_option('pluswebp');
118
119 ?>
120
121 <div class="wrap">
122 <h2>Plus WebP</h2>
123
124 <div id="pluswebp-admin-tabs">
125 <ul>
126 <li><a href="#pluswebp-admin-tabs-1"><?php _e('Bulk Generate', 'plus-webp'); ?></a></li>
127 <li><a href="#pluswebp-admin-tabs-2"><?php _e('Settings'); ?></a></li>
128 <li><a href="#pluswebp-admin-tabs-3"><?php _e('Donate to this plugin &#187;'); ?></a></li>
129 </ul>
130
131 <div id="pluswebp-admin-tabs-1">
132 <div class="wrap">
133 <h2><?php _e('Bulk Generate', 'plus-webp'); ?></h2>
134
135 <div style="margin: 5px; padding: 5px;">
136 <p class="description">
137 <?php _e('Press the following button to start WebP generation after 30 seconds in the background. When finished, you will receive an email.', 'plus-webp'); ?>
138 </p>
139 </div>
140
141 <form method="post" action="<?php echo $scriptname; ?>">
142 <?php wp_nonce_field('pwp_gnt', 'pluswebp_gnt'); ?>
143 <?php submit_button( __('Generate', 'plus-webp'), 'primary', 'Generatewebp', TRUE ); ?>
144 </form>
145
146 </div>
147 </div>
148
149 <div id="pluswebp-admin-tabs-2">
150 <div class="wrap">
151 <h2><?php _e('Settings'); ?></h2>
152
153 <form method="post" action="<?php echo $scriptname.'#pluswebp-admin-tabs-2'; ?>">
154 <?php wp_nonce_field('pwp_set', 'pluswebp_set'); ?>
155
156 <div style="width: 100%; height: 100%; margin: 5px; padding: 5px; border: #CCC 2px solid;">
157 <h3><?php _e('Quality', 'plus-webp'); ?></h3>
158 <div style="margin: 5px; padding: 5px;">
159 <?php _e('low resolution', 'plus-webp'); ?>
160 <input type="range" style="vertical-align:middle;" name="quality" value="<?php echo $pluswebp_settings['quality']; ?>" min="0" max="100" step="1">
161 <?php _e('high resolution', 'plus-webp'); ?>
162 <div style="margin: 5px; padding: 5px;">
163 <p class="description">
164 <?php _e('Specifies the quality of WebP. The higher the resolution, the larger the file size.', 'plus-webp'); ?>
165 </p>
166 </div>
167 </div>
168 </div>
169
170 <div style="width: 100%; height: 100%; margin: 5px; padding: 5px; border: #CCC 2px solid;">
171 <h3><?php _e('WebP replacement of images and contents', 'plus-webp'); ?></h3>
172 <div style="margin: 5px; padding: 5px;">
173 <input type="checkbox" name="replace" value="1" <?php checked('1', $pluswebp_settings['replace']); ?>><?php _e('Apply'); ?>
174 <div style="margin: 5px; padding: 5px;">
175 <p class="description">
176 <?php _e('Checking this setting will replace image files with WebP when adding new media, and delete the original image file. Also, when generating all images, the original image file ID will be overwritten as WebP and the original image file will be deleted. All URLs in the content are also replaced.', 'plus-webp'); ?>
177 </p>
178 </div>
179 </div>
180 </div>
181
182 <?php submit_button( __('Save Changes'), 'large', 'Manageset', FALSE ); ?>
183 </form>
184
185 </div>
186 </div>
187
188 <div id="pluswebp-admin-tabs-3">
189 <div class="wrap">
190 <?php $this->credit(); ?>
191 </div>
192 </div>
193
194 </div>
195
196 </div>
197 <?php
198 }
199
200 /* ==================================================
201 * Credit
202 */
203 private function credit() {
204
205 $plugin_name = NULL;
206 $plugin_ver_num = NULL;
207 $plugin_path = plugin_dir_path( __DIR__ );
208 $plugin_dir = untrailingslashit($plugin_path);
209 $slugs = explode('/', $plugin_dir);
210 $slug = end($slugs);
211 $files = scandir($plugin_dir);
212 foreach ($files as $file) {
213 if($file == '.' || $file == '..' || is_dir($plugin_path.$file)){
214 continue;
215 } else {
216 $exts = explode('.', $file);
217 $ext = strtolower(end($exts));
218 if ( $ext === 'php' ) {
219 $plugin_datas = get_file_data( $plugin_path.$file, array('name'=>'Plugin Name', 'version' => 'Version') );
220 if ( array_key_exists( "name", $plugin_datas ) && !empty($plugin_datas['name']) && array_key_exists( "version", $plugin_datas ) && !empty($plugin_datas['version']) ) {
221 $plugin_name = $plugin_datas['name'];
222 $plugin_ver_num = $plugin_datas['version'];
223 break;
224 }
225 }
226 }
227 }
228 $plugin_version = __('Version:').' '.$plugin_ver_num;
229 $faq = __('https://wordpress.org/plugins/'.$slug.'/faq', $slug);
230 $support = 'https://wordpress.org/support/plugin/'.$slug;
231 $review = 'https://wordpress.org/support/view/plugin-reviews/'.$slug;
232 $translate = 'https://translate.wordpress.org/projects/wp-plugins/'.$slug;
233 $facebook = 'https://www.facebook.com/katsushikawamori/';
234 $twitter = 'https://twitter.com/dodesyo312';
235 $youtube = 'https://www.youtube.com/channel/UC5zTLeyROkvZm86OgNRcb_w';
236 $donate = __('https://shop.riverforest-wp.info/donate/', $slug);
237
238 ?>
239 <span style="font-weight: bold;">
240 <div>
241 <?php echo $plugin_version; ?> |
242 <a style="text-decoration: none;" href="<?php echo $faq; ?>" target="_blank"><?php _e('FAQ'); ?></a> | <a style="text-decoration: none;" href="<?php echo $support; ?>" target="_blank"><?php _e('Support Forums'); ?></a> | <a style="text-decoration: none;" href="<?php echo $review; ?>" target="_blank"><?php _e('Reviews', $slug); ?></a>
243 </div>
244 <div>
245 <a style="text-decoration: none;" href="<?php echo $translate; ?>" target="_blank"><?php echo sprintf(__('Translations for %s'), $plugin_name); ?></a> | <a style="text-decoration: none;" href="<?php echo $facebook; ?>" target="_blank"><span class="dashicons dashicons-facebook"></span></a> | <a style="text-decoration: none;" href="<?php echo $twitter; ?>" target="_blank"><span class="dashicons dashicons-twitter"></span></a> | <a style="text-decoration: none;" href="<?php echo $youtube; ?>" target="_blank"><span class="dashicons dashicons-video-alt3"></span></a>
246 </div>
247 </span>
248
249 <div style="width: 250px; height: 180px; margin: 5px; padding: 5px; border: #CCC 2px solid;">
250 <h3><?php _e('Please make a donation if you like my work or would like to further the development of this plugin.', $slug); ?></h3>
251 <div style="text-align: right; margin: 5px; padding: 5px;"><span style="padding: 3px; color: #ffffff; background-color: #008000">Plugin Author</span> <span style="font-weight: bold;">Katsushi Kawamori</span></div>
252 <button type="button" style="margin: 5px; padding: 5px;" onclick="window.open('<?php echo $donate; ?>')"><?php _e('Donate to this plugin &#187;'); ?></button>
253 </div>
254
255 <?php
256
257 }
258
259 /* ==================================================
260 * Regenerate All Images ('image/jpeg','image/png','image/bmp','image/gif')
261 * @param string $to
262 * @return send mail
263 * @since 1.00
264 */
265 public function allimages_regenerate( $to ) {
266
267 global $wpdb;
268 $post_ids = $wpdb->get_col("
269 SELECT ID
270 FROM $wpdb->posts
271 WHERE post_type = 'attachment'
272 AND post_mime_type IN('image/jpeg','image/png','image/bmp','image/gif')
273 AND post_status = 'inherit'
274 ");
275
276 include_once( ABSPATH . 'wp-admin/includes/image.php' );
277
278 foreach ( $post_ids as $attach_id ) {
279 $postdate = get_the_date('Y-m-d H:i:s', $attach_id);
280 $postdategmt = get_gmt_from_date($postdate);
281 $fullpath_filename = get_attached_file($attach_id);
282 $metadata = wp_get_attachment_metadata( $attach_id );
283 do_action( 'wp_generate_attachment_metadata', $metadata, $attach_id );
284 }
285
286 $subject = sprintf( __('[%s] WebP generate', 'plus-webp'), get_option( 'blogname' ) );
287 $message = __("Generation of WebP is complete.", 'plus-webp')."\r\n\r\n";
288
289 wp_mail( $to, $subject, $message );
290
291 wp_clear_scheduled_hook( 'AllImagesRegenerateHook' );
292
293 }
294
295 /* ==================================================
296 * Update wp_options table.
297 * @param none
298 * @since 1.02
299 */
300 private function options_updated(){
301
302 if ( !empty($_POST['Manageset']) ) {
303 $pluswebp_settings = get_option('pluswebp');
304 if ( !empty($_POST['replace']) ) {
305 $pluswebp_settings['replace'] = TRUE;
306 } else {
307 $pluswebp_settings['replace'] = FALSE;
308 }
309 $pluswebp_settings['quality'] = intval($_POST['quality']);
310 update_option( 'pluswebp', $pluswebp_settings );
311 echo '<div class="notice notice-success is-dismissible"><ul><li>'.__('Settings').' --> '.__('Settings saved.').'</li></ul></div>';
312 }
313
314 }
315
316 }
317
318 ?>