PluginProbe
Vimeography: Vimeo Video Gallery WordPress Plugin / 1.0.3
Vimeography: Vimeo Video Gallery WordPress Plugin v1.0.3
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 1.0.3, at vimeography.php

708 lines 24.1 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: 1.0.3
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 requires the JSON PHP extension.');
14
15 global $wpdb;
16
17 // Define constants
18 define( 'VIMEOGRAPHY_URL', plugin_dir_url(__FILE__) );
19 define( 'VIMEOGRAPHY_PATH', plugin_dir_path(__FILE__) );
20 define( 'VIMEOGRAPHY_THEME_URL', WP_CONTENT_URL . '/vimeography/themes/' );
21 define( 'VIMEOGRAPHY_THEME_PATH', WP_CONTENT_DIR . '/vimeography/themes/' );
22 define( 'VIMEOGRAPHY_ASSETS_URL', WP_CONTENT_URL . '/vimeography/assets/' );
23 define( 'VIMEOGRAPHY_ASSETS_PATH', WP_CONTENT_DIR . '/vimeography/assets/' );
24 define( 'VIMEOGRAPHY_CACHE_URL', WP_CONTENT_URL . '/vimeography/cache/' );
25 define( 'VIMEOGRAPHY_CACHE_PATH', WP_CONTENT_DIR . '/vimeography/cache/' );
26 define( 'VIMEOGRAPHY_BASENAME', plugin_basename( __FILE__ ) );
27 define( 'VIMEOGRAPHY_VERSION', '1.0.3');
28 define( 'VIMEOGRAPHY_GALLERY_TABLE', $wpdb->prefix . "vimeography_gallery");
29 define( 'VIMEOGRAPHY_GALLERY_META_TABLE', $wpdb->prefix . "vimeography_gallery_meta");
30 define( 'VIMEOGRAPHY_CURRENT_PAGE', basename($_SERVER['PHP_SELF']));
31 define( 'VIMEOGRAPHY_CLIENT_ID', 'fc0927c077cb47345eadf7c513d70f4aa564f30d');
32
33 require_once(VIMEOGRAPHY_PATH . 'lib/exception.php');
34
35 // Require Mustache.php
36 if (! class_exists('Mustache_Engine'))
37 {
38 require_once(VIMEOGRAPHY_PATH . '/vendor/mustache.php-master/src/Mustache/Autoloader.php');
39 Mustache_Autoloader::register();
40 }
41
42 class Vimeography
43 {
44 /**
45 * [$themes description]
46 * @var array
47 */
48 public $themes = array();
49
50 /**
51 * [$active_theme description]
52 * @var [type]
53 */
54 public $active_theme = NULL;
55
56 /**
57 * [$instance description]
58 * @var [type]
59 */
60 private static $instance = NULL;
61
62 /**
63 * Creates or returns an instance of this class.
64 *
65 * @return Vimeography A single instance of this class.
66 */
67 public static function get_instance()
68 {
69 if ( NULL == self::$instance )
70 self::$instance = new self;
71
72 return self::$instance;
73 }
74
75 /**
76 * [__construct description]
77 */
78 private function __construct()
79 {
80 add_action( 'init', array($this, 'vimeography_init') );
81 add_action( 'admin_init', array($this, 'vimeography_requires_wordpress_version') );
82 add_action( 'admin_init', array($this, 'vimeography_check_if_db_exists') );
83 add_action( 'admin_init', array($this, 'vimeography_load_updater'));
84 add_action( 'admin_init', array($this, 'vimeography_activate_bugsauce'));
85 add_action( 'init', array($this, 'vimeography_move_folders') );
86 add_action( 'plugins_loaded', array($this, 'vimeography_update_database') );
87 add_action( 'admin_menu', array($this, 'vimeography_add_menu') );
88 add_action( 'do_robots', array($this, 'vimeography_block_robots') );
89
90 // Check the URL for any Vimeography-related parameters
91 add_filter( 'query_vars', array($this, 'vimeography_add_query_vars') );
92 add_action( 'generate_rewrite_rules', array($this, 'vimeography_add_rewrite_rules' ) );
93 add_action( 'parse_request', array($this, 'vimeography_parse_request') );
94
95 register_activation_hook( VIMEOGRAPHY_BASENAME, array($this, 'vimeography_update_tables') );
96 register_activation_hook( VIMEOGRAPHY_BASENAME, array($this, 'vimeography_flush_rewrite_rules') );
97 register_deactivation_hook( VIMEOGRAPHY_BASENAME, array($this, 'vimeography_deactivate_bugsauce') );
98
99 add_filter( 'plugin_action_links', array($this, 'vimeography_filter_plugin_actions'), 10, 2 );
100 add_shortcode( 'vimeography', array($this, 'vimeography_shortcode') );
101
102 // Add shortcode support for widgets
103 add_filter( 'widget_text', 'do_shortcode' );
104
105 // Load the themes that are hooking in to this action.
106 add_action('vimeography/load-theme', array($this, 'vimeography_load_theme'));
107 }
108
109 /**
110 * Check the wordpress version is compatible, and disable plugin if not.
111 *
112 * @access public
113 * @return void
114 */
115 public function vimeography_requires_wordpress_version()
116 {
117 global $wp_version;
118 $plugin_data = get_plugin_data( __FILE__, false );
119
120 if ( version_compare($wp_version, "3.3", "<" ) )
121 {
122 if( is_plugin_active( VIMEOGRAPHY_BASENAME ) )
123 {
124 deactivate_plugins( VIMEOGRAPHY_BASENAME );
125 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>." );
126 }
127 }
128 }
129
130 /**
131 * [vimeography_check_for_updates description]
132 * @return [type] [description]
133 */
134 public function vimeography_load_updater()
135 {
136 require_once(VIMEOGRAPHY_PATH . 'lib/update.php');
137 new Vimeography_Update;
138 }
139
140 /**
141 * Runs on every page load.
142 *
143 * @access public
144 * @return void
145 */
146 public function vimeography_init()
147 {
148 require_once(VIMEOGRAPHY_PATH . 'lib/init.php');
149 $init = new Vimeography_Init;
150
151 $init->vimeography_register_jquery();
152 $init->vimeography_add_gallery_helper();
153 $init->vimeography_written_block_robots();
154
155 require_once(VIMEOGRAPHY_PATH . 'lib/ajax.php');
156 new Vimeography_Ajax;
157 }
158
159 /**
160 * [vimeography_load_theme description]
161 * @param [type] $theme_path [description]
162 * @return [type] [description]
163 */
164 public function vimeography_load_theme($theme_path)
165 {
166 $theme = self::_get_theme_data($theme_path);
167
168 $theme['basename'] = plugin_basename($theme_path);
169 $theme['slug'] = substr($theme['basename'], 0, strpos($theme['basename'], "/"));
170 $theme['thumbnail'] = plugins_url(strtolower($theme['name']) .'.jpg', $theme_path);
171 $theme['file_path'] = $theme_path;
172 $theme['plugin_path'] = plugin_dir_path($theme_path);
173 $theme['partials_path'] = plugin_dir_path($theme_path) . 'partials';
174 $theme['settings_file'] = plugin_dir_path($theme_path) . 'settings.php';
175
176 $this->themes[] = $theme;
177 }
178
179 /**
180 * [set_active_theme description]
181 * @param [type] $theme_name [description]
182 */
183 public function set_active_theme($theme_name)
184 {
185 foreach($this->themes as $index => $theme)
186 {
187 if (strtolower($theme['name']) === strtolower($theme_name))
188 {
189 $this->active_theme = $theme;
190 }
191 }
192 return $this;
193 }
194
195 /**
196 * Retrieves the meta data from the headers of a given plugin file.
197 *
198 * @access private
199 * @static
200 * @param mixed $plugin_file
201 * @return void
202 */
203 private static function _get_theme_data($plugin_file)
204 {
205
206 $default_headers = array(
207 'name' => 'Theme Name',
208 'theme-uri' => 'Theme URI',
209 'version' => 'Version',
210 'description' => 'Description',
211 'author' => 'Author',
212 'author-uri' => 'Author URI',
213 );
214
215 return get_file_data( $plugin_file, $default_headers );
216 }
217
218 /**
219 * [vimeography_update_database description]
220 * @return [type] [description]
221 */
222 public function vimeography_update_database()
223 {
224 require_once(VIMEOGRAPHY_PATH . 'lib/database.php');
225 $db = new Vimeography_Database;
226
227 $db->vimeography_update_db_to_0_6();
228 $db->vimeography_update_db_to_0_7();
229 $db->vimeography_update_db_to_0_8();
230 $db->vimeography_update_db_to_1_0();
231 $this->vimeography_update_db_version();
232 }
233
234 /**
235 * [vimeography_check_if_db_exists description]
236 * @return [type] [description]
237 */
238 public function vimeography_check_if_db_exists()
239 {
240 if (get_option('vimeography_db_version') == FALSE)
241 $this->vimeography_update_db_version();
242 }
243
244 /**
245 * Updates the Vimeography version stored in the database.
246 *
247 * @access public
248 * @return void
249 */
250 public function vimeography_update_db_version()
251 {
252 update_option('vimeography_db_version', VIMEOGRAPHY_VERSION);
253 }
254
255 /**
256 * Move the defined folders to the defined target path in wp-content/uploads.
257 *
258 * @access public
259 * @return void
260 */
261 public function vimeography_move_folders()
262 {
263 $this->_move_folder(array('source' => VIMEOGRAPHY_PATH . 'vimeography-bugsauce/', 'destination' => str_replace('vimeography/', '', VIMEOGRAPHY_PATH) . 'vimeography-bugsauce/', 'clear_destination' => true, 'clear_working' => true));
264 $this->_move_folder(array('source' => VIMEOGRAPHY_PATH . 'components/', 'destination' => VIMEOGRAPHY_ASSETS_PATH, 'clear_destination' => true, 'clear_working' => true));
265
266 if (! file_exists(VIMEOGRAPHY_CACHE_PATH) )
267 {
268 // creates the dir and it's parent dirs
269 if (! mkdir(VIMEOGRAPHY_CACHE_PATH, 0777, true) )
270 {
271 if( is_plugin_active( VIMEOGRAPHY_BASENAME ) )
272 {
273 deactivate_plugins( VIMEOGRAPHY_BASENAME );
274 wp_die( "Vimeography could not create the cache directory. Please contact your host and request permissions to write to your Wordpress installation's wp-content directory.<br /><br />Back to <a href='".admin_url()."'>WordPress admin</a>." );
275 }
276 }
277 }
278 }
279
280 /**
281 * Add Settings link to "installed plugins" admin page.
282 *
283 * @access public
284 * @param mixed $links
285 * @param mixed $file
286 * @return void
287 */
288 public function vimeography_filter_plugin_actions($links, $file)
289 {
290 if ( $file == VIMEOGRAPHY_BASENAME )
291 {
292 $settings_link = '<a href="admin.php?page=vimeography-edit-galleries">' . __('Settings') . '</a>';
293 if (!in_array($settings_link, $links))
294 array_unshift( $links, $settings_link ); // before other links
295 }
296 return $links;
297 }
298
299 /**
300 * Adds a new top level menu to the admin menu.
301 *
302 * @access public
303 * @return void
304 */
305 public function vimeography_add_menu()
306 {
307 global $submenu;
308
309 add_menu_page( 'Vimeography Page Title', 'Vimeography', 'manage_options', 'vimeography-edit-galleries', '', VIMEOGRAPHY_URL.'media/img/vimeography-icon.png' );
310 add_submenu_page( 'vimeography-edit-galleries', 'Edit Galleries', 'Edit Galleries', 'manage_options', 'vimeography-edit-galleries', array(&$this, 'vimeography_render_template' ));
311 add_submenu_page( 'vimeography-edit-galleries', 'New Gallery', 'New Gallery', 'manage_options', 'vimeography-new-gallery', array(&$this, 'vimeography_render_template' ));
312 add_submenu_page( 'vimeography-edit-galleries', 'My Themes', 'My Themes', 'manage_options', 'vimeography-my-themes', array(&$this, 'vimeography_render_template' ));
313 $submenu['vimeography-edit-galleries'][500] = array( 'Vimeography Themes', 'manage_options' , 'http://vimeography.com/themes' );
314 add_submenu_page( 'vimeography-edit-galleries', 'Vimeography Pro', 'Vimeography Pro', 'manage_options', 'vimeography-pro', array(&$this, 'vimeography_render_template' ));
315 add_submenu_page( 'vimeography-edit-galleries', 'Help', 'Help', 'manage_options', 'vimeography-help', array(&$this, 'vimeography_render_template' ));
316 }
317
318 /**
319 * Renders the admin template for the current page.
320 *
321 * @access public
322 * @return void
323 */
324 public function vimeography_render_template()
325 {
326 if ( !current_user_can( 'manage_options' ) )
327 wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
328
329 wp_register_style( 'vimeography-bootstrap', VIMEOGRAPHY_URL.'media/css/bootstrap.min.css');
330 wp_register_style( 'vimeography-admin', VIMEOGRAPHY_URL.'media/css/admin.css');
331
332 wp_register_script( 'vimeography-bootstrap', VIMEOGRAPHY_URL.'media/js/bootstrap.min.js');
333 wp_register_script( 'vimeography-admin', VIMEOGRAPHY_URL.'media/js/admin.js', 'jquery');
334
335 wp_enqueue_style( 'vimeography-bootstrap');
336 wp_enqueue_style( 'vimeography-admin');
337
338 wp_enqueue_script( 'vimeography-bootstrap');
339 wp_enqueue_script( 'vimeography-admin');
340
341 $mustache = new Mustache_Engine(array('loader' => new Mustache_Loader_FilesystemLoader(VIMEOGRAPHY_PATH . 'lib/admin/templates'),));
342 require_once(VIMEOGRAPHY_PATH . 'lib/admin/base.php');
343
344 switch(current_filter())
345 {
346 case 'vimeography_page_vimeography-new-gallery':
347 require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/gallery/new.php');
348 $view = new Vimeography_Gallery_New;
349 $template = $mustache->loadTemplate('gallery/new');
350 break;
351 case 'toplevel_page_vimeography-edit-galleries':
352 if (isset($_GET['id']))
353 {
354 require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/gallery/edit.php');
355 $view = new Vimeography_Gallery_Edit;
356
357 $mustache->setPartialsLoader( new Mustache_Loader_CascadingLoader( array(
358 new Mustache_Loader_FilesystemLoader(VIMEOGRAPHY_PATH . 'lib/admin/templates/gallery/edit/partials'),
359 ) )
360 );
361
362 apply_filters('vimeography-pro/load-edit-partials', $mustache->getPartialsLoader());
363
364 //$mustache->setPartialsLoader(new Mustache_Loader_FilesystemLoader(VIMEOGRAPHY_PATH . 'lib/admin/templates/gallery/edit/partials'));
365
366 $template = $mustache->loadTemplate('gallery/edit/layout');
367
368 }
369 else
370 {
371 require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/gallery/list.php');
372 $view = new Vimeography_Gallery_List;
373 $template = $mustache->loadTemplate('gallery/list');
374 }
375 break;
376 case 'vimeography_page_vimeography-my-themes':
377 require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/theme/list.php');
378 $view = new Vimeography_Theme_List;
379 $template = $mustache->loadTemplate('theme/list');
380 break;
381 case 'vimeography_page_vimeography-pro':
382 require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/vimeography/pro.php');
383 $view = new Vimeography_Pro_About;
384 $template = $mustache->loadTemplate('vimeography/pro');
385 break;
386 case 'vimeography_page_vimeography-help':
387 require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/vimeography/help.php');
388 $view = new Vimeography_Help;
389 $template = $mustache->loadTemplate('vimeography/help');
390 break;
391 default:
392 wp_die( __('The admin template for "'.current_filter().'" cannot be found.') );
393 break;
394 }
395
396 echo $template->render($view);
397 }
398
399 /**
400 * Create tables and define defaults when plugin is activated.
401 *
402 * @access public
403 * @return void
404 */
405 public function vimeography_update_tables() {
406 global $wpdb;
407
408 delete_option('vimeography_default_settings');
409 delete_option('vimeography_advanced_settings');
410
411 add_option('vimeography_default_settings', array(
412 'source_url' => 'https://vimeo.com/channels/staffpicks/',
413 'resource_uri' => '/channels/staffpicks',
414 'featured_video' => '',
415 'cache_timeout' => 3600,
416 'theme_name' => 'bugsauce',
417 ));
418
419 $sql = 'CREATE TABLE '.VIMEOGRAPHY_GALLERY_TABLE.' (
420 id mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
421 title varchar(150) NOT NULL,
422 date_created datetime NOT NULL,
423 is_active tinyint(1) NOT NULL,
424 PRIMARY KEY (id)
425 );
426 CREATE TABLE '.VIMEOGRAPHY_GALLERY_META_TABLE.' (
427 id mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
428 gallery_id mediumint(8) unsigned NOT NULL,
429 source_url varchar(100) NOT NULL,
430 resource_uri varchar(50) NOT NULL,
431 featured_video varchar(100) DEFAULT NULL,
432 gallery_width varchar(10) DEFAULT NULL,
433 cache_timeout mediumint(7) NOT NULL,
434 theme_name varchar(50) NOT NULL,
435 PRIMARY KEY (id)
436 );
437 ';
438
439 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
440 dbDelta($sql);
441 }
442
443 /**
444 * Checks if the provided Vimeo URL is valid and if so, returns an array
445 * containing the URL parts
446 *
447 * @param string $source_url Source collection of Vimeo videos.
448 * @return string Vimeo Resource
449 */
450 public static function validate_vimeo_source($source_url)
451 {
452 $scheme = parse_url($source_url);
453
454 if (empty($scheme['scheme']))
455 $source_url = 'https://' . $source_url;
456
457 if ((($url = parse_url($source_url)) !== FALSE) && (preg_match('~vimeo(?:pro)?\.com$~', $url['host']) > 0))
458 {
459 $host = $url['host'];
460 $url = array_filter(explode('/', $url['path']), 'strlen');
461
462 // If the array doesn't contain one of the following strings, it
463 // must be either a user or a video
464 if (in_array($url[1], array('album', 'channels', 'groups', 'categories')) !== TRUE)
465 {
466 if (is_numeric($url[1]))
467 {
468 array_unshift($url, 'videos');
469 }
470 elseif (isset($url[2]) AND $host != 'vimeo.com')
471 {
472 array_unshift($url, 'portfolios');
473 }
474 else
475 {
476 array_unshift($url, 'users');
477 }
478 }
479
480 // make sure the resource is plural
481 $resource = '/' . rtrim(array_shift($url), 's') . 's/' . array_shift($url);
482 return $resource;
483 }
484 else
485 {
486 throw new Vimeography_Exception('That site doesn\'t look like a valid link to a Vimeo collection.');
487 }
488 }
489
490 /**
491 * Read the shortcode and return the output.
492 * example:
493 * [vimeography id="1" theme='apple']
494 *
495 * @access public
496 * @param mixed $atts
497 * @return void
498 */
499 public function vimeography_shortcode($atts, $content = NULL)
500 {
501 require_once(VIMEOGRAPHY_PATH . 'lib/shortcode.php');
502 $shortcode = new Vimeography_Shortcode($atts, $content);
503 return $shortcode->output();
504 }
505
506 /**
507 * Adds the VIMEOGRAPHY_ASSETS_URL to the virtual robots.txt restricted list.
508 *
509 * @access public
510 * @static
511 * @return void
512 */
513 public static function vimeography_block_robots()
514 {
515 $blocked_asset_path = str_ireplace(site_url(), '', VIMEOGRAPHY_ASSETS_URL);
516 echo 'Disallow: '.$blocked_asset_path."\n";
517 }
518
519 /**
520 * [vimeography_add_query_vars description]
521 * @param [type] $vars [description]
522 * @return [type] [description]
523 */
524 public function vimeography_add_query_vars($vars)
525 {
526 $vars[] = 'vimeography_action';
527 $vars[] = 'vimeography_gallery_id';
528 return $vars;
529 }
530
531 /**
532 * Adds custom rewrite rules.
533 * @param [type] $wp_rewrite [description]
534 * @return [type] [description]
535 */
536 function vimeography_add_rewrite_rules($wp_rewrite)
537 {
538 $wp_rewrite->rules = array(
539 'vimeography/([0-9]{1,4})+/refresh\/?' => $wp_rewrite->index . '?vimeography_action=refresh&vimeography_gallery_id=' . $wp_rewrite->preg_index( 1 ),
540 //'vimeography/notify\/?' => $wp_rewrite->index . '?vimeography_action=' . $wp_rewrite->preg_index( 1 ),
541 ) + $wp_rewrite->rules;
542 }
543
544 /**
545 * [vimeography_parse_request description]
546 * @param [type] $wp [description]
547 * @return [type] [description]
548 */
549 public function vimeography_parse_request($wp)
550 {
551 if (array_key_exists('vimeography_action', $wp->query_vars) AND $wp->query_vars['vimeography_action'] == 'refresh')
552 {
553 require_once VIMEOGRAPHY_PATH . 'lib/cache.php';
554 $cache = new Vimeography_Cache($wp->query_vars['vimeography_gallery_id']);
555 if ($cache->exists())
556 $cache->delete();
557 die('Thanks, Vimeo. Cache busted.');
558 }
559 }
560
561 /**
562 * [vimeography_flush_rewrite_rules description]
563 * @return [type] [description]
564 */
565 public function vimeography_flush_rewrite_rules()
566 {
567 return flush_rewrite_rules();
568 }
569
570 /**
571 * [vimeography_activate_plugin description]
572 * @param [type] $basename [description]
573 * @return [type] [description]
574 */
575 public function vimeography_activate_bugsauce()
576 {
577 $bugsauce = str_replace('vimeography/', 'vimeography-bugsauce/', VIMEOGRAPHY_PATH);
578 if ( is_plugin_inactive('vimeography-bugsauce/vimeography-bugsauce.php') AND file_exists($bugsauce) )
579 activate_plugin('vimeography-bugsauce/vimeography-bugsauce.php');
580 }
581
582 /**
583 * [vimeography_activate_plugin description]
584 * @param [type] $basename [description]
585 * @return [type] [description]
586 */
587 public function vimeography_deactivate_bugsauce()
588 {
589 if (is_plugin_active('vimeography-bugsauce/vimeography-bugsauce.php'))
590 deactivate_plugins('vimeography-bugsauce/vimeography-bugsauce.php');
591 }
592
593 /**
594 * Moves the given folder to the given destination.
595 *
596 * @access private
597 * @param array $args (default: array())
598 * @return void
599 */
600 private function _move_folder($args = array())
601 {
602 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
603
604 // Replaces simple `WP_Filesystem();` call to prevent any extraction issues
605 // @link http://wpquestions.com/question/show/id/2685
606 if (! function_exists('__return_direct'))
607 {
608 function __return_direct() { return 'direct'; }
609 }
610
611 add_filter( 'filesystem_method', '__return_direct' );
612 WP_Filesystem();
613 remove_filter( 'filesystem_method', '__return_direct' );
614
615 global $wp_filesystem;
616
617 // Always pass these
618 $defaults = array(
619 'source' => '',
620 'destination' => '',
621 'clear_destination' => false,
622 'clear_working' => false,
623 'hook_extra' => array(),
624 );
625
626 $args = wp_parse_args($args, $defaults);
627 extract($args);
628
629 @set_time_limit( 300 );
630
631 if ( empty($source) || empty($destination) )
632 return new WP_Error('bad_request', 'bad request.');
633
634 //Retain the Original source and destinations
635 $remote_source = $source;
636 $local_destination = $destination;
637
638 if (! $wp_filesystem->dirlist($remote_source))
639 return FALSE;
640
641 $source_files = array_keys( $wp_filesystem->dirlist($remote_source) );
642 $remote_destination = $wp_filesystem->find_folder($local_destination);
643
644 //Locate which directory to copy to the new folder, This is based on the actual folder holding the files.
645 if ( 1 == count($source_files) && $wp_filesystem->is_dir( trailingslashit($source) . $source_files[0] . '/') ) //Only one folder? Then we want its contents.
646 $source = trailingslashit($source) . trailingslashit($source_files[0]);
647 elseif ( count($source_files) == 0 )
648 return new WP_Error( 'incompatible_archive', 'incompatible archive string', __( 'The plugin contains no files.' ) ); //There are no files?
649 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.
650 $source = trailingslashit($source);
651
652 //Has the source location changed? If so, we need a new source_files list.
653 if ( $source !== $remote_source )
654 $source_files = array_keys( $wp_filesystem->dirlist($source) );
655
656 if ( $clear_destination ) {
657 //We're going to clear the destination if there's something there
658 //$this->skin->feedback('remove_old');
659 $removed = true;
660 if ( $wp_filesystem->exists($remote_destination) )
661 $removed = $wp_filesystem->delete($remote_destination, true);
662 if ( is_wp_error($removed) )
663 return $removed;
664 else if ( ! $removed )
665 return new WP_Error('remove_old_failed', 'couldnt remove old');
666 } elseif ( $wp_filesystem->exists($remote_destination) ) {
667 //If we're not clearing the destination folder and something exists there already, Bail.
668 //But first check to see if there are actually any files in the folder.
669 $_files = $wp_filesystem->dirlist($remote_destination);
670 if ( ! empty($_files) ) {
671 $wp_filesystem->delete($remote_source, true); //Clear out the source files.
672 return new WP_Error('folder_exists', 'folder exists string', $remote_destination );
673 }
674 }
675
676 //Create destination if needed
677 if ( !$wp_filesystem->exists($remote_destination) )
678 if ( !$wp_filesystem->mkdir($remote_destination, FS_CHMOD_DIR) )
679 return new WP_Error('mkdir_failed', 'mkdir failer string', $remote_destination);
680
681 // Copy new version of item into place.
682 $result = copy_dir($source, $remote_destination);
683
684 if ( is_wp_error($result) ) {
685 if ( $clear_working )
686 $wp_filesystem->delete($remote_source, true);
687 return $result;
688 }
689
690 //Clear the Working folder?
691 if ( $clear_working )
692 $wp_filesystem->delete($remote_source, true);
693
694 $destination_name = basename( str_replace($local_destination, '', $destination) );
695 if ( '.' == $destination_name )
696 $destination_name = '';
697
698 $result = compact('local_source', 'source', 'source_name', 'source_files', 'destination', 'destination_name', 'local_destination', 'remote_destination', 'clear_destination', 'delete_source_dir');
699
700 //Bombard the calling function will all the info which we've just used.
701 return $result;
702
703 }
704
705 }
706
707 Vimeography::get_instance();
708