| 1 |
<?php |
| 2 |
/** |
| 3 |
* The admin-specific functionality of the plugin. |
| 4 |
* |
| 5 |
* @link http://webdeclic.com |
| 6 |
* @since 1.0.0 |
| 7 |
* |
| 8 |
* @package Quickwebp |
| 9 |
* @subpackage Quickwebp/admin |
| 10 |
*/ |
| 11 |
class Quickwebp_Settings { |
| 12 |
|
| 13 |
/** |
| 14 |
* The ID of this plugin. |
| 15 |
* |
| 16 |
* @since 1.0.0 |
| 17 |
* @access private |
| 18 |
* @var string $plugin_name The ID of this plugin. |
| 19 |
*/ |
| 20 |
private $plugin_name; |
| 21 |
|
| 22 |
/** |
| 23 |
* The version of this plugin. |
| 24 |
* |
| 25 |
* @since 1.0.0 |
| 26 |
* @access private |
| 27 |
* @var string $version The current version of this plugin. |
| 28 |
*/ |
| 29 |
private $version; |
| 30 |
|
| 31 |
/** |
| 32 |
* Initialize the class and set its properties. |
| 33 |
* |
| 34 |
* @since 1.0.0 |
| 35 |
* @param string $plugin_name The name of this plugin. |
| 36 |
* @param string $version The version of this plugin. |
| 37 |
*/ |
| 38 |
public function __construct( $plugin_name, $version ) { |
| 39 |
|
| 40 |
$this->plugin_name = $plugin_name; |
| 41 |
$this->version = $version; |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Enqueue scripts and styles |
| 47 |
* |
| 48 |
*/ |
| 49 |
public function enqueue_scripts_styles( $hook_suffix ) { |
| 50 |
if ( $hook_suffix == 'toplevel_page_quickwebp-settings' || $hook_suffix == 'media_page_quickwebp-settings' ) { |
| 51 |
|
| 52 |
$admin_main_settings_assets = include( QUICKWEBP_PLUGIN_PATH . 'public/assets/build/admin-main-settings.asset.php' ); |
| 53 |
wp_enqueue_style( 'quickwebpoi_admin_main_settings', QUICKWEBP_PLUGIN_URL . 'public/assets/build/admin-main-settings.css', array(), $admin_main_settings_assets['version'], 'all' ); |
| 54 |
wp_enqueue_script( 'quickwebpoi_admin_main_settings', QUICKWEBP_PLUGIN_URL . 'public/assets/build/admin-main-settings.js', $admin_main_settings_assets['dependencies'], $admin_main_settings_assets['version'], true ); |
| 55 |
wp_localize_script( 'quickwebpoi_admin_main_settings', 'QUICKWEBP_ADMIN_SETTINGS', array( |
| 56 |
'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
| 57 |
'nonce' => wp_create_nonce( 'image_optimize_nonce' ), |
| 58 |
)); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* add_settings_menu |
| 64 |
* |
| 65 |
* @return void |
| 66 |
*/ |
| 67 |
public function add_settings_menu() { |
| 68 |
add_submenu_page( |
| 69 |
'upload.php', |
| 70 |
__('Quickwebp Settings', QUICKWEBP_TEXT_DOMAIN), |
| 71 |
__('Quickwebp', QUICKWEBP_TEXT_DOMAIN), |
| 72 |
'manage_options', |
| 73 |
'quickwebp-settings', |
| 74 |
array( $this, 'render_settings_page' ) |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* render_settings_page |
| 80 |
* |
| 81 |
* @return void |
| 82 |
*/ |
| 83 |
public function render_settings_page() { |
| 84 |
global $is_nginx; |
| 85 |
|
| 86 |
require_once QUICKWEBP_PLUGIN_PATH . 'admin/templates/page-settings.php'; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* render_component |
| 91 |
* |
| 92 |
* @param mixed $data |
| 93 |
* @return void |
| 94 |
*/ |
| 95 |
public function render_component( $data = array() ) { |
| 96 |
$data['type'] = $data['type'] ?? 'text'; |
| 97 |
$data['name'] = $data['name'] ?? ''; |
| 98 |
$file_name = $data['type'] == 'text' || $data['type'] == 'email' || $data['type'] == 'tel' || $data['type'] == 'number' ? 'text' : $data['type']; |
| 99 |
$path_to_component = QUICKWEBP_PLUGIN_PATH . 'admin/components/' . $file_name . '.php'; |
| 100 |
|
| 101 |
if( file_exists( $path_to_component ) ) { |
| 102 |
?> |
| 103 |
<tr> |
| 104 |
<th> |
| 105 |
<label for="<?php echo esc_attr( $data['name'] ?? '' ); ?>"><?php echo esc_html( $data['label'] ?? '' ); ?></label> |
| 106 |
</th> |
| 107 |
<td class="<?php echo esc_attr( $data['name'] ?? '' ); ?>-container"> |
| 108 |
<?php include $path_to_component; ?> |
| 109 |
<?php if( isset( $data['description'] ) ) { |
| 110 |
?> |
| 111 |
<p class="description"><?php echo esc_html( $data['description'] ); ?></p> |
| 112 |
<?php |
| 113 |
} |
| 114 |
?> |
| 115 |
</td> |
| 116 |
</tr> |
| 117 |
<?php |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Add the settings link |
| 123 |
*/ |
| 124 |
public function add_settings_link( $plugin_actions, $plugin_file ) { |
| 125 |
|
| 126 |
$new_actions = array(); |
| 127 |
|
| 128 |
if ( 'quickwebp/quickwebp.php' === $plugin_file ) { |
| 129 |
|
| 130 |
$new_actions['settings'] = sprintf( __( '<a href="%s">Settings</a>', QUICKWEBP_TEXT_DOMAIN ), esc_url( admin_url( 'upload.php?page=quickwebp-settings' ) ) ); |
| 131 |
} |
| 132 |
|
| 133 |
return array_merge( $new_actions, $plugin_actions ); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Show notice |
| 138 |
*/ |
| 139 |
public function show_notice_if_library_not_exist() { |
| 140 |
|
| 141 |
$library_to_use = get_option( 'quickwebp_settings_library', quickwebp_settings_default('quickwebp_settings_library') ); |
| 142 |
|
| 143 |
switch ($library_to_use) { |
| 144 |
case 'gd': |
| 145 |
|
| 146 |
if ( ! extension_loaded('gd') ) { |
| 147 |
add_action( 'admin_notices', function() { |
| 148 |
?> |
| 149 |
<div class="notice notice-error"> |
| 150 |
<p><?php _e( 'Oops! QuickWebP needs the GD library to function properly, but it looks like the library is not installed on your server.', QUICKWEBP_TEXT_DOMAIN ); ?></p> |
| 151 |
<p><?php _e( 'Please contact your hosting provider and ask them to install the GD library for PHP. They should be able to assist you with this process. Once the library is installed, QuickWebP should work as expected.', QUICKWEBP_TEXT_DOMAIN ); ?></p> |
| 152 |
<p><?php _e( 'Thank you for using QuickWebP!', QUICKWEBP_TEXT_DOMAIN ); ?></p> |
| 153 |
</div> |
| 154 |
<?php |
| 155 |
}); |
| 156 |
} |
| 157 |
|
| 158 |
break; |
| 159 |
|
| 160 |
case 'imagick': |
| 161 |
|
| 162 |
if ( ! extension_loaded('imagick') ) { |
| 163 |
add_action( 'admin_notices', function() { |
| 164 |
?> |
| 165 |
<div class="notice notice-error"> |
| 166 |
<p><?php _e( 'Oops! QuickWebP needs the Imagick library to function properly, but it looks like the library is not installed on your server.', QUICKWEBP_TEXT_DOMAIN ); ?></p> |
| 167 |
<p><?php _e( 'Please contact your hosting provider and ask them to install the Imagick library for PHP. They should be able to assist you with this process. Once the library is installed, QuickWebP should work as expected.', QUICKWEBP_TEXT_DOMAIN ); ?></p> |
| 168 |
<p><?php _e( 'Thank you for using QuickWebP!', QUICKWEBP_TEXT_DOMAIN ); ?></p> |
| 169 |
</div> |
| 170 |
<?php |
| 171 |
}); |
| 172 |
} |
| 173 |
|
| 174 |
break; |
| 175 |
|
| 176 |
default: |
| 177 |
|
| 178 |
add_action( 'admin_notices', function() { |
| 179 |
?> |
| 180 |
<div class="notice notice-error"> |
| 181 |
<p><?php _e( 'Oops! QuickWebP needs the Imagick or GD library to function properly, but it looks like the no Imagick nor GD is not installed on your server.', QUICKWEBP_TEXT_DOMAIN ); ?></p> |
| 182 |
<p><?php _e( 'Please contact your hosting provider and ask them to install the Imagick or GD library for PHP. They should be able to assist you with this process. Once the library is installed, QuickWebP should work as expected.', QUICKWEBP_TEXT_DOMAIN ); ?></p> |
| 183 |
<p><?php _e( 'Thank you for using QuickWebP!', QUICKWEBP_TEXT_DOMAIN ); ?></p> |
| 184 |
</div> |
| 185 |
<?php |
| 186 |
}); |
| 187 |
|
| 188 |
break; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Display web mode changed |
| 196 |
*/ |
| 197 |
public function add_rewrite_rules( $values ) { |
| 198 |
global $is_apache, $is_iis7, $is_nginx; |
| 199 |
|
| 200 |
include_once QUICKWEBP_PLUGIN_PATH . 'admin/rewrite-rules/class-apache.php'; |
| 201 |
include_once QUICKWEBP_PLUGIN_PATH . 'admin/rewrite-rules/class-nginx.php'; |
| 202 |
include_once QUICKWEBP_PLUGIN_PATH . 'admin/rewrite-rules/class-iis.php'; |
| 203 |
|
| 204 |
$old_value = get_option( 'quickwebp_settings_conversion_display_webp_mode', quickwebp_settings_default('quickwebp_settings_conversion_display_webp_mode') ); |
| 205 |
$is_rewrite = 'rewrite' === $values; |
| 206 |
$was_rewrite = 'rewrite' === $old_value; |
| 207 |
$add_or_remove = false; |
| 208 |
|
| 209 |
if ( $is_rewrite && !$was_rewrite ) { |
| 210 |
$add_or_remove = 'add'; |
| 211 |
} elseif ( !$is_rewrite && $was_rewrite ) { |
| 212 |
$add_or_remove = 'remove'; |
| 213 |
} else { |
| 214 |
return $values; |
| 215 |
} |
| 216 |
|
| 217 |
if ( $is_apache ) { |
| 218 |
$rules = new Apache(); |
| 219 |
} elseif ( $is_iis7 ) { |
| 220 |
$rules = new IIS(); |
| 221 |
} elseif ( $is_nginx ) { |
| 222 |
$rules = new Nginx(); |
| 223 |
} else { |
| 224 |
return $values; |
| 225 |
} |
| 226 |
|
| 227 |
if ( 'add' === $add_or_remove ) { |
| 228 |
$result = $rules->add(); |
| 229 |
} else { |
| 230 |
$result = $rules->remove(); |
| 231 |
} |
| 232 |
|
| 233 |
if ( is_wp_error( $result ) ) { |
| 234 |
add_action( 'admin_notices', function() use ( $result ) { |
| 235 |
?> |
| 236 |
<div class="notice notice-error"> |
| 237 |
<p><?php echo $result->get_error_message(); ?></p> |
| 238 |
</div> |
| 239 |
<?php |
| 240 |
}); |
| 241 |
} |
| 242 |
|
| 243 |
return $values; |
| 244 |
} |
| 245 |
|
| 246 |
} |