Plugin Directory

Changeset 2261008


Ignore:
Timestamp:
03/14/2020 10:15:05 AM (6 years ago)
Author:
image4io
Message:

Version 0.2.0

Location:
image4io
Files:
122 added
8 edited

Legend:

Unmodified
Added
Removed
  • image4io/trunk/image4io.php

    r2253695 r2261008  
    88Description: Speeds up the images' load time: image optimization, image CDN and image storage, all-in-one platform. To get started: activate the image4io plugin and then go to your Image4io Settings page to set up your credentials.
    99Author: image4io
    10 Version: 0.1.0
     10Version: 0.2.0
    1111Author URI: https://image4.io
    1212*/
  • image4io/trunk/inc/Api/Callbacks/AdminCallbacks.php

    r2253695 r2261008  
    4949        echo "<input type='text' id='$name' class='regular-text' name='" . $optionName . "[" . $name . "]' value='$value' placeholder='Your cloudname'>";
    5050    }
    51     public function image4ioFolder($args){
    52         $name=$args['label_for'];
    53         $optionName=$args['option_name'];
    54         $options=get_option($optionName);
    55         $value=isset($options[$name])?$options[$name]:"";
    56 
    57         echo "<input type='text' id='$name' class='regular-text' name='" . $optionName . "[" . $name . "]' value='$value' placeholder='Which folder to show? (Root is /)'>";
    58     }
    5951
    6052}
  • image4io/trunk/inc/Api/Image4IOApi.php

    r2253449 r2261008  
    5151
    5252        $args= array(
    53             'headers'=>$this->getHeaders()
     53            'headers'=>$this->getHeaders(),
     54            'timeout'=>30
    5455        );
    5556        $response=wp_remote_post( $this->urlWithVersion . 'fetch?from=' . urlencode($from) . '&target_path=' . urlencode($targetPath) , $args );
  • image4io/trunk/inc/Api/Image4IOManager.php

    r2253449 r2261008  
    3535        return $this->apiClient->connect();
    3636    }
    37 
     37/*
    3838    public function uploadToImage4IO($dirpath , $cloudPath){
    3939        if(!$this->isValidOptions()){
     
    4646        $result=$this->apiClient->uploadImage($dirpath,$cloudPath);
    4747        return json_decode($result);
     48    }*/
     49
     50    public function uploadToImage4ioFromUrl($from_url,$target_path){
     51        if(!$this->isValidOptions()){
     52            return; //redirect to options page?
     53        }
     54        $result=$this->apiClient->fetch($from_url,$target_path);
     55        return json_decode( $result );
    4856    }
    4957   
  • image4io/trunk/inc/Base/Enqueue.php

    r2253449 r2261008  
    1717    function enqueue($hook) {
    1818        // enqueue all our scripts
    19         wp_enqueue_style( 'image4io_plugin_style', $this->plugin_url . 'assets/css/image4io.css' );
    20         wp_enqueue_style('image4io_plugin_card',$this->plugin_url . 'assets/css/card.css');
    21         $assetPath=array('image4io_static_images'=>"$this->plugin_url" .'assets/img/' );
    22         wp_enqueue_script( 'image4io_script', $this->plugin_url . 'assets/js/image4io.js' );
    23         wp_localize_script( 'image4io_script', 'assetPath', $assetPath );
     19        //wp_enqueue_style( 'image4io_plugin_style', $this->plugin_url . 'assets/css/image4io.css' );
     20        //wp_enqueue_style('image4io_plugin_card',$this->plugin_url . 'assets/css/card.css');
     21        //$assetPath=array('image4io_static_images'=>"$this->plugin_url" .'assets/img/' );
     22        //wp_enqueue_script( 'image4io_script', $this->plugin_url . 'assets/js/image4io.js' );
     23        //wp_localize_script( 'image4io_script', 'assetPath', $assetPath );
     24        wp_enqueue_script( 'thickbox' );
     25        wp_enqueue_style('thickbox');
    2426    }
    2527}
  • image4io/trunk/inc/Manager/MediaManager.php

    r2253449 r2261008  
    1010
    1111 class MediaManager extends BaseController{
    12 
     12     
    1313    public function register(){
    1414        $this->hookUI();
     
    1616
    1717    public function hookUI(){
    18         $this->mediaButtonHook();
    19        
    20     }
    21 
    22     public function mediaButtonHook(){
    23         add_action("init",array($this,"init_image4io_shortcode"));
    24         add_action('media_buttons', array($this, 'mediaImage4io'), 11);
     18        $this->mediaHook();
     19        //$this->mceButtonHook();
     20    }
     21
     22    public function mceButtonHook(){
     23        add_action('admin_enqueue_scripts', array($this,'mceButtonEnqueue'));
     24    }
     25
     26    public function mceButtonEnqueue(){
     27        wp_enqueue_script( 'image4io_mce_button_script', $this->plugin_url . 'assets/js/image4io-mce-button.js');
     28        $args=array('plugin_url'=> $this->plugin_url);
     29        wp_localize_script( 'image4io_mce_button_script', 'args', $args );
     30    }
     31
     32    public function mediaHook(){
     33        //add_action("init",array($this,"init_image4io_shortcode"));
     34       
     35        add_filter('manage_media_columns', array($this, 'add_image4io_media_column'));
     36        add_action('manage_media_custom_column', array($this, 'image4io_media_column_value'), 0, 2);
     37        add_action('admin_footer-upload.php', array($this, 'image4io_media_lib_upload_admin_footer'));
     38        add_action('load-upload.php', array($this, 'image4io_media_lib_upload_action'));
     39        add_filter('wp_get_attachment_url', array($this, 'fix_local_url_to_image4io'), 1, 2);
     40        add_filter('image_downsize', array($this, 'image4io_resize'), 1, 3);
     41        add_filter('the_content',array($this,'image4io_make_content_responsive'));
     42        add_action('admin_notices',array($this,'image4io_upload_notices'));
    2543        add_action('admin_enqueue_scripts', array($this,'mediaButtonEnqueue'));
    26         add_action('wp_ajax_image4io_image_selected',array($this,'imageSelected'));
    27         add_action('wp_ajax_image4io_model',array($this, 'getImagesByFolder'));
     44       
     45        //add_action('media_buttons', array($this, 'mediaImage4io'), 11);
     46        //add_action('wp_ajax_image4io_image_selected',array($this,'imageSelected'));
     47        //add_action('wp_ajax_image4io_model',array($this, 'getImagesByFolder'));
     48
     49        add_filter( 'http_request_timeout', array($this, 'image4io_timeout_extend'));
    2850    }
    2951
     
    3153        add_shortcode( "image4io", array($this,"image4io_shortcode"));
    3254    }
     55
    3356    public static function image4io_shortcode($atts){
    3457        $defaultSizes=$this->get_wp_sizes();
     
    6891        //return $cloudname;
    6992        if(!isset($cloudname)||$cloudname==""){
    70             return;
     93            return false;
    7194        }
    7295
     
    78101            return "https://cdn.image4.io/" . $cloudname . "/f_auto,c_fit,h_" . $height . $src;
    79102        }
     103    }
     104
     105    public function createImage4ioUrlWithTransformation($src,$transformation){
     106        $options=get_option("image4io_settings");
     107        $cloudname=$options['cloudname'];
     108        //return $cloudname;
     109        if(!isset($cloudname)||$cloudname==""){
     110            return false;
     111        }
     112        return "https://cdn.image4.io/" . $cloudname . "/" .$transformation . $src;
    80113    }
    81114
     
    125158    }
    126159
    127     public function get_wp_sizes()
    128     {
     160    public function get_wp_sizes(){
    129161        if (isset($this->sizes)) {
    130162            return $this->sizes;
     
    160192        return $this->sizes;
    161193    }
     194
     195    public function add_image4io_media_column($cols){
     196        $cols['image4io_column'] = 'Image4io';
     197        return $cols;
     198    }
     199
     200    public function image4io_media_column_value($column_name, $attachment_id){
     201        if ('image4io_column' == $column_name) {
     202            $metadata = wp_get_attachment_metadata($attachment_id);
     203            $isImage4io=isset($metadata['image4io'])?$metadata['image4io']:null;
     204            if(is_array($metadata)&&$isImage4io){ //<img src='$src' style='vertical-align: middle;' width='24' height='24'/>
     205                echo "<div style='line-height: 24px;'>Uploaded</div>";
     206                $action_url = wp_nonce_url('?', 'image4io-media');
     207                echo "<a href='$action_url&image4io_upload=$attachment_id&image4io_undo=1'>Undo</a>";
     208            }else{
     209                $action_url = wp_nonce_url('?', 'image4io-media');
     210                echo "<a href='$action_url&image4io_upload=$attachment_id'>Upload to Image4io</a>";
     211            }
     212        }
     213    }
     214
     215    public function image4io_media_lib_upload_admin_footer(){
     216        $loader=$this->plugin_url . "assets/img/ajax-loader.gif";
     217        require_once("$this->plugin_path/templates/loading.php");
     218        ?>
     219        <script type="text/javascript">
     220            jQuery(function($) {
     221                $(document).ready(function(){
     222                    add_bulk_option();
     223                });
     224
     225                function add_bulk_option(){
     226                    var buttons=$("select[name='action'],select[name='action2']");
     227                    if(buttons.length>0){
     228                        buttons.each(function() {
     229                            $('<option>').val('image4io_upload').text('Upload to Image4io').appendTo(this);
     230                        });
     231                    }else{
     232                        setTimeout(add_bulk_option, 10);
     233                    }
     234                }
     235
     236                function show_loader(){
     237                    tb_show('','#TB_inline?height=100&width=150&inlineId=loadingModal&modal=true');
     238                }
     239            });
     240        </script>
     241        <?php
     242       
     243    }
     244   
     245    public function image4io_upload_animation_show(){
     246        ?>
     247        <script type="text/javascript">
     248            jQuery(document).ready(function() {
     249                tb_show('','#TB_inline?height=100&width=150&inlineId=loadingModal&modal=true');
     250            });
     251        </script>
     252        <?php
     253    }
     254
     255    public function image4io_upload_animation_remove(){
     256        ?>
     257        <script type="text/javascript">
     258            jQuery(document).ready(function() {
     259                console.log('remove')
     260                tb_remove();
     261            });
     262        </script>
     263        <?php
     264    }
     265
     266    public function image4io_media_lib_upload_action(){
     267        $sendback = wp_get_referer();
     268
     269        global $pagenow;
     270        if ('upload.php' == $pagenow && isset($_REQUEST['image4io_upload']) && (int) $_REQUEST['image4io_upload']) {
     271           
     272            check_admin_referer('image4io-media');
     273            $result=null;
     274            if(isset($_REQUEST['image4io_undo'])&&$_REQUEST['image4io_undo']==1){
     275                $result=$this->undo_image4io($_REQUEST['image4io_upload']);
     276                if($result){
     277                    $message=$result;
     278                    $sendback=add_query_arg(array('image4io_message'=>urlencode($message),'image4io_error'=>true),$sendback);
     279                }else{
     280                    $message="Media is loaded to Wordpress.";
     281                    $sendback=add_query_arg(array('image4io_message'=>urlencode($message)),$sendback);
     282                }
     283                //return to media library
     284                wp_redirect($sendback);
     285                exit();
     286            }
     287
     288            //upload to image4io
     289            $result=$this->upload_to_image4io($_REQUEST['image4io_upload']);
     290            //setup message
     291            if($result){
     292                $message=$result;
     293                $sendback=add_query_arg(array('image4io_message'=>urlencode($message),'image4io_error'=>true),$sendback);
     294            }else{
     295                $message="Media is uploaded to image4io.";
     296                $sendback=add_query_arg(array('image4io_message'=>urlencode($message)),$sendback);
     297            }
     298            //return to media library
     299            wp_redirect($sendback);
     300            exit();
     301        }
     302       
     303        //bulk action
     304        $wp_list_table = _get_list_table('WP_Media_List_Table');
     305        $action = $wp_list_table->current_action();
     306        if($action=='image4io_upload'){
     307
     308            check_admin_referer('bulk-media');
     309
     310            $post_ids = array();
     311            if (isset($_REQUEST['media'])) {
     312                $post_ids = $_REQUEST['media'];
     313            } elseif (isset($_REQUEST['ids'])) {
     314                $post_ids = explode(',', $_REQUEST['ids']);
     315            }
     316            $results=array();
     317            foreach($post_ids as $k=>$post_id){
     318                $res=$this->upload_to_image4io($post_id);
     319                if($res){
     320                    $results[$post_id]=$res;
     321                }
     322            }
     323            $message="";
     324            if(count($results)>0){
     325                foreach($results as $id=>$result){
     326                    $message .= " Filename: " . $id . "; Error: " . $result . "\n";
     327                }
     328                $message=rtrim($message,'\n');
     329                $message= "There are some errors while uploading to image4io.\n" . $message;
     330                $sendback=add_query_arg( array('image4io_message'=>urlencode($message),'image4io_error'=>true),$sendback );
     331            }else{
     332                $message="All selected images have uploaded to image4io successfully.";
     333                $sendback=add_query_arg(array('image4io_message'=>urlencode($message)),$sendback);
     334            }
     335            wp_redirect($sendback);
     336            exit();
     337        }
     338    }
     339
     340    public function undo_image4io($attachment_id){
     341        $md = wp_get_attachment_metadata($attachment_id);
     342        if(!$md['image4io']){
     343            return 'Already loaded to Wordpress';
     344        }
     345
     346        $attachment = get_post($attachment_id);
     347        $old_url = wp_get_attachment_url($attachment_id);
     348
     349        $error=$this->unregister_image($attachment_id,$attachment,$old_url,$md['original_url']);
     350        if($error){
     351            return $error;
     352        }
     353
     354        $this->update_image_src_all($attachment_id, $old_url, $md['original_url']);
     355    }
     356
     357    public function upload_to_image4io($attachment_id){
     358        $md = wp_get_attachment_metadata($attachment_id);
     359        if(isset($md['image4io'])&&$md['image4io']){
     360            return 'Already uploaded to Image4io';
     361        }
     362
     363        $attachment = get_post($attachment_id);
     364
     365        $mime_type=$attachment->post_mime_type;
     366        if(!preg_match( '!^image/!', $mime_type )){
     367            return 'Unsupported file type: ' . $mime_type ;
     368        }
     369
     370        $full_path = $attachment->guid;
     371        if (empty($full_path)) {
     372            return 'Unsupported attachment type!';
     373        }
     374
     375        $manager = new Image4IOManager;
     376        $manager->setup();
     377        $result = $manager->uploadToImage4ioFromUrl($full_path,"/");
     378       
     379        if(!isset($result->fetchedFile)){
     380            return "Cannot upload to image4io server! File: " . $attachment->$post_title;
     381        }
     382        $name=$result->fetchedFile->name;
     383        $url= $this->build_url_from_name($name);
     384
     385        $old_url = wp_get_attachment_url($attachment_id);
     386       
     387        $res= $this->register_image($name, $url, $attachment->post_parent, $attachment_id, $attachment);
     388        if(!$res){
     389            return "Cannot register image! File: " . $attachment->$post_title;
     390        }
     391        $this->update_image_src_all($attachment_id, $old_url, $url);
     392        return null;
     393    }
     394
     395    public function register_image($name, $url, $post_id, $attachment_id, $original_attachment){
     396        $info = pathinfo($url);
     397        $public_id = $info['filename'];
     398        $mime_types = array('png' => 'image/png', 'jpg' => 'image/jpeg', 'bmp' => 'image/bmp');
     399        $type = $mime_types[$info['extension']];
     400       
     401        $md = wp_get_attachment_metadata($attachment_id);
     402        $meta = $md['image_meta'];
     403        $title = $original_attachment->post_title;
     404        $caption = $original_attachment->post_content;
     405       
     406        $sizes=$md['sizes'];
     407        $image4io_sizes=array();
     408        foreach($sizes as $key=>$size){
     409            $i_size=array(
     410                'file'=>$this->createImage4ioUrl($name,$size['width'],0),
     411                'width'=>(int)$size['width'],
     412                'height'=>(int)$size['height'],
     413                'mime-type'=>$size['mime-type']
     414            );
     415            $image4io_sizes[$key]=$i_size;
     416        }
     417
     418        if (!$title) {
     419            $title = $public_id;
     420        }
     421        if (!$caption) {
     422            $caption = '';
     423        }
     424        if (!$meta) {
     425            $meta = array(
     426                  'aperture' => 0,
     427                  'credit' => '',
     428                  'camera' => '',
     429                  'caption' => $caption,
     430                  'created_timestamp' => 0,
     431                  'copyright' => '',
     432                  'focal_length' => 0,
     433                  'iso' => 0,
     434                  'shutter_speed' => 0,
     435                  'title' => $title, );
     436        }
     437
     438        $attachment = array(
     439            'post_mime_type' => $type,
     440            'guid' => $url,
     441            'post_parent' => $post_id,
     442            'post_title' => $title,
     443            'post_content' => $caption, );
     444        if ($attachment_id && is_numeric($attachment_id)) {
     445            $attachment['ID'] = intval($attachment_id);
     446        }
     447        // Save the data
     448        $id = wp_insert_attachment($attachment, $url, $post_id);
     449        if (!is_wp_error($id)) {
     450            $md['image4io']=true;
     451            $md['image4io_name']=$name;
     452            $md['image4io_sizes']=$image4io_sizes;
     453            $md['original_url']=$original_attachment->guid;
     454            wp_update_attachment_metadata($id, $md);
     455        }
     456       
     457        return $id;
     458    }
     459
     460    public function unregister_image($attachment_id, $attachment, $old_url, $new_url){
     461        $new_attachment = array(
     462            'ID'=>$attachment_id,
     463            'post_mime_type' => $attachment->post_mime_type,
     464            'guid' => $new_url,
     465            'post_parent' => $attachment->post_parent,
     466            'post_title' => $attachment->post_title,
     467            'post_content' => $attachment->post_content,
     468        );
     469        $id = wp_insert_attachment($new_attachment, $new_url, $attachment->post_parent);
     470        if (!is_wp_error($id)) {
     471            $md = wp_get_attachment_metadata($attachment_id);
     472            $md['image4io']=false;
     473            $md['image4io_name']="";
     474            $md['image4io_sizes']=array();
     475            $md['original_url']="";
     476            wp_update_attachment_metadata($id, $md);
     477            return null;
     478        }else{
     479            return "Cannot register image";
     480        }
     481    }
     482
     483    public function update_image_src_all($attachment_id,  $old_url, $new_url){
     484        $query = new \WP_Query(
     485            array(
     486                'post_type' => 'any',
     487                'post_status' => 'publish,pending,draft,auto-draft,future,private',
     488                's' => "wp-image-{$attachment_id}",
     489            )
     490        );
     491
     492        while ($query->have_posts()) {
     493            $query->the_post();
     494            $this->update_image_src($query->post, $attachment_id, $old_url, $new_url);
     495        }
     496    }
     497
     498    public function update_image_src($post, $attachment_id, $old_url, $new_url){
     499        $sizes=$this->get_wp_sizes();
     500        $post_content = $post->post_content;
     501        $metadata = wp_get_attachment_metadata($attachment_id);
     502        preg_match_all('/<img [^>]+>/', $post->post_content, $images);
     503        foreach ($images[0] as $img) {
     504            if (preg_match('/class *= *["\']([^"\']+)["\']/i', $img, $class) && preg_match('/wp-image-([0-9]+)/i', $class[1], $id) && $id[1] == $attachment_id) {
     505                $wanted_size = null;
     506                if (preg_match('/size-([a-zA-Z0-9_\-]+)/i', $class[1], $size)) {
     507                    if (isset($sizes[$size[1]])) {
     508                        $wanted_size = $size[1];
     509                    } elseif ('full' == $size[1]) {
     510                        // default url requested
     511                    } else {
     512                        // Unknown transformation.
     513                        // TODO build url from width and height values
     514                        continue;
     515                    }
     516                }
     517                if (preg_match('/src *= *["\']([^"\']+)["\']/i', $img, $src)) {
     518                   
     519                    // Migrate In
     520                    if(preg_match('/^.*?cdn\.image4.*/i',$new_url)>=1){
     521                        list($new_img_src) = $this->build_resize_url($new_url, $metadata , $wanted_size);
     522                        if ($new_img_src) {
     523                            $post_content = str_replace($src[1], $new_img_src, $post_content);
     524                        }
     525                    //Migrate Out
     526                    }elseif(preg_match('/^.*?cdn\.image4.*/i',$old_url)>=1){
     527                        if(!$wanted_size){
     528                            $post_content=str_replace($src[1], $new_url, $post_content);
     529                        }
     530                        list($new_img_src)=image_downsize( $attachment_id, $wanted_size );
     531                        if ($new_img_src) {
     532                            $post_content = str_replace($src[1], $new_img_src, $post_content);
     533                        }
     534                    }
     535                }
     536            }
     537            // Also replace original link with new link, for hrefs
     538            $post_content = str_replace($old_url, $new_url, $post_content);
     539        }
     540        if ($post_content != $post->post_content) {
     541            return wp_update_post(array('post_content' => $post_content, 'ID' => $post->ID));
     542        }
     543
     544        return false;
     545    }
     546
     547    public function build_url_from_name($name){
     548        $values=get_option( "image4io_settings" );
     549        if(!isset($values)||!isset($values["cloudname"])){
     550            return null;
     551        }
     552        return "https://cdn.image4.io/" . $values["cloudname"] . $name;
     553    }
     554
     555    public function fix_local_url_to_image4io($url,$post_id){
     556        $metadata = wp_get_attachment_metadata($post_id);
     557        if (isset($metadata['image4io']) && $metadata['image4io'] && preg_match('#^.*?/(https?://.*)#', $url, $matches)) {
     558            return $matches[1];
     559        }
     560        return $url;
     561    }
     562   
     563    public function image4io_resize($is_downsize, $post_id, $size){
     564        $url = wp_get_attachment_url($post_id);
     565        $metadata = wp_get_attachment_metadata($post_id);
     566        if (!isset($metadata['image4io']) || !$metadata['image4io']) {
     567            return;
     568        }
     569
     570        return $this->build_resize_url($url, $metadata, $size);
     571    }
     572
     573    public function build_resize_url($url, $metadata, $size){
     574        if (!$size) {
     575            return array($url, $metadata['width'], $metadata['height'], false);
     576        }
     577       
     578        if (is_string($size)) {
     579            $available_sizes = $this->get_wp_sizes();
     580           
     581            if (!array_key_exists($size, $available_sizes)) {
     582                return array($url, $metadata['width'], $metadata['height'], false);
     583            }
     584
     585            $wanted_size = $available_sizes[$size];
     586            $crop = $wanted_size['crop'];
     587        } elseif (is_array($size)) {
     588            $wanted_size = array('width' => $size[0], 'height' => $size[1]);
     589            $crop = false;
     590        } else {
     591            // Unsupported argument
     592            return false;
     593        }
     594        //TODO
     595        /*
     596        $transformation = '';
     597        $src_w = $dst_w = $metadata['width'];
     598        $src_h = $dst_h = $metadata['height'];
     599        if ($crop) {
     600            $resized = image_resize_dimensions($metadata['width'], $metadata['height'], $wanted_size['width'], $wanted_size['height'], true);
     601            if ($resized) {
     602                list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $resized;
     603                $transformation = "f_auto,c_crop,h_$src_h,w_$src_w,x_$src_x,y_$src_y/";
     604
     605
     606
     607                return $this->createImage4ioUrlWithTransformation()
     608            }
     609           
     610        }*/
     611
     612        //TODO After chain manuplation
     613        /*list($width, $height) = image_constrain_size_for_editor($dst_w, $dst_h, $size);
     614        if ($width != $src_w || $height != $src_h) {
     615            $transformation = $transformation."h_$height,w_$width/";
     616        }*/
     617        //preg_match('/^.+?[^\/:](?=[?\/]|$)/', $input_line, $output_array);
     618        //$url="https://cdn.image4.io/i4io/f_auto,h_450,w_900/692df81d-227f-46d5-aed3-5ec2ff76543a.png";
     619        $parsed_url=explode('/',$url);
     620        $result_url="";
     621        foreach($parsed_url as $idx=>$part){
     622            if($idx==4){
     623                //replace with new one
     624                $result_url=$result_url . "f_auto,c_fit,w_" . $wanted_size['width'] . "/";
     625                if(preg_match("((\w)_([0-9a-zA-Z]),?)",$part)){
     626                   
     627                   continue;
     628                }
     629            }
     630            $result_url=$result_url . $part . '/';
     631        }
     632        return array($result_url, $wanted_size['width'], $wanted_size['height'], true);
     633    }
     634
     635    public function image4io_generate_image_srcset($image_src, $image_meta, $attachment_id){
     636        /*if (!isset($image_meta['image4io']) || !$image_meta['image4io']){
     637            return $sources;
     638        }*/
     639        $sizes=$image_meta['image4io_sizes'];
     640       
     641        $results=array();
     642        foreach($sizes as $size){
     643            $result=array(
     644                'url'=>$size['file'],
     645                'descriptor'=>'w',
     646                'value'=>$size['width'],
     647            );
     648            $results[$size['width']]=$result;
     649        }
     650        //unset($size);
     651       
     652        if ( ! is_array( $results ) || count( $results ) < 1 ) {
     653            return false;
     654        }
     655
     656        $srcset = '';
     657       
     658        foreach ( $results as $result ) {
     659            $srcset .= str_replace( ' ', '%20', $result['url'] ) . ' ' . $result['value'] . $result['descriptor'] . ', ';
     660        }
     661        $srcset = rtrim($srcset, ', ');
     662        $srcset_sizes= wp_calculate_image_sizes( array($image_meta["width"],$image_meta["height"]), $image_src, $image_meta, $attachment_id );
     663       
     664        preg_match('/(^.*?src=\"[^"]+")(.*)/i',$image_src,$res);
     665       
     666        return $res[1] . ' srcset="' . $srcset . '" sizes="' . $srcset_sizes . '"' . $res[2];
     667    }
     668
     669    public function image4io_make_content_responsive($content){
     670        if ( ! preg_match_all( '/<img [^>]+>/', $content, $matches ) ) {
     671            return $content;
     672        }
     673        $selected_images = array();
     674        $attachment_ids  = array();
     675        foreach ( $matches[0] as $image ) {
     676            if ( false === strpos( $image, ' srcset=' ) && preg_match('/^.*?cdn\.image4.*/i',$image)>=1 && preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) ) {
     677                $attachment_id = absint( $class_id[1] );
     678   
     679                if ( $attachment_id ) {
     680                    /*
     681                     * If exactly the same image tag is used more than once, overwrite it.
     682                     * All identical tags will be replaced later with 'str_replace()'.
     683                     */
     684                    $selected_images[ $image ] = $attachment_id;
     685                    // Overwrite the ID when the same image is included more than once.
     686                    $attachment_ids[ $attachment_id ] = true;
     687
     688                }
     689            }
     690        }
     691        if ( count( $attachment_ids ) > 1 ) {
     692            /*
     693             * Warm the object cache with post and meta information for all found
     694             * images to avoid making individual database calls.
     695             */
     696            _prime_post_caches( array_keys( $attachment_ids ), false, true );
     697        }
     698       
     699        foreach ( $selected_images as $image => $attachment_id ) {
     700            $image_meta = wp_get_attachment_metadata( $attachment_id );
     701            $content    = str_replace( $image, $this->image4io_generate_image_srcset( $image, $image_meta, $attachment_id ), $content );
     702        }
     703   
     704        return $content;
     705    }
     706
     707    public function image4io_timeout_extend( $time){
     708        return 30;
     709    }
     710
     711    public function image4io_upload_notices(){
     712        global $post_type, $pagenow;
     713       
     714        if ('upload.php' == $pagenow && isset($_REQUEST['image4io_message'])) {
     715            if(isset($_REQUEST['image4io_error'])&&$_REQUEST['image4io_error']){
     716                $message = htmlentities($_REQUEST['image4io_message'], ENT_NOQUOTES);
     717                echo "<div class='error notice is-dismissible'><p>{$message}</p></div>";
     718                return;
     719            }else{
     720                $message = htmlentities($_REQUEST['image4io_message'], ENT_NOQUOTES);
     721                echo "<div class='updated notice is-dismissible'><p>{$message}</p></div>";
     722                return;
     723            }
     724        }
     725    }
    162726 }
    163 
    164 
    165 
  • image4io/trunk/inc/Pages/Admin.php

    r2253449 r2261008  
    106106                    'label_for' => 'cloudname'
    107107                )
    108             ),
    109             array(
    110                 'id' => 'folder',
    111                 'title' => 'Folder',
    112                 'callback' => array( $this->callbacks, 'image4ioFolder' ),
    113                 'page' => 'image4io_plugin',
    114                 'section' => 'image4io_admin_dashboard_index',
    115                 'args' => array(
    116                     'option_name'=>'image4io_settings',
    117                     'label_for' => 'folder'
    118                 )
    119108            )
    120109        );
  • image4io/trunk/package.json

    r2253449 r2261008  
    11{
    22  "name": "image4io",
    3   "version": "0.1.0",
     3  "version": "0.2.0",
    44  "description": "image4io Wordpress Plugin. Speeds up the images' load time: image optimization, image CDN and image storage, all-in-one platform.",
    55  "main": "index.js",
Note: See TracChangeset for help on using the changeset viewer.