PluginProbe
Vimeography: Vimeo Video Gallery WordPress Plugin / 0.8
Vimeography: Vimeo Video Gallery WordPress Plugin v0.8
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.8, at vimeography.php

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