- File:
-
- 1 edited
-
pixabay-images/trunk/pixabay-images.php (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
pixabay-images/trunk/pixabay-images.php
r926633 r1067992 1 <? 1 <?php 2 2 3 3 /* … … 5 5 Plugin URI: http://pixabay.com/blog/posts/p-36/ 6 6 Description: Find quality public domain images from Pixabay and upload them with just one click. 7 Version: 2. 07 Version: 2.4 8 8 Author: Simon Steinberger 9 9 Author URI: http://pixabay.com/users/Simon/ … … 13 13 14 14 // i18n 15 function pixabay_images_load_textdomain() { load_plugin_textdomain('pixabay_images', false, dirname(plugin_basename(__FILE__ )).'/langs/'); } 15 16 add_action('plugins_loaded', 'pixabay_images_load_textdomain'); 16 function pixabay_images_load_textdomain() {17 load_plugin_textdomain('pixabay_images', false, dirname(plugin_basename(__FILE__ )).'/langs/');18 }19 17 20 18 … … 24 22 25 23 // add tab to media upload window 26 add_filter('media_upload_tabs', function($tabs){ $tabs['pixabaytab'] = __('Pixabay Images', 'pixabay_images'); return $tabs; }); 24 function media_upload_tabs_handler($tabs) { $tabs['pixabaytab'] = __('Pixabay Images', 'pixabay_images'); return $tabs; } 25 add_filter('media_upload_tabs', 'media_upload_tabs_handler'); 27 26 28 27 29 28 // add button next to "Add Media" 30 29 $pixabay_images_settings = get_option('pixabay_images_options'); 31 if (!$pixabay_images_settings['button'] | $pixabay_images_settings['button']=='true') 32 add_filter('media_buttons_context', function($editor_id=''){ return '<a href="'.add_query_arg('tab', 'pixabaytab', esc_url(get_upload_iframe_src())).'" id="'.esc_attr($editor_id).'-add_media" class="thickbox button" title="'.esc_attr__('Pixabay Images', 'pixabay_images').'"><img style="position:relative;top:-1px" src="'.plugin_dir_url(__FILE__).'favicon.ico'.'"> Pixabay</a>'; }); 30 if (!$pixabay_images_settings['button'] | $pixabay_images_settings['button']=='true') { 31 function media_buttons_context_handler($editor_id='') { return '<a href="'.add_query_arg('tab', 'pixabaytab', esc_url(get_upload_iframe_src())).'" id="'.esc_attr($editor_id).'-add_media" class="thickbox button" title="'.esc_attr__('Pixabay Images', 'pixabay_images').'"><img style="position:relative;top:-2px" src="'.plugin_dir_url(__FILE__).'favicon.png'.'"> Pixabay</a>'; } 32 add_filter('media_buttons_context', 'media_buttons_context_handler'); 33 } 33 34 34 35 … … 60 61 <p><input id="q" type="text" value="" style="width:100%;max-width:500px;padding:7px 9px"></p> 61 62 <p> 62 <label style="margin-right:15px"><input type="checkbox" id="filter_photos"<?= $pixabay_images_settings['image_type']=='clipart'?'':' checked="checked"'; ?>><? _e('Photos', 'pixabay_images');?></label>63 <label style="margin-right:20px"><input type="checkbox" id="filter_cliparts"<?= $pixabay_images_settings['image_type']=='photo'?'':' checked="checked"'; ?>><? _e('Cliparts', 'pixabay_images');?></label>63 <label style="margin-right:15px"><input type="checkbox" id="filter_photos"<?= $pixabay_images_settings['image_type']=='clipart'?'':' checked="checked"'; ?>><?= _e('Photos', 'pixabay_images'); ?></label> 64 <label style="margin-right:20px"><input type="checkbox" id="filter_cliparts"<?= $pixabay_images_settings['image_type']=='photo'?'':' checked="checked"'; ?>><?= _e('Cliparts', 'pixabay_images'); ?></label> 64 65 <span style="margin-right:20px">|</span> 65 <label style="margin-right:15px"><input type="checkbox" id="filter_horizontal"<?= $pixabay_images_settings['orientation']=='vertical'?'':' checked="checked"'; ?>><? _e('Horizontal', 'pixabay_images');?></label>66 <label style="margin-right:25px"><input type="checkbox" id="filter_vertical"<?= $pixabay_images_settings['orientation']=='horizontal'?'':' checked="checked"'; ?>><? _e('Vertical', 'pixabay_images');?></label>67 <a href="options-general.php?page=pixabay_images_settings" target="_blank"><? _e('Settings', 'pixabay_images');?></a>66 <label style="margin-right:15px"><input type="checkbox" id="filter_horizontal"<?= $pixabay_images_settings['orientation']=='vertical'?'':' checked="checked"'; ?>><?= _e('Horizontal', 'pixabay_images'); ?></label> 67 <label style="margin-right:25px"><input type="checkbox" id="filter_vertical"<?= $pixabay_images_settings['orientation']=='horizontal'?'':' checked="checked"'; ?>><?= _e('Vertical', 'pixabay_images'); ?></label> 68 <a href="options-general.php?page=pixabay_images_settings" target="_blank"><?= _e('Settings', 'pixabay_images'); ?></a> 68 69 </p> 69 <input id="submit_search" class="button" type="submit" value="<? _e('Search', 'pixabay_images');?>">70 <input id="submit_search" class="button" type="submit" value="<?= _e('Search', 'pixabay_images'); ?>"> 70 71 </form> 71 72 <div id="pixabay_results" style="margin-top:25px;padding-top:25px;border-top:1px solid #ddd"></div> … … 73 74 <script> 74 75 function crossDomainAjax(c,a){if("XDomainRequest" in window&&window.XDomainRequest!==null){var b=new XDomainRequest();b.open("get",c);b.onload=function(){var e=new ActiveXObject("Microsoft.XMLDOM"),d=$.parseJSON(b.responseText);e.async=false;if(d==null||typeof(d)=="undefined"){d=$.parseJSON(data.firstChild.textContent)}a(d)};b.onerror=function(){_result=false};b.send()}else{if(navigator.userAgent.indexOf('MSIE')!=-1&&parseInt(navigator.userAgent.match(/MSIE ([\d.]+)/)[1])<8){return false}else{$.ajax({url:c,cache:false,dataType:"json",type:"GET",async:false,success:function(d,e){a(d)}})}}}; 75 function escape JS(s){return s.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"').replace(/'/g,"\\'");}76 function escapejs(s){return s.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"').replace(/'/g,"\\'");} 76 77 // hoverIntent r7 77 78 (function(e){e.fn.hoverIntent=function(t,n,r){var i={interval:100,sensitivity:7,timeout:0};if(typeof t==="object"){i=e.extend(i,t)}else if(e.isFunction(n)){i=e.extend(i,{over:t,out:n,selector:r})}else{i=e.extend(i,{over:t,out:t,selector:n})}var s,o,u,a;var f=function(e){s=e.pageX;o=e.pageY};var l=function(t,n){n.hoverIntent_t=clearTimeout(n.hoverIntent_t);if(Math.abs(u-s)+Math.abs(a-o)<i.sensitivity){e(n).off("mousemove.hoverIntent",f);n.hoverIntent_s=1;return i.over.apply(n,[t])}else{u=s;a=o;n.hoverIntent_t=setTimeout(function(){l(t,n)},i.interval)}};var c=function(e,t){t.hoverIntent_t=clearTimeout(t.hoverIntent_t);t.hoverIntent_s=0;return i.out.apply(t,[e])};var h=function(t){var n=jQuery.extend({},t);var r=this;if(r.hoverIntent_t){r.hoverIntent_t=clearTimeout(r.hoverIntent_t)}if(t.type=="mouseenter"){u=n.pageX;a=n.pageY;e(r).on("mousemove.hoverIntent",f);if(r.hoverIntent_s!=1){r.hoverIntent_t=setTimeout(function(){l(n,r)},i.interval)}}else{e(r).off("mousemove.hoverIntent",f);if(r.hoverIntent_s==1){r.hoverIntent_t=setTimeout(function(){c(n,r)},i.timeout)}}};return this.on({"mouseenter.hoverIntent":h,"mouseleave.hoverIntent":h},i.selector)}})(jQuery) 78 79 79 var $=jQuery, post_id=<?=absint($_REQUEST['post_id']) ?>,80 var $=jQuery, post_id=<?=absint($_REQUEST['post_id']) ?>, 80 81 lang='<?= $pixabay_images_settings['language']?$pixabay_images_settings['language']:substr(get_locale(), 0, 2) ?>', 81 per_page=<?=$pixabay_images_settings['per_page']?$pixabay_images_settings['per_page']:30 ?>,82 per_page=<?=$pixabay_images_settings['per_page']?$pixabay_images_settings['per_page']:30 ?>, 82 83 form = $('#pixabay_images_form'), hits, cache, resizeTimer, q, image_type, orientation; 83 84 … … 124 125 s += '<span class="button disabled">Prev</span>'; 125 126 else 126 s += '<a href="#" onclick="return call_api(\''+escape JS(q)+'\', '+(p-1)+');" class="button">Prev</a>';127 s += '<a href="#" onclick="return call_api(\''+escapejs(q)+'\', '+(p-1)+');" class="button">Prev</a>'; 127 128 for (i=1; i < pages+1; i++) { 128 s += '<a href="#" onclick="return call_api(\''+escape JS(q)+'\', '+i+');" class="button'+(p==i?' disabled':'')+'">'+i+'</a>';129 s += '<a href="#" onclick="return call_api(\''+escapejs(q)+'\', '+i+');" class="button'+(p==i?' disabled':'')+'">'+i+'</a>'; 129 130 } 130 131 if (p==pages) 131 s += '<span class="button disabled"> Prev</span>';132 s += '<span class="button disabled">Next</span>'; 132 133 else 133 s += '<a href="#" onclick="return call_api(\''+escape JS(q)+'\', '+(p+1)+');" class="button">Next</a>';134 s += '<a href="#" onclick="return call_api(\''+escapejs(q)+'\', '+(p+1)+');" class="button">Next</a>'; 134 135 s += '</div>'; 135 136 $('#pixabay_results').html(s); … … 166 167 167 168 preview = $('<div data-idx="'+idx+'" class="preview">\ 168 <div title="<?= _e('Insert image')?>" style="padding:4px 5px 6px;margin:0 0 5px;text-align:left;border-bottom:1px solid #ddd">\169 <div title="<?= _e('Insert image') ?>" style="padding:4px 5px 6px;margin:0 0 5px;text-align:left;border-bottom:1px solid #ddd">\ 169 170 <a href="#" class="upload 150px" style="margin-right:15px">'+img.width()+' x '+img.height()+'</a>\ 170 171 <a href="#" class="upload 640px" style="margin-right:15px">'+width640+' x '+height640+'</a>\ 171 172 <a href="#" class="upload 1280px">'+width1280+' x '+height1280+'</a>\ 172 173 </div>\ 173 <a title="<?= _e('Insert image')?>" href="#" class="upload 640px" style="display:block;margin:auto;position:relative;width:'+parseInt(width640/2)+'px;height:'+parseInt(height640/2)+'px;">\174 <a title="<?= _e('Insert image') ?>" href="#" class="upload 640px" style="display:block;margin:auto;position:relative;width:'+parseInt(width640/2)+'px;height:'+parseInt(height640/2)+'px;">\ 174 175 <img src="'+img.attr('src')+'" style="width:'+parseInt(width640/2)+'px !important;height:'+parseInt(height640/2)+'px !important;">\ 175 176 <img src="'+hits[idx].webformatURL+'" style="width:'+parseInt(width640/2)+'px !important;height:'+parseInt(height640/2)+'px !important;">\ 176 177 </a>\ 177 <div style="padding:6px 5px 4px;margin:5px 0 0;text-align:left;border-top:1px solid #ddd"><? _e('CC0 Image by', 'pixabay_images');?> <a href="http://pixabay.com/users/'+hits[idx].user+'/" target="_blank">'+hits[idx].user+'</a> / <a href="http://pixabay.com/'+lang+'/photos/?image_type='+image_type+'&orientation='+orientation+'&q='+escapeJS(q)+'" target="_blank">Pixabay</a></div>\178 <div style="padding:6px 5px 4px;margin:5px 0 0;text-align:left;border-top:1px solid #ddd"><?= _e('CC0 Image by', 'pixabay_images'); ?> <a href="http://pixabay.com/users/'+hits[idx].user+'/" target="_blank">'+hits[idx].user+'</a> / <a href="http://pixabay.com/'+lang+'/photos/?image_type='+image_type+'&orientation='+orientation+'&q='+escapejs(q)+'" target="_blank">Pixabay</a></div>\ 178 179 </div>'); 179 180 thumb.append(preview); … … 208 209 }); 209 210 </script> 210 <? 211 <?php 211 212 } 212 add_action('media_upload_pixabaytab', function(){ wp_iframe('media_pixabay_images_tab'); }); 213 function media_upload_pixabaytab_handler() { wp_iframe('media_pixabay_images_tab'); } 214 add_action('media_upload_pixabaytab', 'media_upload_pixabaytab_handler'); 213 215 214 216 … … 217 219 $pixabay_images_settings = get_option('pixabay_images_options'); 218 220 219 # "pluggable.php" is required for current_user_can() and other upload relevant functions221 # "pluggable.php" is required for current_user_can() and other upload relevant functions 220 222 require_once(ABSPATH.'wp-includes/pluggable.php'); 221 if (!is_admin() or !current_user_can('edit_post', $post_id) ) die("You don't have permission to edit this post."); 223 if (!is_user_logged_in() or !current_user_can('edit_post', $post_id)) die("You don't have permission to edit this post."); 224 225 // parse image_url 226 $url = parse_url($_POST['image_url']); 227 if(strcmp($url['host'], "pixabay.com")){ 228 die("Error: wrong host in url (must be pixabay.com)"); 229 } 222 230 223 231 // get image file … … 227 235 $q_tags = explode(' ' , $_POST['q']); 228 236 array_splice($q_tags, 2); 229 foreach ($q_tags as $k=>$v) $q_tags[$k] = trim($v); 237 foreach ($q_tags as $k=>$v) { 238 // Remove ../../../.. 239 $v = str_replace("..","",$v); 240 $q_tags[$k] = trim($v); 241 } 230 242 $path_info = pathinfo($_POST['image_url']); 231 243 $file_name = implode('_', $q_tags).'_'.time().'.'.$path_info['extension']; … … 242 254 $result = @file_put_contents($target_file_name, $response['body']); 243 255 unset($response['body']); 244 if ($result === false) die('Error: Failed to write file - '.$target_file_name); 256 if ($result === false) die('Error: Failed to write file: '.$target_file_name); 257 258 // are we dealing with an image 259 require_once(ABSPATH.'wp-admin/includes/image.php'); 260 if (!wp_read_image_metadata($target_file_name)) { 261 unlink($target_file_name); 262 die('Error: File is not an image.'); 263 } 264 245 265 $image_title = ucwords(implode(', ', $q_tags)); 246 266 $attachment_caption = ''; 247 267 if (!$pixabay_images_settings['attribution'] | $pixabay_images_settings['attribution']=='true') 248 $attachment_caption = '<a href="http://pixabay.com/users/'. $_POST['image_user'].'/">'.$_POST['image_user'].'</a> / Pixabay';268 $attachment_caption = '<a href="http://pixabay.com/users/'.htmlentities($_POST['image_user']).'/">'.htmlentities($_POST['image_user']).'</a> / Pixabay'; 249 269 250 270 // insert attachment … … 259 279 if ($attach_id == 0) die('Error: File attachment error'); 260 280 261 require_once(ABSPATH.'wp-admin/includes/image.php');262 281 $attach_data = wp_generate_attachment_metadata($attach_id, $target_file_name); 263 282 $result = wp_update_attachment_metadata($attach_id, $attach_data);
Note: See TracChangeset
for help on using the changeset viewer.