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

564 lines 20.4 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.6
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.6');
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( 'plugins_loaded', array(&$this, 'vimeography_update_db_to_0_6') );
41 add_action( 'plugins_loaded', array(&$this, 'vimeography_update_db_version') );
42 add_action( 'admin_menu', array(&$this, 'vimeography_add_menu') );
43
44 register_activation_hook( VIMEOGRAPHY_BASENAME, array(&$this, 'vimeography_update_tables') );
45
46 add_filter( 'plugin_action_links', array(&$this, 'vimeography_filter_plugin_actions'), 10, 2 );
47 add_shortcode( 'vimeography', array(&$this, 'vimeography_shortcode') );
48
49 // Add shortcode support for widgets
50 add_filter( 'widget_text', 'do_shortcode' );
51 }
52
53 public function vimeography_init()
54 {
55 if(in_array(VIMEOGRAPHY_CURRENT_PAGE, array('post.php', 'page.php', 'page-new.php', 'post-new.php'))){
56 add_action('admin_footer', array(&$this, 'vimeography_add_mce_popup'));
57 }
58
59 if ( get_user_option('rich_editing') == 'true' ) {
60 add_filter( 'mce_external_plugins', array(&$this, 'vimeography_add_editor_plugin' ));
61 add_filter( 'mce_buttons', array(&$this, 'vimeography_register_editor_button') );
62 }
63
64 }
65
66 public function vimeography_register_editor_button($buttons)
67 {
68 array_push( $buttons, "|", "vimeography" );
69 return $buttons;
70 }
71
72 public function vimeography_add_editor_plugin( $plugin_array ) {
73 $plugin_array['vimeography'] = VIMEOGRAPHY_URL . 'media/js/mce.js';
74 return $plugin_array;
75 }
76
77 /**
78 * Check the wordpress version is compatible, and disable plugin if not.
79 *
80 * @access public
81 * @return void
82 */
83 public function vimeography_requires_wordpress_version() {
84 global $wp_version;
85 $plugin = plugin_basename( __FILE__ );
86 $plugin_data = get_plugin_data( __FILE__, false );
87
88 if ( version_compare($wp_version, "3.3", "<" ) ) {
89 if( is_plugin_active($plugin) ) {
90 deactivate_plugins( $plugin );
91 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>." );
92 }
93 }
94 }
95
96 public function vimeography_check_if_db_exists()
97 {
98 if (get_option('vimeography_db_version') == FALSE)
99 $this->vimeography_update_db_version();
100 }
101
102 /**
103 * Check if the Vimeography database structure needs updated to version 0.6 based on the stored db version.
104 *
105 * @access public
106 * @return void
107 */
108 public function vimeography_update_db_to_0_6()
109 {
110 if (get_option('vimeography_db_version') < 0.6)
111 {
112 global $wpdb;
113 $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;');
114 $new_galleries = array();
115
116 if (is_array($old_galleries))
117 {
118 foreach ($old_galleries as $old_gallery)
119 {
120 $new_gallery = array();
121
122 $new_gallery['gallery_id'] = $old_gallery->gallery_id;
123 $new_gallery['video_limit'] = $old_gallery->video_count;
124 $new_gallery['featured_video'] = $old_gallery->featured_video;
125 $new_gallery['cache_timeout'] = $old_gallery->cache_timeout;
126 $new_gallery['theme_name'] = $old_gallery->theme_name;
127 switch ($old_gallery->source_type)
128 {
129 case 'user':
130 $new_gallery['source_url'] = 'https://vimeo.com/'.$old_gallery->source_name;
131 break;
132 case 'album':
133 $new_gallery['source_url'] = 'https://vimeo.com/album/'.$old_gallery->source_name;
134 break;
135 case 'group':
136 $new_gallery['source_url'] = 'https://vimeo.com/groups/'.$old_gallery->source_name;
137 break;
138 case 'channel':
139 $new_gallery['source_url'] = 'https://vimeo.com/channels/'.$old_gallery->source_name;
140 break;
141 }
142 $new_galleries[] = $new_gallery;
143 }
144 }
145 $wpdb->query('DROP TABLE '.VIMEOGRAPHY_GALLERY_META_TABLE.';');
146
147 $this->vimeography_update_tables();
148
149 foreach ($new_galleries as $new_gallery)
150 {
151 $wpdb->insert(
152 VIMEOGRAPHY_GALLERY_META_TABLE,
153 $new_gallery
154 );
155 }
156 }
157 }
158
159 /**
160 * Updates the Vimeography version stored in the database.
161 *
162 * @access public
163 * @return void
164 */
165 public function vimeography_update_db_version()
166 {
167 update_option('vimeography_db_version', VIMEOGRAPHY_VERSION);
168 }
169
170 /**
171 * Add Settings link to "installed plugins" admin page.
172 *
173 * @access public
174 * @param mixed $links
175 * @param mixed $file
176 * @return void
177 */
178 public function vimeography_filter_plugin_actions($links, $file)
179 {
180 if ( $file == VIMEOGRAPHY_BASENAME )
181 {
182 $settings_link = '<a href="admin.php?page=vimeography-edit-galleries">' . __('Settings') . '</a>';
183 if (!in_array($settings_link, $links))
184 array_unshift( $links, $settings_link ); // before other links
185 }
186 return $links;
187 }
188
189 /**
190 * Action target that displays the popup to insert a form to a post/page.
191 *
192 * @access public
193 * @return void
194 */
195 public function vimeography_add_mce_popup(){
196 require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/vimeography/mce.php');
197 $mustache = new Vimeography_MCE();
198 $template = $this->_load_template('vimeography/mce');
199 echo $mustache->render($template);
200 }
201
202 /**
203 * Adds a new top level menu to the admin menu.
204 *
205 * @access public
206 * @return void
207 */
208 public function vimeography_add_menu()
209 {
210
211 global $submenu;
212
213 add_menu_page( 'Vimeography Page Title', 'Vimeography', 'manage_options', 'vimeography-edit-galleries', '', VIMEOGRAPHY_URL.'media/img/vimeography-icon.png' );
214 add_submenu_page( 'vimeography-edit-galleries', 'Edit Galleries', 'Edit Galleries', 'manage_options', 'vimeography-edit-galleries', array(&$this, 'vimeography_render_template' ));
215 add_submenu_page( 'vimeography-edit-galleries', 'New Gallery', 'New Gallery', 'manage_options', 'vimeography-new-gallery', array(&$this, 'vimeography_render_template' ));
216 add_submenu_page( 'vimeography-edit-galleries', 'My Themes', 'My Themes', 'manage_options', 'vimeography-my-themes', array(&$this, 'vimeography_render_template' ));
217 $submenu['vimeography-edit-galleries'][500] = array( 'Buy Themes', 'manage_options' , 'http://vimeography.com/themes' );
218 add_submenu_page( 'vimeography-edit-galleries', 'Vimeography Pro', 'Vimeography Pro', 'manage_options', 'vimeography-pro', array(&$this, 'vimeography_render_template' ));
219 add_submenu_page( 'vimeography-edit-galleries', 'Help', 'Help', 'manage_options', 'vimeography-help', array(&$this, 'vimeography_render_template' ));
220
221 }
222
223 public function vimeography_render_template()
224 {
225 if ( !current_user_can( 'manage_options' ) ) {
226 wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
227 }
228
229 wp_register_style( 'bootstrap_css', VIMEOGRAPHY_URL.'media/css/bootstrap.min.css');
230 wp_register_style( 'bootstrap_responsive_css', VIMEOGRAPHY_URL.'media/css/bootstrap-responsive.min.css');
231 wp_register_style( 'vimeography-admin.css', VIMEOGRAPHY_URL.'media/css/admin.css');
232 wp_register_script( 'bootstrap_tab_js', VIMEOGRAPHY_URL.'media/js/bootstrap-tab.js');
233 wp_register_script( 'bootstrap_alert_js', VIMEOGRAPHY_URL.'media/js/bootstrap-alert.js');
234 wp_register_script( 'vimeography-admin.js', VIMEOGRAPHY_URL.'media/js/admin.js', 'jquery');
235
236 wp_enqueue_style( 'bootstrap_css');
237 wp_enqueue_style( 'bootstrap_responsive_css');
238 wp_enqueue_style( 'vimeography-admin.css');
239 wp_enqueue_script( 'jquery');
240 wp_enqueue_script( 'bootstrap_tab_js');
241 wp_enqueue_script( 'bootstrap_alert_js');
242 wp_enqueue_script( 'vimeography-admin.js');
243
244 switch(current_filter())
245 {
246 case 'vimeography_page_vimeography-new-gallery':
247 require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/gallery/new.php');
248 $mustache = new Vimeography_Gallery_New();
249 $template = $this->_load_template('gallery/new');
250 break;
251 case 'toplevel_page_vimeography-edit-galleries':
252 if (isset($_GET['id']))
253 {
254 require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/gallery/edit.php');
255 $mustache = new Vimeography_Gallery_Edit();
256 $template = $this->_load_template('gallery/edit');
257 }
258 else
259 {
260 require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/gallery/list.php');
261 $mustache = new Vimeography_Gallery_List();
262 $template = $this->_load_template('gallery/list');
263 }
264 break;
265 case 'vimeography_page_vimeography-my-themes':
266 require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/theme/list.php');
267 $mustache = new Vimeography_Theme_List();
268 $template = $this->_load_template('theme/list');
269 break;
270 case 'vimeography_page_vimeography-pro':
271 require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/vimeography/pro.php');
272 $mustache = new Vimeography_Pro();
273 $template = $this->_load_template('vimeography/pro');
274 break;
275 case 'vimeography_page_vimeography-help':
276 require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/vimeography/help.php');
277 $mustache = new Vimeography_Help();
278 $template = $this->_load_template('vimeography/help');
279 break;
280 default:
281 wp_die( __('The admin template for "'.current_filter().'" cannot be found.') );
282 break;
283 }
284 echo $mustache->render($template);
285 }
286
287 protected function _load_template($name)
288 {
289 $path = VIMEOGRAPHY_PATH . 'lib/admin/templates/' . $name .'.mustache';
290 if (! $result = @file_get_contents($path))
291 wp_die('The admin template "'.$name.'" cannot be found.');
292 return $result;
293 }
294
295 /**
296 * Create tables and define defaults when plugin is activated.
297 *
298 * @access public
299 * @return void
300 */
301 public function vimeography_update_tables() {
302 global $wpdb;
303
304 delete_option('vimeography_default_settings');
305 delete_option('vimeography_advanced_settings');
306
307 add_option('vimeography_advanced_settings', array(
308 'active' => FALSE,
309 'client_id' => '',
310 'client_secret' => '',
311 'access_token' => '',
312 'access_token_secret' => '',
313 ));
314
315 add_option('vimeography_default_settings', array(
316 'source_url' => 'https://vimeo.com/channels/staffpicks/',
317 'video_limit' => 20,
318 'featured_video' => '',
319 'cache_timeout' => 3600,
320 'theme_name' => 'bugsauce',
321 ));
322
323 $sql = 'CREATE TABLE '.VIMEOGRAPHY_GALLERY_TABLE.' (
324 id mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
325 title varchar(150) NOT NULL,
326 date_created datetime NOT NULL,
327 is_active tinyint(1) NOT NULL,
328 PRIMARY KEY (id)
329 );
330 CREATE TABLE '.VIMEOGRAPHY_GALLERY_META_TABLE.' (
331 id mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
332 gallery_id mediumint(8) unsigned NOT NULL,
333 source_url varchar(100) NOT NULL,
334 video_limit mediumint(7) NOT NULL,
335 featured_video int(9) unsigned DEFAULT NULL,
336 cache_timeout mediumint(7) NOT NULL,
337 theme_name varchar(50) NOT NULL,
338 PRIMARY KEY (id)
339 );
340 ';
341
342 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
343 dbDelta($sql);
344
345 // Let's also move the bugsauce folder to the vimeography_theme_path (wp-content/uploads)
346 $this->_move_theme(array('source' => VIMEOGRAPHY_PATH . 'bugsauce/', 'destination' => VIMEOGRAPHY_THEME_PATH.'bugsauce/', 'clear_destination' => true, 'clear_working' => true));
347 $this->_move_theme(array('source' => VIMEOGRAPHY_PATH . 'theme-assets/', 'destination' => VIMEOGRAPHY_ASSETS_PATH, 'clear_destination' => true, 'clear_working' => true));
348
349 }
350
351 /**
352 * Read the shortcode and return the output.
353 * example:
354 * [vimeography from='user' named='davekiss' theme='apple']
355 *
356 * @access public
357 * @param mixed $atts
358 * @return void
359 */
360 public function vimeography_shortcode($atts)
361 {
362
363 // Let's get the data for this gallery from the db
364 if (intval($atts['id']))
365 {
366 global $wpdb;
367 $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;');
368 }
369
370 // Get admin panel options
371 $default_settings = get_option('vimeography_default_settings');
372
373 $gallery_settings['theme'] = isset($gallery_info[0]->theme_name) ? $gallery_info[0]->theme_name : $default_settings['theme_name'];
374 $gallery_settings['featured'] = isset($gallery_info[0]->featured_video) ? $gallery_info[0]->featured_video : $default_settings['featured_video'];
375 $gallery_settings['source'] = isset($gallery_info[0]->source_url) ? $gallery_info[0]->source_url : $default_settings['source_url'];
376 $gallery_settings['limit'] = isset($gallery_info[0]->video_limit) ? $gallery_info[0]->video_limit : $default_settings['video_limit'];
377 $gallery_settings['cache'] = isset($gallery_info[0]->cache_timeout) ? $gallery_info[0]->cache_timeout : $default_settings['cache_timeout'];
378
379 // Get shortcode attributes
380 $settings = shortcode_atts( array(
381 'id' => '',
382 'theme' => $gallery_settings['theme'],
383 'featured' => $gallery_settings['featured'],
384 'source' => $gallery_settings['source'],
385 'limit' => $gallery_settings['limit'],
386 'cache' => $gallery_settings['cache'],
387 'width' => '',
388 'height' => '',
389 ), $atts );
390
391 try
392 {
393 require_once(VIMEOGRAPHY_PATH . 'lib/core.php');
394 $vimeography = Vimeography_Core::factory('videos', $settings);
395
396 // if cache is set, render it. otherwise, get the json, set the cache, and render it
397 if (($vimeography_data = $this->get_vimeography_cache($settings['id'])) === FALSE)
398 {
399 // cache not set, let's do a new request to the vimeo API and cache it
400 $vimeography_data = $vimeography->get('videos');
401 $transient = $this->set_vimeography_cache($settings['id'], $vimeography_data, $settings['cache']);
402 }
403 return $vimeography->render($vimeography_data);
404 }
405 catch (Vimeography_Exception $e)
406 {
407 return "Vimeography error: ".$e->getMessage();
408 }
409 }
410
411 /**
412 * Get the JSON data stored in the Vimeography cache for the provided gallery id.
413 *
414 * @access public
415 * @static
416 * @param mixed $id
417 * @return void
418 */
419 public static function get_vimeography_cache($id)
420 {
421 return FALSE === ( $vimeography_cache_results = get_transient( 'vimeography_cache_'.$id ) ) ? FALSE : $vimeography_cache_results;
422 }
423
424 /**
425 * Set the JSON data to the Vimeography cache for the provided gallery id.
426 *
427 * @access public
428 * @static
429 * @param mixed $id
430 * @param mixed $data
431 * @param mixed $cache_limit
432 * @return void
433 */
434 public static function set_vimeography_cache($id, $data, $cache_limit)
435 {
436 return set_transient( 'vimeography_cache_'.$id, $data, $cache_limit );
437 }
438
439 /**
440 * Clear the Vimeography cache for the provided gallery id.
441 *
442 * @access public
443 * @static
444 * @param mixed $id
445 * @return void
446 */
447 public static function delete_vimeography_cache($id)
448 {
449 return delete_transient('vimeography_cache_'.$id);
450 }
451
452 /**
453 * Moves the given folder to the given destination.
454 *
455 * @access private
456 * @param array $args (default: array())
457 * @return void
458 */
459 private function _move_theme($args = array())
460 {
461 // Replaces simple `WP_Filesystem();` call to prevent any extraction issues
462 // @link http://wpquestions.com/question/show/id/2685
463 if (! function_exists('__return_direct'))
464 {
465 function __return_direct() { return 'direct'; }
466 }
467
468 add_filter( 'filesystem_method', '__return_direct' );
469 WP_Filesystem();
470 remove_filter( 'filesystem_method', '__return_direct' );
471
472 global $wp_filesystem;
473 $defaults = array( 'source' => '', 'destination' => '', //Please always pass these
474 'clear_destination' => false, 'clear_working' => false,
475 'hook_extra' => array());
476
477 $args = wp_parse_args($args, $defaults);
478 extract($args);
479
480 @set_time_limit( 300 );
481
482 if ( empty($source) || empty($destination) )
483 return new WP_Error('bad_request', 'bad request.');
484
485 // $this->skin->feedback('installing_package');
486
487 //Retain the Original source and destinations
488 $remote_source = $source;
489 $local_destination = $destination;
490
491 if (! $wp_filesystem->dirlist($remote_source)) return FALSE;
492
493 $source_files = array_keys( $wp_filesystem->dirlist($remote_source) );
494 $remote_destination = $wp_filesystem->find_folder($local_destination);
495
496 //Locate which directory to copy to the new folder, This is based on the actual folder holding the files.
497 if ( 1 == count($source_files) && $wp_filesystem->is_dir( trailingslashit($source) . $source_files[0] . '/') ) //Only one folder? Then we want its contents.
498 $source = trailingslashit($source) . trailingslashit($source_files[0]);
499 elseif ( count($source_files) == 0 )
500 return new WP_Error( 'incompatible_archive', 'incompatible archive string', __( 'The plugin contains no files.' ) ); //There are no files?
501 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.
502 $source = trailingslashit($source);
503
504 //Has the source location changed? If so, we need a new source_files list.
505 if ( $source !== $remote_source )
506 $source_files = array_keys( $wp_filesystem->dirlist($source) );
507
508 if ( $clear_destination ) {
509 //We're going to clear the destination if there's something there
510 //$this->skin->feedback('remove_old');
511 $removed = true;
512 if ( $wp_filesystem->exists($remote_destination) )
513 $removed = $wp_filesystem->delete($remote_destination, true);
514 if ( is_wp_error($removed) )
515 return $removed;
516 else if ( ! $removed )
517 return new WP_Error('remove_old_failed', 'couldnt remove old');
518 } elseif ( $wp_filesystem->exists($remote_destination) ) {
519 //If we're not clearing the destination folder and something exists there already, Bail.
520 //But first check to see if there are actually any files in the folder.
521 $_files = $wp_filesystem->dirlist($remote_destination);
522 if ( ! empty($_files) ) {
523 $wp_filesystem->delete($remote_source, true); //Clear out the source files.
524 return new WP_Error('folder_exists', 'folder exists string', $remote_destination );
525 }
526 }
527
528 //Create themes folder, if needed
529 if ( !$wp_filesystem->exists(VIMEOGRAPHY_THEME_PATH) )
530 if ( !$wp_filesystem->mkdir(VIMEOGRAPHY_THEME_PATH, FS_CHMOD_DIR) )
531 return new WP_Error('mkdir_failed', 'mkdir failer string', $remote_destination);
532
533 //Create destination if needed
534 if ( !$wp_filesystem->exists($remote_destination) )
535 if ( !$wp_filesystem->mkdir($remote_destination, FS_CHMOD_DIR) )
536 return new WP_Error('mkdir_failed', 'mkdir failer string', $remote_destination);
537
538 // Copy new version of item into place.
539 $result = copy_dir($source, $remote_destination);
540
541 if ( is_wp_error($result) ) {
542 if ( $clear_working )
543 $wp_filesystem->delete($remote_source, true);
544 return $result;
545 }
546
547 //Clear the Working folder?
548 if ( $clear_working )
549 $wp_filesystem->delete($remote_source, true);
550
551 $destination_name = basename( str_replace($local_destination, '', $destination) );
552 if ( '.' == $destination_name )
553 $destination_name = '';
554
555 $result = compact('local_source', 'source', 'source_name', 'source_files', 'destination', 'destination_name', 'local_destination', 'remote_destination', 'clear_destination', 'delete_source_dir');
556
557 //Bombard the calling function will all the info which we've just used.
558 return $result;
559
560 }
561
562 }
563
564 new Vimeography;