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

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