Plugin Directory

Changeset 3319728


Ignore:
Timestamp:
06/30/2025 07:13:34 AM (9 months ago)
Author:
avi.megladon
Message:

Bug fixes and improvement is image conversion

Location:
webp-converter
Files:
35 added
6 edited

Legend:

Unmodified
Added
Removed
  • webp-converter/trunk/includes/class-activate.php

    r2597750 r3319728  
    11<?php
    2 namespace WEBP\Activate;
    3 use WEBP\Htserve as Htserve;
    42
    5 class APWEBP_Activate {
     3class APWEBP_Activate
     4{
    65
    7     static function webp_install() {
     6    public static function webp_install()
     7    {
    88        update_option('apwebp_image_quality', 50);
    99    }
    1010
    11     static function webp_uninstall() {
    12         $hts = new Htserve\APWEBP_Htserve;
     11    public static function webp_uninstall()
     12    {
     13        $hts = new APWEBP_Htserve;
    1314        $hts->remove_ht_data();
    1415        delete_option('apwebp_use_htaccess');
  • webp-converter/trunk/includes/class-htserve.php

    r2597187 r3319728  
    11<?php
    2 namespace WEBP\Htserve;
    3 
    4 class APWEBP_Htserve {
     2
     3class APWEBP_Htserve
     4{
    55
    66    public $wp_content_dir = '';
    7     public $wp_content_ht = '';
    8     public $uploads_dir = '';
    9     public $uploads_ht = '';
    10     public $ht_pre = '## START WEBP CONVERTER ##';
    11     public $ht_post = '## END WEBP CONVERTER ##';
    12 
    13     public function __construct() {
     7    public $wp_content_ht  = '';
     8    public $uploads_dir    = '';
     9    public $uploads_ht     = '';
     10    public $ht_pre         = '## START WEBP CONVERTER ##';
     11    public $ht_post        = '## END WEBP CONVERTER ##';
     12
     13    public function __construct()
     14    {
    1415        $this->wp_content_dir = WP_CONTENT_DIR;
    15         $this->wp_content_ht = $this->wp_content_dir . '/' . '.htaccess';
    16         $upload_dir = wp_upload_dir();
    17         $this->uploads_dir = $upload_dir['basedir'];
    18         $this->uploads_ht = $this->uploads_dir . '/' . '.htaccess';
    19         add_action('apwebp_save_settings', array($this, 'use_htaccess'));
    20     }
    21 
    22     public function use_htaccess() {
     16        $this->wp_content_ht  = $this->wp_content_dir . '/' . '.htaccess';
     17        $upload_dir           = wp_upload_dir();
     18        $this->uploads_dir    = $upload_dir['basedir'];
     19        $this->uploads_ht     = $this->uploads_dir . '/' . '.htaccess';
     20        add_action('apwebp_save_settings', [$this, 'use_htaccess']);
     21    }
     22
     23    public function use_htaccess()
     24    {
    2325
    2426        $wpc_ht_created = false;
     
    3234            $this->remove_ht_data();
    3335            delete_option('apwebp_use_htaccess');
    34             add_filter('webp_msg_filter', array($this, 'files_removed'), 10, 1);
     36            add_filter('webp_msg_filter', [$this, 'files_removed'], 10, 1);
    3537        }
    3638
    3739        if ($wpc_ht_created and $wpu_ht_created) {
    38             add_filter('webp_msg_filter', array($this, 'files_created'), 10, 1);
    39         }
    40 
    41     }
    42 
    43     function add_wpc_ht_data() {
     40            add_filter('webp_msg_filter', [$this, 'files_created'], 10, 1);
     41        }
     42
     43    }
     44
     45    public function add_wpc_ht_data()
     46    {
    4447        if (is_writable($this->wp_content_dir)) {
    4548            if ($this->is_wpc_ht_exists()) {
     
    5861            return true;
    5962        } else {
    60             add_filter('webp_msg_filter', array($this, 'file_not_writable'), 10, 1);
     63            add_filter('webp_msg_filter', [$this, 'file_not_writable'], 10, 1);
    6164        }
    6265        return false;
    6366    }
    6467
    65     function add_wpu_ht_data() {
     68    public function add_wpu_ht_data()
     69    {
    6670        if (is_writable($this->uploads_dir)) {
    6771            if ($this->is_wpu_ht_exists()) {
     
    8084            return true;
    8185        } else {
    82             add_filter('webp_msg_filter', array($this, 'file_not_writable_uploads'), 10, 1);
     86            add_filter('webp_msg_filter', [$this, 'file_not_writable_uploads'], 10, 1);
    8387        }
    8488        return false;
    8589    }
    8690
    87     public function remove_ht_data() {
     91    public function remove_ht_data()
     92    {
    8893        if ($this->is_wpc_ht_exists()) {
    8994            $oldht_data = file_get_contents($this->wp_content_ht);
     
    102107    }
    103108
    104     public function get_wpc_ht_data() {
     109    public function get_wpc_ht_data()
     110    {
    105111        $data = PHP_EOL;
    106112        $data .= '<IfModule mod_rewrite.c>' . PHP_EOL;
     
    129135    }
    130136
    131     public function get_wpu_ht_data() {
     137    public function get_wpu_ht_data()
     138    {
    132139        $data = PHP_EOL;
    133140        $data .= '<IfModule mod_rewrite.c>' . PHP_EOL;
     
    156163    }
    157164
    158     public function is_wpc_ht_exists() {
     165    public function is_wpc_ht_exists()
     166    {
    159167        if (is_file($this->wp_content_ht)) {
    160168            return true;
     
    164172    }
    165173
    166     public function is_wpu_ht_exists() {
     174    public function is_wpu_ht_exists()
     175    {
    167176        if (is_file($this->uploads_ht)) {
    168177            return true;
     
    172181    }
    173182
    174     public function get_wpc_ht_data_to_user() {
    175         if (!$this->is_wpc_ht_exists() or !is_writable($this->wp_content_dir)) {
     183    public function get_wpc_ht_data_to_user()
     184    {
     185        if (! $this->is_wpc_ht_exists() or ! is_writable($this->wp_content_dir)) {
    176186            return nl2br(stripslashes(esc_html($this->ht_pre . $this->get_wpc_ht_data() . $this->ht_post)));
    177187        } else {
     
    180190    }
    181191
    182     public function get_wpu_ht_data_to_user() {
    183         if (!$this->is_wpu_ht_exists() or !is_writable($this->uploads_dir)) {
     192    public function get_wpu_ht_data_to_user()
     193    {
     194        if (! $this->is_wpu_ht_exists() or ! is_writable($this->uploads_dir)) {
    184195            return nl2br(stripslashes(esc_html($this->ht_pre . $this->get_wpu_ht_data() . $this->ht_post)));
    185196        } else {
     
    188199    }
    189200
    190     public function file_not_writable($msg) {
     201    public function file_not_writable($msg)
     202    {
    191203        return $msg . '<font color="red">' . __('/wp-content/ dir not writable. Please check if the dir has correct permissions.', 'webp-converter') . '</font>' . '<br>';
    192204    }
    193205
    194     public function file_not_writable_uploads($msg) {
     206    public function file_not_writable_uploads($msg)
     207    {
    195208        return $msg . '<font color="red">' . __('/uploads/ dir not writable. Please check if the dir has correct permissions.', 'webp-converter') . '</font>' . '<br>';
    196209    }
    197210
    198     public function files_created($msg) {
     211    public function files_created($msg)
     212    {
    199213        return $msg . '<font color="green">' . __('.htaccess files are created successfully.', 'webp-converter') . '</font>' . '<br>';
    200214    }
    201215
    202     public function files_removed($msg) {
     216    public function files_removed($msg)
     217    {
    203218        return $msg . '<font color="red">' . __('Images will not be served as webp by .htaccess', 'webp-converter') . '</font>' . '<br>';
    204219    }
  • webp-converter/trunk/includes/class-scripts.php

    r2593052 r3319728  
    11<?php
    2 namespace WEBP\Scripts;
    32
    4 class APWEBP_Converter_Scripts {
     3class APWEBP_Converter_Scripts
     4{
    55
    6     public function __construct() {
    7         add_action('admin_enqueue_scripts', array($this, 'additional_scripts_admin'));
     6    public function __construct()
     7    {
     8        add_action('admin_enqueue_scripts', [$this, 'additional_scripts_admin']);
    89    }
    910
    10     public function additional_scripts_admin() {
     11    public function additional_scripts_admin()
     12    {
    1113        wp_enqueue_script('jquery-ui-tooltip');
    1214        wp_enqueue_script('ap.cookie', plugins_url(APWEBP_DIRECTORY_NAME . '/js/ap.cookie.js'));
     
    1416
    1517        wp_register_script('webp', plugins_url(APWEBP_DIRECTORY_NAME . '/js/webp.js'));
    16         wp_localize_script('webp', 'apwebp_ajax', array('ajaxurl' => admin_url('admin-ajax.php'), 'pluginimg' => plugins_url(APWEBP_DIRECTORY_NAME . '/images/')));
     18        wp_localize_script('webp', 'apwebp_ajax', ['ajaxurl' => admin_url('admin-ajax.php'), 'pluginimg' => plugins_url(APWEBP_DIRECTORY_NAME . '/images/')]);
    1719        wp_enqueue_script('webp');
    1820
  • webp-converter/trunk/includes/class-settings.php

    r2597750 r3319728  
    11<?php
    2 namespace WEBP\Settings;
    3 use WEBP\Htserve as Htserve;
    4 
    5 class APWEBP_Converter_Settings {
    6 
    7     public function __construct() {
    8         add_action('admin_menu', array($this, 'admin_menu'));
    9         add_action('wp_ajax_webpDoConvert', array($this, 'apwebp_do_convert'));
    10         add_action('wp_ajax_webpPluginStatus', array($this, 'apwebp_plugin_status'));
    11         add_action('admin_init', array($this, 'apwebp_options_save_settings'));
    12         add_filter('set-screen-option', array($this, 'apwebp_set_screen_option'), 10, 3);
    13     }
    14 
    15     public function apwebp_options_save_settings() {
     2
     3class APWEBP_Converter_Settings
     4{
     5
     6    public function __construct()
     7    {
     8        add_action('admin_menu', [$this, 'admin_menu']);
     9        add_action('wp_ajax_webpDoConvert', [$this, 'apwebp_do_convert']);
     10        add_action('wp_ajax_webpPluginStatus', [$this, 'apwebp_plugin_status']);
     11        add_action('admin_init', [$this, 'apwebp_options_save_settings']);
     12        add_filter('set-screen-option', [$this, 'apwebp_set_screen_option'], 10, 3);
     13    }
     14
     15    public function apwebp_options_save_settings()
     16    {
    1617        if (isset($_POST['option']) and sanitize_text_field($_POST['option']) == "apwebp_save_settings") {
    1718
    18             if (!isset($_POST['apwebp_options_save_action_field']) || !wp_verify_nonce($_POST['apwebp_options_save_action_field'], 'apwebp_options_save_action')) {
     19            if (! isset($_POST['apwebp_options_save_action_field']) || ! wp_verify_nonce($_POST['apwebp_options_save_action_field'], 'apwebp_options_save_action')) {
    1920                wp_die('Sorry, your nonce did not verify.');
    2021            }
     
    4445    }
    4546
    46     public function admin_menu() {
     47    public function admin_menu()
     48    {
    4749        global $apwebp_list_page;
    48         $apwebp_list_page = add_menu_page('WEBP Converter', 'WEBP Converter', 'manage_options', 'apwebp_converter_setup_data', array($this, 'converter_data'), 'dashicons-admin-tools');
    49         add_action("load-$apwebp_list_page", array($this, "apwebp_listing_screen_options"));
    50         add_submenu_page('apwebp_converter_setup_data', 'Settings', 'Settings', 'activate_plugins', 'apwebp_converter_settings', array($this, 'settings'));
    51     }
    52 
    53     public function apwebp_set_screen_option($status, $option, $value) {
     50        $apwebp_list_page = add_menu_page('WEBP Converter', 'WEBP Converter', 'manage_options', 'apwebp_converter_setup_data', [$this, 'converter_data'], 'dashicons-admin-tools');
     51        add_action("load-$apwebp_list_page", [$this, "apwebp_listing_screen_options"]);
     52        add_submenu_page('apwebp_converter_setup_data', 'Settings', 'Settings', 'activate_plugins', 'apwebp_converter_settings', [$this, 'settings']);
     53    }
     54
     55    public function apwebp_set_screen_option($status, $option, $value)
     56    {
    5457        if ('images_per_page' == $option) {
    5558            return $value;
     
    5760    }
    5861
    59     public function apwebp_listing_screen_options() {
     62    public function apwebp_listing_screen_options()
     63    {
    6064        global $apwebp_list_page;
    6165        $screen = get_current_screen();
    62         if (!is_object($screen) || $screen->id != $apwebp_list_page) {
     66        if (! is_object($screen) || $screen->id != $apwebp_list_page) {
    6367            return;
    6468        }
    65         $args = array(
    66             'label' => __('Images per page', 'webp-converter'),
     69        $args = [
     70            'label'   => __('Images per page', 'webp-converter'),
    6771            'default' => 5,
    68             'option' => 'images_per_page',
    69         );
     72            'option'  => 'images_per_page',
     73        ];
    7074        add_screen_option('per_page', $args);
    7175    }
    7276
    73     public function pro_add() {
     77    public function pro_add()
     78    {
    7479        include APWEBP_DIRECTORY_PATH . '/view/admin/pro-add.php';
    7580    }
    7681
    77     public function help_support() {
     82    public function help_support()
     83    {
    7884        include APWEBP_DIRECTORY_PATH . '/view/admin/help.php';
    7985    }
    8086
    81     public function converter_data() {
     87    public function converter_data()
     88    {
    8289        global $id_spliter;
    83         $user = get_current_user_id();
    84         $screen = get_current_screen();
     90        $user          = get_current_user_id();
     91        $screen        = get_current_screen();
    8592        $screen_option = $screen->get_option('per_page', 'option');
    86         $per_page = get_user_meta($user, $screen_option, true);
     93        $per_page      = get_user_meta($user, $screen_option, true);
    8794        include APWEBP_DIRECTORY_PATH . '/view/admin/converter-data.php';
    8895    }
    8996
    90     public function settings() {
    91         $hts = new Htserve\APWEBP_Htserve;
     97    public function settings()
     98    {
     99        $hts = new APWEBP_Htserve;
    92100        include APWEBP_DIRECTORY_PATH . '/view/admin/settings.php';
    93101    }
    94102
    95     function call_web_service($url, $data = [], $headers = []) {
    96         $response = wp_remote_post($url, array(
    97             'body' => json_encode($data),
     103    public function call_web_service($url, $data = [], $headers = [])
     104    {
     105        $response = wp_remote_post($url, [
     106            'body'    => json_encode($data),
    98107            'headers' => $headers,
    99         ));
     108        ]);
    100109        if (is_wp_error($response)) {
    101110            return;
     
    105114    }
    106115
    107     public function apwebp_do_convert() {
     116    public function apwebp_do_convert()
     117    {
    108118        global $id_spliter, $apwebp_token, $apwebp_api_base;
    109119
    110         $rid = sanitize_text_field($_POST['id']);
     120        $rid        = sanitize_text_field($_POST['id']);
    111121        $thumb_type = '';
    112122        if (strpos($rid, $id_spliter) !== false) {
    113             $id_exp = explode($id_spliter, $rid);
     123            $id_exp     = explode($id_spliter, $rid);
    114124            $thumb_type = $id_exp[0];
    115             $id = $id_exp[1];
     125            $id         = $id_exp[1];
    116126        } else {
    117127            $id = $rid;
    118128        }
    119129
    120         $upload_dir = wp_upload_dir();
     130        $upload_dir  = wp_upload_dir();
    121131        $image_datas = wp_get_attachment_metadata($id);
    122         $info = pathinfo($image_datas['file']);
    123         $file_dir = $info['dirname'];
    124         $base_dir = $upload_dir['basedir'];
     132        $info        = pathinfo($image_datas['file']);
     133        $file_dir    = $info['dirname'];
     134        $base_dir    = $upload_dir['basedir'];
    125135
    126136        $url = $apwebp_api_base . 'convert-free.php';
    127137
    128         $headers = array(
    129             'token' => $apwebp_token,
     138        $headers = [
     139            'token'        => $apwebp_token,
    130140            'Content-Type' => 'application/json',
    131         );
     141        ];
    132142
    133143        if ($thumb_type == '') {
    134144            $filename_no_ext = $info['filename'];
    135             $filename_ext = $info['basename'];
    136             $file_full_url = $upload_dir['baseurl'] . '/' . $file_dir . '/' . $filename_ext;
     145            $filename_ext    = $info['basename'];
     146            $file_full_url   = $upload_dir['baseurl'] . '/' . $file_dir . '/' . $filename_ext;
    137147
    138148            $post = [
    139                 'url' => $file_full_url,
     149                'url'     => $file_full_url,
    140150                'quality' => get_option('apwebp_image_quality'),
    141151            ];
     
    143153            $response = $this->call_web_service($url, $post, $headers);
    144154        } else {
    145             $thumb_file = $image_datas['sizes'][$thumb_type];
    146             $info_thumb = pathinfo($thumb_file['file']);
     155            $thumb_file      = $image_datas['sizes'][$thumb_type];
     156            $info_thumb      = pathinfo($thumb_file['file']);
    147157            $filename_no_ext = $info_thumb['filename'];
    148             $filename_ext = $info_thumb['basename'];
    149             $file_full_url = $upload_dir['baseurl'] . '/' . $file_dir . '/' . $filename_ext;
     158            $filename_ext    = $info_thumb['basename'];
     159            $file_full_url   = $upload_dir['baseurl'] . '/' . $file_dir . '/' . $filename_ext;
    150160
    151161            $post = [
    152                 'url' => $file_full_url,
     162                'url'     => $file_full_url,
    153163                'quality' => get_option('apwebp_image_quality'),
    154164            ];
     
    156166        }
    157167
    158         $data = unserialize($response);
     168        $data = json_decode($response, true);
    159169
    160170        if ($data['status'] == 'success') {
    161171            if (is_writable($base_dir . '/' . $file_dir)) {
    162172                $fp = fopen($base_dir . '/' . $file_dir . '/' . $filename_no_ext . '.webp', 'w');
    163                 fwrite($fp, $data['image']);
     173
     174                $base64_image = $data['image'];
     175                $base64       = preg_replace('#^data:image/webp;base64,#', '', $base64_image);
     176                $image_data   = base64_decode($base64);
     177
     178                fwrite($fp, $image_data);
    164179                fclose($fp);
    165                 echo json_encode(array('status' => 'success', 'msg' => '<font color="green">' . $data['msg'] . '</font>'));
    166             } else {
    167                 echo json_encode(array('status' => 'error', 'msg' => '<font color="red">Image not created</font>'));
    168             }
    169         } else {
    170             echo json_encode(array('status' => 'error', 'msg' => '<font color="red">' . $data['msg'] . '</font>'));
     180                echo json_encode(['status' => 'success', 'msg' => '<font color="green">' . $data['msg'] . '</font>']);
     181            } else {
     182                echo json_encode(['status' => 'error', 'msg' => '<font color="red">Image not created</font>']);
     183            }
     184        } else {
     185            echo json_encode(['status' => 'error', 'msg' => '<font color="red">' . $data['msg'] . '</font>']);
    171186        }
    172187
     
    174189    }
    175190
    176     public function apwebp_plugin_status() {
    177         $m = '';
     191    public function apwebp_plugin_status()
     192    {
     193        $m   = '';
    178194        $ret = curl_response_aviplugins(AP_API_BASE . 'api.php?option=webp_free_status_check&site_url=' . urlencode(site_url('/')));
    179195        if ($ret->status == 'success') {
     
    182198            $m = $ret->msg;
    183199        }
    184         echo json_encode(array('status' => 'success', 'msg' => $m));
     200        echo json_encode(['status' => 'success', 'msg' => $m]);
    185201        wp_die();
    186202    }
  • webp-converter/trunk/readme.txt

    r2723761 r3319728  
    44Tags: webp, nextgen, converter, image, images, media, image converter, seed, speed up, google page speeds, gtmatrix, fast page load, fast site, super fast site
    55Requires at least: 2.0.2
    6 Tested up to: 5.9.3
    7 Stable tag: 1.0.1
     6Tested up to: 6.8.1
     7Stable tag: 2.0.0
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    4444== Changelog ==
    4545
     46= 2.0.0 =
     47* Bug fixes for wordpress > 6 and PHP > 7
     48
    4649= 1.0.1 =
    4750* Now embedded images can be served as webp using .htaccess
  • webp-converter/trunk/webp-converter.php

    r2597750 r3319728  
    44Plugin URI: #
    55Description: Speed up your website by serving images in WebP format. This plugin will replace files in standard JPEG, PNG and GIF formats with WebP format.
    6 Version: 1.0.1
     6Version: 2.0.0
    77Text Domain: webp-converter
    88Domain Path: /languages
     
    1818*/
    1919
    20 namespace WEBP\Init;
    21 use WEBP\Activate as Activate;
    22 use WEBP\Htserve as Htserve;
    23 use WEBP\Init as Init;
    24 use WEBP\Scripts as Scripts;
    25 use WEBP\Settings as Settings;
    26 
    2720define('APWEBP_NAME', 'Webp Converter');
    2821define('APWEBP_DIRECTORY_NAME', 'webp-converter');
    2922define('APWEBP_DIRECTORY_PATH', dirname(__FILE__));
    30 if (!defined('AP_SITE')) {
     23if (! defined('AP_SITE')) {
    3124    define('AP_SITE', 'https://www.aviplugins.com/');
    3225}
    33 if (!defined('AP_API_BASE')) {
     26if (! defined('AP_API_BASE')) {
    3427    define('AP_API_BASE', AP_SITE . 'api/');
    3528}
     
    3730include_once APWEBP_DIRECTORY_PATH . '/includes/class-activate.php';
    3831
    39 class APWEBP_Converter_Pre_Checking {
    40     public function __construct() {
     32class APWEBP_Converter_Pre_Checking
     33{
     34    public function __construct()
     35    {
    4136        $this->plug_install_apwebp_converter();
    4237    }
    4338
    44     public function plug_install_apwebp_converter() {
     39    public function plug_install_apwebp_converter()
     40    {
    4541        include_once ABSPATH . 'wp-admin/includes/plugin.php';
    4642        if (is_plugin_active('webp-converter-pro/webp-converter.php')) {
     
    5551        include_once APWEBP_DIRECTORY_PATH . '/functions.php';
    5652
    57         new Settings\APWEBP_Converter_Settings;
    58         new Htserve\APWEBP_Htserve;
    59         new Scripts\APWEBP_Converter_Scripts;
     53        new APWEBP_Converter_Settings;
     54        new APWEBP_Htserve;
     55        new APWEBP_Converter_Scripts;
    6056    }
    6157}
    6258
    63 new Init\APWEBP_Converter_Pre_Checking;
    64 $webpp_activate = new Activate\APWEBP_Activate;
     59new APWEBP_Converter_Pre_Checking;
     60$webpp_activate = new APWEBP_Activate;
    6561
    66 register_activation_hook(__FILE__, array($webpp_activate, 'webp_install'));
    67 register_deactivation_hook(__FILE__, array($webpp_activate, 'webp_uninstall'));
     62register_activation_hook(__FILE__, [$webpp_activate, 'webp_install']);
     63register_deactivation_hook(__FILE__, [$webpp_activate, 'webp_uninstall']);
    6864
    6965add_filter('wp_get_attachment_image_src', 'apwebp_ap_convert_url_for_images', 10, 4);
Note: See TracChangeset for help on using the changeset viewer.