PluginProbe
Vimeography: Vimeo Video Gallery WordPress Plugin / 0.7
Vimeography: Vimeo Video Gallery WordPress Plugin v0.7
2.4.9 2.4.8 trunk 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 0.5.6 0.5.7 0.6 0.6.1 0.6.2 0.6.3 0.6.4 0.6.5 0.6.6 0.6.7 0.6.8 0.6.8.1 0.6.9 0.6.9.1 0.6.9.2 0.7 0.8 All 103 releases
vimeography / vimeography.php

vimeography.php in Vimeography: Vimeo Video Gallery WordPress Plugin 0.7, at vimeography.php

705 lines 24.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Vimeography
4 Plugin URI: http://vimeography.com
5 Description: Vimeography is the easiest way to set up a custom Vimeo gallery on your site.
6 Version: 0.7
7 Author: Dave Kiss
8 Author URI: http://davekiss.com
9 License: MIT
10 */
11
12 if (!function_exists('json_decode'))
13 wp_die('Vimeography needs the JSON PHP extension.');
14
15 global $wpdb;
16 $wp_upload_dir = wp_upload_dir();
17
18 // Define constants
19 define( 'VIMEOGRAPHY_URL', plugin_dir_url(__FILE__) );
20 define( 'VIMEOGRAPHY_PATH', plugin_dir_path(__FILE__) );
21 define( 'VIMEOGRAPHY_THEME_URL', $wp_upload_dir['baseurl'].'/vimeography-themes/' );
22 define( 'VIMEOGRAPHY_THEME_PATH', $wp_upload_dir['basedir'].'/vimeography-themes/' );
23 define( 'VIMEOGRAPHY_ASSETS_URL', $wp_upload_dir['baseurl'].'/vimeography-assets/' );
24 define( 'VIMEOGRAPHY_ASSETS_PATH', $wp_upload_dir['basedir'].'/vimeography-assets/' );
25 define( 'VIMEOGRAPHY_BASENAME', plugin_basename( __FILE__ ) );
26 define( 'VIMEOGRAPHY_VERSION', '0.7');
27 define( 'VIMEOGRAPHY_GALLERY_TABLE', $wpdb->prefix . "vimeography_gallery");
28 define( 'VIMEOGRAPHY_GALLERY_META_TABLE', $wpdb->prefix . "vimeography_gallery_meta");
29 define( 'VIMEOGRAPHY_CURRENT_PAGE', basename($_SERVER['PHP_SELF']));
30
31 require_once(VIMEOGRAPHY_PATH . '/vendor/mustache/Mustache.php');
32
33 class Vimeography
34 {
35 public function __construct()
36 {
37 add_action( 'init', array(&$this, 'vimeography_init') );
38 add_action( 'admin_init', array(&$this, 'vimeography_requires_wordpress_version') );
39 add_action( 'admin_init', array(&$this, 'vimeography_check_if_db_exists') );
40 add_action( 'init', array(&$this, 'vimeography_move_folders') );
41 add_action( 'plugins_loaded', array(&$this, 'vimeography_update_db_to_0_6') );
42 add_action( 'plugins_loaded', array(&$this, 'vimeography_update_db_to_0_7') );
43 add_action( 'plugins_loaded', array(&$this, 'vimeography_update_db_version') );
44 add_action( 'admin_menu', array(&$this, 'vimeography_add_menu') );
45 add_action( 'do_robots', array(&$this, 'vimeography_block_robots') );
46
47 register_activation_hook( VIMEOGRAPHY_BASENAME, array(&$this, 'vimeography_update_tables') );
48
49 add_filter( 'plugin_action_links', array(&$this, 'vimeography_filter_plugin_actions'), 10, 2 );
50 add_shortcode( 'vimeography', array(&$this, 'vimeography_shortcode') );
51
52 // Add shortcode support for widgets
53 add_filter( 'widget_text', 'do_shortcode' );
54 }
55
56 /**
57 * Runs on every page load.
58 *
59 * @access public
60 * @return void
61 */
62 public function vimeography_init()
63 {
64 if (! wp_script_is('jquery'))
65 {
66 wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js", false, null);
67 wp_enqueue_script('jquery');
68 }
69
70 if (in_array(VIMEOGRAPHY_CURRENT_PAGE, array('post.php', 'page.php', 'page-new.php', 'post-new.php'))){
71 add_action('admin_footer', array(&$this, 'vimeography_add_mce_popup'));
72 }
73
74 if ( get_user_option('rich_editing') == 'true' ) {
75 add_filter( 'mce_external_plugins', array(&$this, 'vimeography_add_editor_plugin' ));
76 add_filter( 'mce_buttons', array(&$this, 'vimeography_register_editor_button') );
77 }
78
79 // Let's check if the user has a custom robots.txt file.
80 if (file_exists(ABSPATH.'/robots.txt'))
81 {
82 // See if our rule already exists inside of it.
83 $robotstxt = file_get_contents(ABSPATH.'/robots.txt');
84 $blocked_theme_path = str_ireplace(site_url(), '', VIMEOGRAPHY_THEME_URL);
85 $blocked_asset_path = str_ireplace(site_url(), '', VIMEOGRAPHY_ASSETS_URL);
86
87 if (strpos($robotstxt, 'Disallow: '.$blocked_theme_path === FALSE))
88 {
89 // Write our rule.
90 $robotstxt .= "\nDisallow: ".$blocked_theme_path."\n";
91 file_put_contents(ABSPATH.'/robots.txt', $robotstxt);
92 }
93 if (strpos($robotstxt, 'Disallow: '.$blocked_asset_path === FALSE))
94 {
95 // Write our rule.
96 $robotstxt .= "\nDisallow: ".$blocked_asset_path."\n";
97 file_put_contents(ABSPATH.'/robots.txt', $robotstxt);
98 }
99 }
100
101 }
102
103 public function vimeography_register_editor_button($buttons)
104 {
105 array_push( $buttons, "|", "vimeography" );
106 return $buttons;
107 }
108
109 public function vimeography_add_editor_plugin( $plugin_array ) {
110 $plugin_array['vimeography'] = VIMEOGRAPHY_URL . 'media/js/mce.js';
111 return $plugin_array;
112 }
113
114 /**
115 * Check the wordpress version is compatible, and disable plugin if not.
116 *
117 * @access public
118 * @return void
119 */
120 public function vimeography_requires_wordpress_version() {
121 global $wp_version;
122 $plugin = plugin_basename( __FILE__ );
123 $plugin_data = get_plugin_data( __FILE__, false );
124
125 if ( version_compare($wp_version, "3.3", "<" ) ) {
126 if( is_plugin_active($plugin) ) {
127 deactivate_plugins( $plugin );
128 wp_die( "'".$plugin_data['Name']."' requires WordPress 3.3 or higher, and has been deactivated! Please upgrade WordPress and try again.<br /><br />Back to <a href='".admin_url()."'>WordPress admin</a>." );
129 }
130 }
131 }
132
133 public function vimeography_check_if_db_exists()
134 {
135 if (get_option('vimeography_db_version') == FALSE)
136 $this->vimeography_update_db_version();
137 }
138
139 /**
140 * Move the defined folders to the defined target path in wp-content/uploads.
141 *
142 * @access public
143 * @return void
144 */
145 public function vimeography_move_folders()
146 {
147 $this->_move_folder(array('source' => VIMEOGRAPHY_PATH . 'bugsauce/', 'destination' => VIMEOGRAPHY_THEME_PATH.'bugsauce/', 'clear_destination' => true, 'clear_working' => true));
148 $this->_move_folder(array('source' => VIMEOGRAPHY_PATH . 'theme-assets/', 'destination' => VIMEOGRAPHY_ASSETS_PATH, 'clear_destination' => true, 'clear_working' => true));
149
150 // Now, check if the .htaccess exists in the VIMEOGRAPHY_THEME_PATH
151 if (file_exists(VIMEOGRAPHY_THEME_PATH.'.htaccess'))
152 {
153 unlink(VIMEOGRAPHY_THEME_PATH.'.htaccess');
154 }
155 // file_put_contents(VIMEOGRAPHY_THEME_PATH.'.htaccess', "Options All -Indexes\n<FilesMatch \".(htaccess|mustache)$\">\nOrder Allow,Deny\nDeny from all\n</FilesMatch>");
156 }
157
158 /**
159 * Check if the Vimeography database structure needs updated to version 0.6 based on the stored db version.
160 *
161 * @access public
162 * @return void
163 */
164 public function vimeography_update_db_to_0_6()
165 {
166 if (get_option('vimeography_db_version') < 0.6)
167 {
168 global $wpdb;
169 $old_galleries = $wpdb->get_results('SELECT * FROM '.VIMEOGRAPHY_GALLERY_META_TABLE.' AS meta JOIN '.VIMEOGRAPHY_GALLERY_TABLE.' AS gallery ON meta.gallery_id = gallery.id;');
170 $new_galleries = array();
171
172 if (is_array($old_galleries))
173 {
174 foreach ($old_galleries as $old_gallery)
175 {
176 $new_gallery = array();
177
178 $new_gallery['gallery_id'] = $old_gallery->gallery_id;
179 $new_gallery['video_limit'] = $old_gallery->video_count;
180 $new_gallery['featured_video'] = $old_gallery->featured_video;
181 $new_gallery['cache_timeout'] = $old_gallery->cache_timeout;
182 $new_gallery['theme_name'] = $old_gallery->theme_name;
183 switch ($old_gallery->source_type)
184 {
185 case 'user':
186 $new_gallery['source_url'] = 'https://vimeo.com/'.$old_gallery->source_name;
187 break;
188 case 'album':
189 $new_gallery['source_url'] = 'https://vimeo.com/album/'.$old_gallery->source_name;
190 break;
191 case 'group':
192 $new_gallery['source_url'] = 'https://vimeo.com/groups/'.$old_gallery->source_name;
193 break;
194 case 'channel':
195 $new_gallery['source_url'] = 'https://vimeo.com/channels/'.$old_gallery->source_name;
196 break;
197 }
198 $new_galleries[] = $new_gallery;
199 }
200 }
201 $wpdb->query('DROP TABLE '.VIMEOGRAPHY_GALLERY_META_TABLE.';');
202
203 $this->vimeography_update_tables();
204
205 foreach ($new_galleries as $new_gallery)
206 {
207 $wpdb->insert(
208 VIMEOGRAPHY_GALLERY_META_TABLE,
209 $new_gallery
210 );
211 }
212 }
213 }
214
215 /**
216 * Check if the Vimeography database structure needs updated to version 0.7 based on the stored db version.
217 *
218 * @access public
219 * @return void
220 */
221 public function vimeography_update_db_to_0_7()
222 {
223 if (get_option('vimeography_db_version') < 0.7)
224 {
225 $this->vimeography_update_tables();
226 }
227 }
228
229 /**
230 * Updates the Vimeography version stored in the database.
231 *
232 * @access public
233 * @return void
234 */
235 public function vimeography_update_db_version()
236 {
237 update_option('vimeography_db_version', VIMEOGRAPHY_VERSION);
238 }
239
240 /**
241 * Add Settings link to "installed plugins" admin page.
242 *
243 * @access public
244 * @param mixed $links
245 * @param mixed $file
246 * @return void
247 */
248 public function vimeography_filter_plugin_actions($links, $file)
249 {
250 if ( $file == VIMEOGRAPHY_BASENAME )
251 {
252 $settings_link = '<a href="admin.php?page=vimeography-edit-galleries">' . __('Settings') . '</a>';
253 if (!in_array($settings_link, $links))
254 array_unshift( $links, $settings_link ); // before other links
255 }
256 return $links;
257 }
258
259 /**
260 * Action target that displays the popup to insert a form to a post/page.
261 *
262 * @access public
263 * @return void
264 */
265 public function vimeography_add_mce_popup(){
266 require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/vimeography/mce.php');
267 $mustache = new Vimeography_MCE();
268 $template = $this->_load_template('vimeography/mce');
269 echo $mustache->render($template);
270 }
271
272 /**
273 * Adds a new top level menu to the admin menu.
274 *
275 * @access public
276 * @return void
277 */
278 public function vimeography_add_menu()
279 {
280
281 global $submenu;
282
283 add_menu_page( 'Vimeography Page Title', 'Vimeography', 'manage_options', 'vimeography-edit-galleries', '', VIMEOGRAPHY_URL.'media/img/vimeography-icon.png' );
284 add_submenu_page( 'vimeography-edit-galleries', 'Edit Galleries', 'Edit Galleries', 'manage_options', 'vimeography-edit-galleries', array(&$this, 'vimeography_render_template' ));
285 add_submenu_page( 'vimeography-edit-galleries', 'New Gallery', 'New Gallery', 'manage_options', 'vimeography-new-gallery', array(&$this, 'vimeography_render_template' ));
286 add_submenu_page( 'vimeography-edit-galleries', 'My Themes', 'My Themes', 'manage_options', 'vimeography-my-themes', array(&$this, 'vimeography_render_template' ));
287 $submenu['vimeography-edit-galleries'][500] = array( 'Buy Themes', 'manage_options' , 'http://vimeography.com/themes' );
288 add_submenu_page( 'vimeography-edit-galleries', 'Vimeography Pro', 'Vimeography Pro', 'manage_options', 'vimeography-pro', array(&$this, 'vimeography_render_template' ));
289 add_submenu_page( 'vimeography-edit-galleries', 'Help', 'Help', 'manage_options', 'vimeography-help', array(&$this, 'vimeography_render_template' ));
290
291 }
292
293 public function vimeography_render_template()
294 {
295 if ( !current_user_can( 'manage_options' ) ) {
296 wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
297 }
298
299 wp_register_style( 'bootstrap_css', VIMEOGRAPHY_URL.'media/css/bootstrap.min.css');
300 wp_register_style( 'bootstrap_responsive_css', VIMEOGRAPHY_URL.'media/css/bootstrap-responsive.min.css');
301 wp_register_style( 'vimeography-admin.css', VIMEOGRAPHY_URL.'media/css/admin.css');
302 wp_register_script( 'bootstrap_tab_js', VIMEOGRAPHY_URL.'media/js/bootstrap-tab.js');
303 wp_register_script( 'bootstrap_alert_js', VIMEOGRAPHY_URL.'media/js/bootstrap-alert.js');
304 wp_register_script( 'vimeography-admin.js', VIMEOGRAPHY_URL.'media/js/admin.js', 'jquery');
305
306 wp_enqueue_style( 'bootstrap_css');
307 wp_enqueue_style( 'bootstrap_responsive_css');
308 wp_enqueue_style( 'vimeography-admin.css');
309 wp_enqueue_script( 'jquery');
310 wp_enqueue_script( 'bootstrap_tab_js');
311 wp_enqueue_script( 'bootstrap_alert_js');
312 wp_enqueue_script( 'vimeography-admin.js');
313
314 switch(current_filter())
315 {
316 case 'vimeography_page_vimeography-new-gallery':
317 require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/gallery/new.php');
318 $mustache = new Vimeography_Gallery_New();
319 $template = $this->_load_template('gallery/new');
320 break;
321 case 'toplevel_page_vimeography-edit-galleries':
322 if (isset($_GET['id']))
323 {
324 require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/gallery/edit.php');
325 $mustache = new Vimeography_Gallery_Edit();
326 $template = $this->_load_template('gallery/edit');
327 }
328 else
329 {
330 require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/gallery/list.php');
331 $mustache = new Vimeography_Gallery_List();
332 $template = $this->_load_template('gallery/list');
333 }
334 break;
335 case 'vimeography_page_vimeography-my-themes':
336 require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/theme/list.php');
337 $mustache = new Vimeography_Theme_List();
338 $template = $this->_load_template('theme/list');
339 break;
340 case 'vimeography_page_vimeography-pro':
341 require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/vimeography/pro.php');
342 $mustache = new Vimeography_Pro();
343 $template = $this->_load_template('vimeography/pro');
344 break;
345 case 'vimeography_page_vimeography-help':
346 require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/vimeography/help.php');
347 $mustache = new Vimeography_Help();
348 $template = $this->_load_template('vimeography/help');
349 break;
350 default:
351 wp_die( __('The admin template for "'.current_filter().'" cannot be found.') );
352 break;
353 }
354 echo $mustache->render($template);
355 }
356
357 protected function _load_template($name)
358 {
359 $path = VIMEOGRAPHY_PATH . 'lib/admin/templates/' . $name .'.mustache';
360 if (! $result = @file_get_contents($path))
361 wp_die('The admin template "'.$name.'" cannot be found.');
362 return $result;
363 }
364
365 /**
366 * Create tables and define defaults when plugin is activated.
367 *
368 * @access public
369 * @return void
370 */
371 public function vimeography_update_tables() {
372 global $wpdb;
373
374 delete_option('vimeography_default_settings');
375 delete_option('vimeography_advanced_settings');
376
377 add_option('vimeography_advanced_settings', array(
378 'active' => FALSE,
379 'client_id' => '',
380 'client_secret' => '',
381 'access_token' => '',
382 'access_token_secret' => '',
383 ));
384
385 add_option('vimeography_default_settings', array(
386 'source_url' => 'https://vimeo.com/channels/staffpicks/',
387 'video_limit' => 20,
388 'featured_video' => '',
389 'cache_timeout' => 3600,
390 'theme_name' => 'bugsauce',
391 ));
392
393 $sql = 'CREATE TABLE '.VIMEOGRAPHY_GALLERY_TABLE.' (
394 id mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
395 title varchar(150) NOT NULL,
396 date_created datetime NOT NULL,
397 is_active tinyint(1) NOT NULL,
398 PRIMARY KEY (id)
399 );
400 CREATE TABLE '.VIMEOGRAPHY_GALLERY_META_TABLE.' (
401 id mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
402 gallery_id mediumint(8) unsigned NOT NULL,
403 source_url varchar(100) NOT NULL,
404 video_limit mediumint(7) NOT NULL,
405 featured_video int(9) unsigned DEFAULT NULL,
406 gallery_width varchar(10) DEFAULT NULL,
407 cache_timeout mediumint(7) NOT NULL,
408 theme_name varchar(50) NOT NULL,
409 PRIMARY KEY (id)
410 );
411 ';
412
413 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
414 dbDelta($sql);
415 }
416
417 /**
418 * Read the shortcode and return the output.
419 * example:
420 * [vimeography id="1" theme='apple']
421 *
422 * @access public
423 * @param mixed $atts
424 * @return void
425 */
426 public function vimeography_shortcode($atts, $content = NULL)
427 {
428
429 // Let's get the data for this gallery from the db
430 if (intval($atts['id']))
431 {
432 global $wpdb;
433 $gallery_info = $wpdb->get_results('SELECT * from '.VIMEOGRAPHY_GALLERY_META_TABLE.' AS meta JOIN '.VIMEOGRAPHY_GALLERY_TABLE.' AS gallery ON meta.gallery_id = gallery.id WHERE meta.gallery_id = '.$atts['id'].' LIMIT 1;');
434 }
435
436 // Get admin panel options
437 $default_settings = get_option('vimeography_default_settings');
438
439 $gallery_settings['theme'] = isset($gallery_info[0]->theme_name) ? $gallery_info[0]->theme_name : $default_settings['theme_name'];
440 $gallery_settings['featured'] = isset($gallery_info[0]->featured_video) ? $gallery_info[0]->featured_video : $default_settings['featured_video'];
441 $gallery_settings['source'] = isset($gallery_info[0]->source_url) ? $gallery_info[0]->source_url : $default_settings['source_url'];
442 $gallery_settings['limit'] = isset($gallery_info[0]->video_limit) ? $gallery_info[0]->video_limit : $default_settings['video_limit'];
443 $gallery_settings['cache'] = isset($gallery_info[0]->cache_timeout) ? $gallery_info[0]->cache_timeout : $default_settings['cache_timeout'];
444 $gallery_settings['width'] = isset($gallery_info[0]->gallery_width) ? $gallery_info[0]->gallery_width : '';
445
446 // Get shortcode attributes
447 $settings = shortcode_atts( array(
448 'id' => '',
449 'theme' => $gallery_settings['theme'],
450 'featured' => $gallery_settings['featured'],
451 'source' => $gallery_settings['source'],
452 'limit' => $gallery_settings['limit'],
453 'cache' => $gallery_settings['cache'],
454 'width' => $gallery_settings['width'],
455 ), $atts );
456
457 if (!empty($settings['width']))
458 {
459 preg_match('/(\d*)(px|%?)/', $settings['width'], $matches);
460 // If a number value is set...
461 if (!empty($matches[1]))
462 {
463 // If a '%' or 'px' is set...
464 if (!empty($matches[2]))
465 {
466 // Accept the valid matching string
467 $settings['width'] = $matches[0];
468 }
469 else
470 {
471 // Append a 'px' value to the matching number
472 $settings['width'] = $matches[1] . 'px';
473 }
474 }
475 else
476 {
477 // Not a valid width
478 $settings['width'] = '';
479 }
480 }
481
482 try
483 {
484 require_once(VIMEOGRAPHY_PATH . 'lib/core.php');
485 $vimeography = Vimeography_Core::factory('videos', $settings);
486
487 $settings_check = $settings;
488 $unused_id = array_shift($settings_check);
489
490 // If the shortcode settings are equal to the DB settings, the
491 // gallery isn't being overloaded by shortcode, so proceed to render
492 // the standard cache.
493
494 if ($settings_check == $gallery_settings)
495 {
496 // if cache is set, render it. otherwise, get the json, set the
497 // cache, and render it
498
499 if (($vimeography_data = $this->get_vimeography_cache($settings['id'])) === FALSE)
500 {
501 // cache not set, let's do a new request to the vimeo API
502 // and cache it if the cache settings aren't zero seconds
503 $vimeography_data = $vimeography->get('videos');
504 if ($settings['cache'] != 0)
505 $transient = $this->set_vimeography_cache($settings['id'], $vimeography_data, $settings['cache']);
506 }
507 }
508 // Otherwise, let's see if a cache exists for these particular
509 // shortcode settings, and if not, we'll create one using an
510 // alternate cache name generated using an md5 of the serialized
511 // shortcode combined with the gallery id.
512
513 else
514 {
515 $cache_hash = $settings['id'].'_'.md5(serialize($settings_check));
516
517 // if cache is set, render it. otherwise, get the json, set the
518 // cache, and render it
519 if (($vimeography_data = $this->get_vimeography_cache($cache_hash)) === FALSE)
520 {
521 // cache not set, let's do a new request to the vimeo API
522 // and cache it if the cache settings aren't zero seconds
523 $vimeography_data = $vimeography->get('videos');
524 if ($settings['cache'] != 0)
525 $transient = $this->set_vimeography_cache($cache_hash, $vimeography_data, $settings['cache']);
526 }
527 }
528 return $vimeography->render($vimeography_data);
529 }
530 catch (Vimeography_Exception $e)
531 {
532 return "Vimeography error: ".$e->getMessage();
533 }
534 }
535
536 /**
537 * Adds the VIMEOGRAPHY_THEME_URL and VIMEOGRAPHY_ASSETS_URL to the virtual robots.txt restricted list.
538 *
539 * @access public
540 * @static
541 * @return void
542 */
543 public static function vimeography_block_robots()
544 {
545 $blocked_theme_path = str_ireplace(site_url(), '', VIMEOGRAPHY_THEME_URL);
546 $blocked_asset_path = str_ireplace(site_url(), '', VIMEOGRAPHY_ASSETS_URL);
547 echo 'Disallow: '.$blocked_theme_path."\n";
548 echo 'Disallow: '.$blocked_asset_path."\n";
549 }
550
551 /**
552 * Get the JSON data stored in the Vimeography cache for the provided gallery id.
553 *
554 * @access public
555 * @static
556 * @param mixed $id
557 * @return void
558 */
559 public static function get_vimeography_cache($id)
560 {
561 return FALSE === ( $vimeography_cache_results = get_transient( 'vimeography_cache_'.$id ) ) ? FALSE : $vimeography_cache_results;
562 }
563
564 /**
565 * Set the JSON data to the Vimeography cache for the provided gallery id.
566 *
567 * @access public
568 * @static
569 * @param mixed $id
570 * @param mixed $data
571 * @param mixed $cache_limit
572 * @return void
573 */
574 public static function set_vimeography_cache($id, $data, $cache_limit)
575 {
576 return set_transient( 'vimeography_cache_'.$id, $data, $cache_limit );
577 }
578
579 /**
580 * Clear the Vimeography cache for the provided gallery id.
581 *
582 * @access public
583 * @static
584 * @param mixed $id
585 * @return void
586 */
587 public static function delete_vimeography_cache($id)
588 {
589 return delete_transient('vimeography_cache_'.$id);
590 }
591
592 /**
593 * Moves the given folder to the given destination.
594 *
595 * @access private
596 * @param array $args (default: array())
597 * @return void
598 */
599 private function _move_folder($args = array())
600 {
601 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
602 // Replaces simple `WP_Filesystem();` call to prevent any extraction issues
603 // @link http://wpquestions.com/question/show/id/2685
604 if (! function_exists('__return_direct'))
605 {
606 function __return_direct() { return 'direct'; }
607 }
608
609 add_filter( 'filesystem_method', '__return_direct' );
610 WP_Filesystem();
611 remove_filter( 'filesystem_method', '__return_direct' );
612
613 global $wp_filesystem;
614 $defaults = array( 'source' => '', 'destination' => '', //Please always pass these
615 'clear_destination' => false, 'clear_working' => false,
616 'hook_extra' => array());
617
618 $args = wp_parse_args($args, $defaults);
619 extract($args);
620
621 @set_time_limit( 300 );
622
623 if ( empty($source) || empty($destination) )
624 return new WP_Error('bad_request', 'bad request.');
625
626 // $this->skin->feedback('installing_package');
627
628 //Retain the Original source and destinations
629 $remote_source = $source;
630 $local_destination = $destination;
631
632 if (! $wp_filesystem->dirlist($remote_source)) return FALSE;
633
634 $source_files = array_keys( $wp_filesystem->dirlist($remote_source) );
635 $remote_destination = $wp_filesystem->find_folder($local_destination);
636
637 //Locate which directory to copy to the new folder, This is based on the actual folder holding the files.
638 if ( 1 == count($source_files) && $wp_filesystem->is_dir( trailingslashit($source) . $source_files[0] . '/') ) //Only one folder? Then we want its contents.
639 $source = trailingslashit($source) . trailingslashit($source_files[0]);
640 elseif ( count($source_files) == 0 )
641 return new WP_Error( 'incompatible_archive', 'incompatible archive string', __( 'The plugin contains no files.' ) ); //There are no files?
642 else //Its only a single file, The upgrader will use the foldername of this file as the destination folder. foldername is based on zip filename.
643 $source = trailingslashit($source);
644
645 //Has the source location changed? If so, we need a new source_files list.
646 if ( $source !== $remote_source )
647 $source_files = array_keys( $wp_filesystem->dirlist($source) );
648
649 if ( $clear_destination ) {
650 //We're going to clear the destination if there's something there
651 //$this->skin->feedback('remove_old');
652 $removed = true;
653 if ( $wp_filesystem->exists($remote_destination) )
654 $removed = $wp_filesystem->delete($remote_destination, true);
655 if ( is_wp_error($removed) )
656 return $removed;
657 else if ( ! $removed )
658 return new WP_Error('remove_old_failed', 'couldnt remove old');
659 } elseif ( $wp_filesystem->exists($remote_destination) ) {
660 //If we're not clearing the destination folder and something exists there already, Bail.
661 //But first check to see if there are actually any files in the folder.
662 $_files = $wp_filesystem->dirlist($remote_destination);
663 if ( ! empty($_files) ) {
664 $wp_filesystem->delete($remote_source, true); //Clear out the source files.
665 return new WP_Error('folder_exists', 'folder exists string', $remote_destination );
666 }
667 }
668
669 //Create themes folder, if needed
670 if ( !$wp_filesystem->exists(VIMEOGRAPHY_THEME_PATH) )
671 if ( !$wp_filesystem->mkdir(VIMEOGRAPHY_THEME_PATH, FS_CHMOD_DIR) )
672 return new WP_Error('mkdir_failed', 'mkdir failer string', $remote_destination);
673
674 //Create destination if needed
675 if ( !$wp_filesystem->exists($remote_destination) )
676 if ( !$wp_filesystem->mkdir($remote_destination, FS_CHMOD_DIR) )
677 return new WP_Error('mkdir_failed', 'mkdir failer string', $remote_destination);
678
679 // Copy new version of item into place.
680 $result = copy_dir($source, $remote_destination);
681
682 if ( is_wp_error($result) ) {
683 if ( $clear_working )
684 $wp_filesystem->delete($remote_source, true);
685 return $result;
686 }
687
688 //Clear the Working folder?
689 if ( $clear_working )
690 $wp_filesystem->delete($remote_source, true);
691
692 $destination_name = basename( str_replace($local_destination, '', $destination) );
693 if ( '.' == $destination_name )
694 $destination_name = '';
695
696 $result = compact('local_source', 'source', 'source_name', 'source_files', 'destination', 'destination_name', 'local_destination', 'remote_destination', 'clear_destination', 'delete_source_dir');
697
698 //Bombard the calling function will all the info which we've just used.
699 return $result;
700
701 }
702
703 }
704
705 new Vimeography;