| 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 |
/** ================================================== |
| 25 |
* Management screen |
| 26 |
*/ |
| 27 |
class PlusWebpAdmin { |
| 28 |
|
| 29 |
/** ================================================== |
| 30 |
* Construct |
| 31 |
* |
| 32 |
* @since 1.00 |
| 33 |
*/ |
| 34 |
public function __construct() { |
| 35 |
|
| 36 |
add_action( 'admin_menu', array( $this, 'plugin_menu' ) ); |
| 37 |
add_action( 'admin_enqueue_scripts', array( $this, 'load_custom_wp_admin_style' ) ); |
| 38 |
add_filter( 'plugin_action_links', array( $this, 'settings_link' ), 10, 2 ); |
| 39 |
|
| 40 |
add_action( 'AllImagesRegenerateHook', array( $this, 'allimages_regenerate' ), 10, 1 ); |
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
/** ================================================== |
| 45 |
* Add a "Settings" link to the plugins page |
| 46 |
* |
| 47 |
* @param array $links links array. |
| 48 |
* @param string $file file. |
| 49 |
* @return array $links links array. |
| 50 |
* @since 1.00 |
| 51 |
*/ |
| 52 |
public function settings_link( $links, $file ) { |
| 53 |
static $this_plugin; |
| 54 |
if ( empty( $this_plugin ) ) { |
| 55 |
$this_plugin = 'plus-webp/pluswebp.php'; |
| 56 |
} |
| 57 |
if ( $file === $this_plugin ) { |
| 58 |
$links[] = '<a href="' . admin_url( 'tools.php?page=pluswebp' ) . '">' . esc_html__( 'Bulk Generate', 'plus-webp' ) . '</a>'; |
| 59 |
} |
| 60 |
return $links; |
| 61 |
} |
| 62 |
|
| 63 |
/** ================================================== |
| 64 |
* Settings page |
| 65 |
* |
| 66 |
* @since 1.00 |
| 67 |
*/ |
| 68 |
public function plugin_menu() { |
| 69 |
add_management_page( 'Plus WebP', 'Plus WebP', 'manage_options', 'pluswebp', array( $this, 'plugin_options' ) ); |
| 70 |
} |
| 71 |
|
| 72 |
/** ================================================== |
| 73 |
* Add Css and Script |
| 74 |
* |
| 75 |
* @since 1.00 |
| 76 |
*/ |
| 77 |
public function load_custom_wp_admin_style() { |
| 78 |
if ( $this->is_my_plugin_screen() ) { |
| 79 |
wp_enqueue_style( 'jquery-responsiveTabs', plugin_dir_url( __DIR__ ) . 'css/responsive-tabs.css', array(), '1.4.0' ); |
| 80 |
wp_enqueue_style( 'jquery-responsiveTabs-style', plugin_dir_url( __DIR__ ) . 'css/style.css', array(), '1.4.0' ); |
| 81 |
wp_enqueue_script( 'jquery' ); |
| 82 |
wp_enqueue_script( 'jquery-responsiveTabs', plugin_dir_url( __DIR__ ) . 'js/jquery.responsiveTabs.min.js', array(), '1.4.0', false ); |
| 83 |
wp_enqueue_script( 'pluswebp-admin-js', plugin_dir_url( __DIR__ ) . 'js/jquery.pluswebp.admin.js', array( 'jquery' ), array(), '1.00', false ); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/** ================================================== |
| 88 |
* For only admin style |
| 89 |
* |
| 90 |
* @since 1.00 |
| 91 |
*/ |
| 92 |
private function is_my_plugin_screen() { |
| 93 |
$screen = get_current_screen(); |
| 94 |
if ( is_object( $screen ) && 'tools_page_pluswebp' === $screen->id ) { |
| 95 |
return true; |
| 96 |
} else { |
| 97 |
return false; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/** ================================================== |
| 102 |
* Settings page |
| 103 |
* |
| 104 |
* @since 1.00 |
| 105 |
*/ |
| 106 |
public function plugin_options() { |
| 107 |
|
| 108 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 109 |
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.' ) ); |
| 110 |
} |
| 111 |
|
| 112 |
if ( isset( $_POST['Generatewebp'] ) && ! empty( $_POST['Generatewebp'] ) ) { |
| 113 |
if ( check_admin_referer( 'pwp_gnt', 'pluswebp_gnt' ) ) { |
| 114 |
$user = wp_get_current_user(); |
| 115 |
$to = $user->user_email; |
| 116 |
$userid = $user->ID; |
| 117 |
// 30seconds |
| 118 |
wp_schedule_single_event( time() + 30, 'AllImagesRegenerateHook', array( $to ) ); |
| 119 |
echo '<div class="notice notice-success is-dismissible"><ul><li>' . esc_html__( 'After 30 seconds background generation starts.', 'plus-webp' ) . '</li></ul></div>'; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
$this->options_updated(); |
| 124 |
|
| 125 |
$scriptname = admin_url( 'tools.php?page=pluswebp' ); |
| 126 |
$pluswebp_settings = get_option( 'pluswebp' ); |
| 127 |
|
| 128 |
?> |
| 129 |
|
| 130 |
<div class="wrap"> |
| 131 |
<h2>Plus WebP</h2> |
| 132 |
|
| 133 |
<div id="pluswebp-admin-tabs"> |
| 134 |
<ul> |
| 135 |
<li><a href="#pluswebp-admin-tabs-1"><?php esc_html_e( 'Bulk Generate', 'plus-webp' ); ?></a></li> |
| 136 |
<li><a href="#pluswebp-admin-tabs-2"><?php esc_html_e( 'Settings' ); ?></a></li> |
| 137 |
<li><a href="#pluswebp-admin-tabs-3"><?php esc_html_e( 'Donate to this plugin »' ); ?></a></li> |
| 138 |
</ul> |
| 139 |
<div id="pluswebp-admin-tabs-1"> |
| 140 |
<div class="wrap"> |
| 141 |
<h2><?php esc_html_e( 'Bulk Generate', 'plus-webp' ); ?></h2> |
| 142 |
|
| 143 |
<div style="margin: 5px; padding: 5px;"> |
| 144 |
<p class="description"> |
| 145 |
<?php esc_html_e( 'Press the following button to start WebP generation after 30 seconds in the background. When finished, you will receive an email.', 'plus-webp' ); ?> |
| 146 |
</p> |
| 147 |
</div> |
| 148 |
|
| 149 |
<form method="post" action="<?php echo esc_url( $scriptname ); ?>"> |
| 150 |
<?php wp_nonce_field( 'pwp_gnt', 'pluswebp_gnt' ); ?> |
| 151 |
<?php submit_button( __( 'Generate', 'plus-webp' ), 'primary', 'Generatewebp', true ); ?> |
| 152 |
</form> |
| 153 |
</div> |
| 154 |
</div> |
| 155 |
|
| 156 |
<div id="pluswebp-admin-tabs-2"> |
| 157 |
<div class="wrap"> |
| 158 |
<h2><?php esc_html_e( 'Settings' ); ?></h2> |
| 159 |
|
| 160 |
<form method="post" action="<?php echo esc_url( $scriptname ) . '#pluswebp-admin-tabs-2'; ?>"> |
| 161 |
<?php wp_nonce_field( 'pwp_set', 'pluswebp_set' ); ?> |
| 162 |
|
| 163 |
<div style="width: 100%; height: 100%; margin: 5px; padding: 5px; border: #CCC 2px solid;"> |
| 164 |
<h3><?php esc_html_e( 'Quality', 'plus-webp' ); ?></h3> |
| 165 |
<div style="margin: 5px; padding: 5px;"> |
| 166 |
<?php esc_html_e( 'low resolution', 'plus-webp' ); ?> |
| 167 |
<input type="range" style="vertical-align:middle;" name="quality" value="<?php echo esc_attr( $pluswebp_settings['quality'] ); ?>" min="0" max="100" step="1"> |
| 168 |
<?php esc_html_e( 'high resolution', 'plus-webp' ); ?> |
| 169 |
<div style="margin: 5px; padding: 5px;"> |
| 170 |
<p class="description"> |
| 171 |
<?php esc_html_e( 'Specifies the quality of WebP. The higher the resolution, the larger the file size.', 'plus-webp' ); ?> |
| 172 |
</p> |
| 173 |
</div> |
| 174 |
</div> |
| 175 |
</div> |
| 176 |
|
| 177 |
<div style="width: 100%; height: 100%; margin: 5px; padding: 5px; border: #CCC 2px solid;"> |
| 178 |
<h3><?php esc_html_e( 'WebP replacement of images and contents', 'plus-webp' ); ?></h3> |
| 179 |
<div style="margin: 5px; padding: 5px;"> |
| 180 |
<input type="checkbox" name="replace" value="1" <?php checked( '1', $pluswebp_settings['replace'] ); ?>><?php esc_html_e( 'Apply' ); ?> |
| 181 |
<div style="margin: 5px; padding: 5px;"> |
| 182 |
<p class="description"> |
| 183 |
<?php esc_html_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' ); ?> |
| 184 |
</p> |
| 185 |
</div> |
| 186 |
</div> |
| 187 |
</div> |
| 188 |
|
| 189 |
<?php submit_button( __( 'Save Changes' ), 'large', 'Manageset', false ); ?> |
| 190 |
</form> |
| 191 |
</div> |
| 192 |
</div> |
| 193 |
|
| 194 |
<div id="pluswebp-admin-tabs-3"> |
| 195 |
<div class="wrap"> |
| 196 |
<?php $this->credit(); ?> |
| 197 |
</div> |
| 198 |
</div> |
| 199 |
|
| 200 |
</div> |
| 201 |
|
| 202 |
</div> |
| 203 |
<?php |
| 204 |
} |
| 205 |
|
| 206 |
/** ================================================== |
| 207 |
* Credit |
| 208 |
* |
| 209 |
* @since 1.00 |
| 210 |
*/ |
| 211 |
private function credit() { |
| 212 |
|
| 213 |
$plugin_name = null; |
| 214 |
$plugin_ver_num = null; |
| 215 |
$plugin_path = plugin_dir_path( __DIR__ ); |
| 216 |
$plugin_dir = untrailingslashit( $plugin_path ); |
| 217 |
$slugs = explode( '/', $plugin_dir ); |
| 218 |
$slug = end( $slugs ); |
| 219 |
$files = scandir( $plugin_dir ); |
| 220 |
foreach ( $files as $file ) { |
| 221 |
if ( '.' === $file || '..' === $file || is_dir( $plugin_path . $file ) ) { |
| 222 |
continue; |
| 223 |
} else { |
| 224 |
$exts = explode( '.', $file ); |
| 225 |
$ext = strtolower( end( $exts ) ); |
| 226 |
if ( 'php' === $ext ) { |
| 227 |
$plugin_datas = get_file_data( |
| 228 |
$plugin_path . $file, |
| 229 |
array( |
| 230 |
'name' => 'Plugin Name', |
| 231 |
'version' => 'Version', |
| 232 |
) |
| 233 |
); |
| 234 |
if ( array_key_exists( 'name', $plugin_datas ) && ! empty( $plugin_datas['name'] ) && array_key_exists( 'version', $plugin_datas ) && ! empty( $plugin_datas['version'] ) ) { |
| 235 |
$plugin_name = $plugin_datas['name']; |
| 236 |
$plugin_ver_num = $plugin_datas['version']; |
| 237 |
break; |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
$plugin_version = __( 'Version:' ) . ' ' . $plugin_ver_num; |
| 243 |
/* translators: FAQ Link & Slug */ |
| 244 |
$faq = sprintf( esc_html__( 'https://wordpress.org/plugins/%s/faq', '%s' ), $slug ); |
| 245 |
$support = 'https://wordpress.org/support/plugin/' . $slug; |
| 246 |
$review = 'https://wordpress.org/support/view/plugin-reviews/' . $slug; |
| 247 |
$translate = 'https://translate.wordpress.org/projects/wp-plugins/' . $slug; |
| 248 |
$facebook = 'https://www.facebook.com/katsushikawamori/'; |
| 249 |
$twitter = 'https://twitter.com/dodesyo312'; |
| 250 |
$youtube = 'https://www.youtube.com/channel/UC5zTLeyROkvZm86OgNRcb_w'; |
| 251 |
$donate = sprintf( esc_html__( 'https://shop.riverforest-wp.info/donate/', '%s' ), $slug ); |
| 252 |
|
| 253 |
?> |
| 254 |
<span style="font-weight: bold;"> |
| 255 |
<div> |
| 256 |
<?php echo esc_html( $plugin_version ); ?> | |
| 257 |
<a style="text-decoration: none;" href="<?php echo esc_url( $faq ); ?>" target="_blank"><?php esc_html_e( 'FAQ' ); ?></a> | <a style="text-decoration: none;" href="<?php echo esc_url( $support ); ?>" target="_blank"><?php esc_html_e( 'Support Forums' ); ?></a> | <a style="text-decoration: none;" href="<?php echo esc_url( $review ); ?>" target="_blank"><?php sprintf( esc_html_e( 'Reviews', '%s' ), $slug ); ?></a> |
| 258 |
</div> |
| 259 |
<div> |
| 260 |
<a style="text-decoration: none;" href="<?php echo esc_url( $translate ); ?>" target="_blank"> |
| 261 |
<?php |
| 262 |
/* translators: Plugin translation link */ |
| 263 |
echo sprintf( esc_html__( 'Translations for %s' ), esc_html( $plugin_name ) ); |
| 264 |
?> |
| 265 |
</a> | <a style="text-decoration: none;" href="<?php echo esc_url( $facebook ); ?>" target="_blank"><span class="dashicons dashicons-facebook"></span></a> | <a style="text-decoration: none;" href="<?php echo esc_url( $twitter ); ?>" target="_blank"><span class="dashicons dashicons-twitter"></span></a> | <a style="text-decoration: none;" href="<?php echo esc_url( $youtube ); ?>" target="_blank"><span class="dashicons dashicons-video-alt3"></span></a> |
| 266 |
</div> |
| 267 |
</span> |
| 268 |
|
| 269 |
<div style="width: 250px; height: 180px; margin: 5px; padding: 5px; border: #CCC 2px solid;"> |
| 270 |
<h3><?php sprintf( esc_html_e( 'Please make a donation if you like my work or would like to further the development of this plugin.', '%s' ), $slug ); ?></h3> |
| 271 |
<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> |
| 272 |
<button type="button" style="margin: 5px; padding: 5px;" onclick="window.open('<?php echo esc_url( $donate ); ?>')"><?php esc_html_e( 'Donate to this plugin »' ); ?></button> |
| 273 |
</div> |
| 274 |
|
| 275 |
<?php |
| 276 |
|
| 277 |
} |
| 278 |
|
| 279 |
/** ================================================== |
| 280 |
* Regenerate All Images ('image/jpeg','image/png','image/bmp','image/gif') |
| 281 |
* |
| 282 |
* @param string $to mail address. |
| 283 |
* @since 1.00 |
| 284 |
*/ |
| 285 |
public function allimages_regenerate( $to ) { |
| 286 |
|
| 287 |
global $wpdb; |
| 288 |
$post_ids = $wpdb->get_col( |
| 289 |
" |
| 290 |
SELECT ID |
| 291 |
FROM $wpdb->posts |
| 292 |
WHERE post_type = 'attachment' |
| 293 |
AND post_mime_type IN('image/jpeg','image/png','image/bmp','image/gif') |
| 294 |
AND post_status = 'inherit' |
| 295 |
" |
| 296 |
); |
| 297 |
|
| 298 |
include_once ABSPATH . 'wp-admin/includes/image.php'; |
| 299 |
|
| 300 |
foreach ( $post_ids as $attach_id ) { |
| 301 |
$postdate = get_the_date( 'Y-m-d H:i:s', $attach_id ); |
| 302 |
$postdategmt = get_gmt_from_date( $postdate ); |
| 303 |
$fullpath_filename = get_attached_file( $attach_id ); |
| 304 |
$metadata = wp_get_attachment_metadata( $attach_id ); |
| 305 |
do_action( 'wp_generate_attachment_metadata', $metadata, $attach_id ); |
| 306 |
} |
| 307 |
|
| 308 |
/* translators: blogname for subject */ |
| 309 |
$subject = sprintf( __( '[%s] WebP generate', 'plus-webp' ), get_option( 'blogname' ) ); |
| 310 |
$message = __( 'Generation of WebP is complete.', 'plus-webp' ) . "\r\n\r\n"; |
| 311 |
|
| 312 |
wp_mail( $to, $subject, $message ); |
| 313 |
|
| 314 |
wp_clear_scheduled_hook( 'AllImagesRegenerateHook' ); |
| 315 |
|
| 316 |
} |
| 317 |
|
| 318 |
/** ================================================== |
| 319 |
* Update wp_options table. |
| 320 |
* |
| 321 |
* @since 1.02 |
| 322 |
*/ |
| 323 |
private function options_updated() { |
| 324 |
|
| 325 |
if ( isset( $_POST['Manageset'] ) && ! empty( $_POST['Manageset'] ) ) { |
| 326 |
if ( check_admin_referer( 'pwp_set', 'pluswebp_set' ) ) { |
| 327 |
$pluswebp_settings = get_option( 'pluswebp' ); |
| 328 |
if ( isset( $_POST['replace'] ) && ! empty( $_POST['replace'] ) ) { |
| 329 |
$pluswebp_settings['replace'] = true; |
| 330 |
} else { |
| 331 |
$pluswebp_settings['replace'] = false; |
| 332 |
} |
| 333 |
if ( isset( $_POST['quality'] ) ) { |
| 334 |
$pluswebp_settings['quality'] = intval( $_POST['quality'] ); |
| 335 |
} |
| 336 |
update_option( 'pluswebp', $pluswebp_settings ); |
| 337 |
echo '<div class="notice notice-success is-dismissible"><ul><li>' . esc_html__( 'Settings' ) . ' --> ' . esc_html__( 'Settings saved.' ) . '</li></ul></div>'; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
} |
| 342 |
|
| 343 |
} |
| 344 |
|
| 345 |
?> |
| 346 |
|