| 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.1 |
| 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_BASENAME', plugin_basename( __FILE__ ) ); |
| 24 |
define( 'VIMEOGRAPHY_VERSION', '0.5'); |
| 25 |
define( 'VIMEOGRAPHY_GALLERY_TABLE', $wpdb->prefix . "vimeography_gallery"); |
| 26 |
define( 'VIMEOGRAPHY_GALLERY_META_TABLE', $wpdb->prefix . "vimeography_gallery_meta"); |
| 27 |
define( 'VIMEOGRAPHY_CURRENT_PAGE', basename($_SERVER['PHP_SELF'])); |
| 28 |
|
| 29 |
require_once(VIMEOGRAPHY_PATH . '/vendor/mustache/Mustache.php'); |
| 30 |
|
| 31 |
class Vimeography |
| 32 |
{ |
| 33 |
public function __construct() |
| 34 |
{ |
| 35 |
add_action( 'init', array(&$this, 'vimeography_init') ); |
| 36 |
add_action( 'admin_init', array(&$this, 'vimeography_requires_wordpress_version') ); |
| 37 |
add_action( 'plugins_loaded', array(&$this, 'vimeography_update_db_check') ); |
| 38 |
add_action( 'admin_menu', array(&$this, 'vimeography_add_menu') ); |
| 39 |
|
| 40 |
register_activation_hook( VIMEOGRAPHY_BASENAME, array(&$this, 'vimeography_create_tables') ); |
| 41 |
|
| 42 |
add_filter( 'plugin_action_links', array(&$this, 'vimeography_filter_plugin_actions'), 10, 2 ); |
| 43 |
add_shortcode( 'vimeography', array(&$this, 'vimeography_shortcode') ); |
| 44 |
|
| 45 |
// Add shortcode support for widgets |
| 46 |
add_filter( 'widget_text', 'do_shortcode' ); |
| 47 |
} |
| 48 |
|
| 49 |
public function vimeography_init() |
| 50 |
{ |
| 51 |
if(in_array(VIMEOGRAPHY_CURRENT_PAGE, array('post.php', 'page.php', 'page-new.php', 'post-new.php'))){ |
| 52 |
add_action('admin_footer', array(&$this, 'vimeography_add_mce_popup')); |
| 53 |
} |
| 54 |
|
| 55 |
if ( get_user_option('rich_editing') == 'true' ) { |
| 56 |
add_filter( 'mce_external_plugins', array(&$this, 'vimeography_add_editor_plugin' )); |
| 57 |
add_filter( 'mce_buttons', array(&$this, 'vimeography_register_editor_button') ); |
| 58 |
} |
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
public function vimeography_register_editor_button($buttons) |
| 63 |
{ |
| 64 |
array_push( $buttons, "|", "vimeography" ); |
| 65 |
return $buttons; |
| 66 |
} |
| 67 |
|
| 68 |
public function vimeography_add_editor_plugin( $plugin_array ) { |
| 69 |
$plugin_array['vimeography'] = VIMEOGRAPHY_URL . 'media/js/mce.js'; |
| 70 |
return $plugin_array; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Check the wordpress version is compatible, and disable plugin if not. |
| 75 |
* |
| 76 |
* @access public |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
public function vimeography_requires_wordpress_version() { |
| 80 |
global $wp_version; |
| 81 |
$plugin = plugin_basename( __FILE__ ); |
| 82 |
$plugin_data = get_plugin_data( __FILE__, false ); |
| 83 |
|
| 84 |
if ( version_compare($wp_version, "3.3", "<" ) ) { |
| 85 |
if( is_plugin_active($plugin) ) { |
| 86 |
deactivate_plugins( $plugin ); |
| 87 |
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>." ); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Check if the Vimeography database needs updated based on the db version. |
| 94 |
* |
| 95 |
* @access public |
| 96 |
* @return void |
| 97 |
*/ |
| 98 |
public function vimeography_update_db_check() |
| 99 |
{ |
| 100 |
if (get_option('vimeography_db_version') != VIMEOGRAPHY_VERSION) |
| 101 |
$this->vimeography_create_tables(); |
| 102 |
|
| 103 |
update_option('vimeography_db_version', VIMEOGRAPHY_VERSION); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Add Settings link to "installed plugins" admin page. |
| 108 |
* |
| 109 |
* @access public |
| 110 |
* @param mixed $links |
| 111 |
* @param mixed $file |
| 112 |
* @return void |
| 113 |
*/ |
| 114 |
public function vimeography_filter_plugin_actions($links, $file) |
| 115 |
{ |
| 116 |
if ( $file == VIMEOGRAPHY_BASENAME ) |
| 117 |
{ |
| 118 |
$settings_link = '<a href="admin.php?page=vimeography-edit-galleries">' . __('Settings') . '</a>'; |
| 119 |
if (!in_array($settings_link, $links)) |
| 120 |
array_unshift( $links, $settings_link ); // before other links |
| 121 |
} |
| 122 |
return $links; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Action target that displays the popup to insert a form to a post/page. |
| 127 |
* |
| 128 |
* @access public |
| 129 |
* @return void |
| 130 |
*/ |
| 131 |
public function vimeography_add_mce_popup(){ |
| 132 |
require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/vimeography/mce.php'); |
| 133 |
$mustache = new Vimeography_MCE(); |
| 134 |
$template = $this->_load_template('vimeography/mce'); |
| 135 |
echo $mustache->render($template); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Adds a new top level menu to the admin menu. |
| 140 |
* |
| 141 |
* @access public |
| 142 |
* @return void |
| 143 |
*/ |
| 144 |
public function vimeography_add_menu() |
| 145 |
{ |
| 146 |
|
| 147 |
global $submenu; |
| 148 |
|
| 149 |
add_menu_page( 'Vimeography Page Title', 'Vimeography', 'manage_options', 'vimeography-edit-galleries', '', VIMEOGRAPHY_URL.'media/img/vimeography-icon.png' ); |
| 150 |
add_submenu_page( 'vimeography-edit-galleries', 'Edit Galleries', 'Edit Galleries', 'manage_options', 'vimeography-edit-galleries', array(&$this, 'vimeography_render_template' )); |
| 151 |
add_submenu_page( 'vimeography-edit-galleries', 'New Gallery', 'New Gallery', 'manage_options', 'vimeography-new-gallery', array(&$this, 'vimeography_render_template' )); |
| 152 |
add_submenu_page( 'vimeography-edit-galleries', 'My Themes', 'My Themes', 'manage_options', 'vimeography-my-themes', array(&$this, 'vimeography_render_template' )); |
| 153 |
$submenu['vimeography-edit-galleries'][500] = array( 'Buy Themes', 'manage_options' , 'http://vimeography.com/themes' ); |
| 154 |
add_submenu_page( 'vimeography-edit-galleries', 'Vimeography Pro', 'Vimeography Pro', 'manage_options', 'vimeography-pro', array(&$this, 'vimeography_render_template' )); |
| 155 |
add_submenu_page( 'vimeography-edit-galleries', 'Help', 'Help', 'manage_options', 'vimeography-help', array(&$this, 'vimeography_render_template' )); |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
public function vimeography_render_template() |
| 160 |
{ |
| 161 |
if ( !current_user_can( 'manage_options' ) ) { |
| 162 |
wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); |
| 163 |
} |
| 164 |
|
| 165 |
wp_register_style( 'bootstrap_css', VIMEOGRAPHY_URL.'media/css/bootstrap.min.css'); |
| 166 |
wp_register_style( 'bootstrap_responsive_css', VIMEOGRAPHY_URL.'media/css/bootstrap-responsive.min.css'); |
| 167 |
wp_register_style( 'vimeography-admin.css', VIMEOGRAPHY_URL.'media/css/admin.css'); |
| 168 |
wp_register_script( 'bootstrap_tab_js', VIMEOGRAPHY_URL.'media/js/bootstrap-tab.js'); |
| 169 |
wp_register_script( 'bootstrap_alert_js', VIMEOGRAPHY_URL.'media/js/bootstrap-alert.js'); |
| 170 |
wp_register_script( 'vimeography-admin.js', VIMEOGRAPHY_URL.'media/js/admin.js', 'jquery'); |
| 171 |
|
| 172 |
wp_enqueue_style( 'bootstrap_css'); |
| 173 |
wp_enqueue_style( 'bootstrap_responsive_css'); |
| 174 |
wp_enqueue_style( 'vimeography-admin.css'); |
| 175 |
wp_enqueue_script( 'jquery'); |
| 176 |
wp_enqueue_script( 'bootstrap_tab_js'); |
| 177 |
wp_enqueue_script( 'bootstrap_alert_js'); |
| 178 |
wp_enqueue_script( 'vimeography-admin.js'); |
| 179 |
|
| 180 |
switch(current_filter()) |
| 181 |
{ |
| 182 |
case 'vimeography_page_vimeography-new-gallery': |
| 183 |
require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/gallery/new.php'); |
| 184 |
$mustache = new Vimeography_Gallery_New(); |
| 185 |
$template = $this->_load_template('gallery/new'); |
| 186 |
break; |
| 187 |
case 'toplevel_page_vimeography-edit-galleries': |
| 188 |
if (isset($_GET['id'])) |
| 189 |
{ |
| 190 |
require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/gallery/edit.php'); |
| 191 |
$mustache = new Vimeography_Gallery_Edit(); |
| 192 |
$template = $this->_load_template('gallery/edit'); |
| 193 |
} |
| 194 |
else |
| 195 |
{ |
| 196 |
require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/gallery/list.php'); |
| 197 |
$mustache = new Vimeography_Gallery_List(); |
| 198 |
$template = $this->_load_template('gallery/list'); |
| 199 |
} |
| 200 |
break; |
| 201 |
case 'vimeography_page_vimeography-my-themes': |
| 202 |
require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/theme/list.php'); |
| 203 |
$mustache = new Vimeography_Theme_List(); |
| 204 |
$template = $this->_load_template('theme/list'); |
| 205 |
break; |
| 206 |
case 'vimeography_page_vimeography-pro': |
| 207 |
require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/vimeography/pro.php'); |
| 208 |
$mustache = new Vimeography_Pro(); |
| 209 |
$template = $this->_load_template('vimeography/pro'); |
| 210 |
break; |
| 211 |
case 'vimeography_page_vimeography-help': |
| 212 |
require_once(VIMEOGRAPHY_PATH . 'lib/admin/view/vimeography/help.php'); |
| 213 |
$mustache = new Vimeography_Help(); |
| 214 |
$template = $this->_load_template('vimeography/help'); |
| 215 |
break; |
| 216 |
default: |
| 217 |
wp_die( __('The admin template for "'.current_filter().'" cannot be found.') ); |
| 218 |
break; |
| 219 |
} |
| 220 |
echo $mustache->render($template); |
| 221 |
} |
| 222 |
|
| 223 |
protected function _load_template($name) |
| 224 |
{ |
| 225 |
$path = VIMEOGRAPHY_PATH . 'lib/admin/templates/' . $name .'.mustache'; |
| 226 |
if (! $result = @file_get_contents($path)) |
| 227 |
wp_die('The admin template "'.$name.'" cannot be found.'); |
| 228 |
return $result; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Create tables and define defaults when plugin is activated. |
| 233 |
* |
| 234 |
* @access public |
| 235 |
* @return void |
| 236 |
*/ |
| 237 |
public function vimeography_create_tables() { |
| 238 |
global $wpdb; |
| 239 |
|
| 240 |
delete_option('vimeography_default_settings'); |
| 241 |
delete_option('vimeography_advanced_settings'); |
| 242 |
|
| 243 |
add_option('vimeography_advanced_settings', array( |
| 244 |
'active' => FALSE, |
| 245 |
'client_id' => '', |
| 246 |
'client_secret' => '', |
| 247 |
'access_token' => '', |
| 248 |
'access_token_secret' => '', |
| 249 |
)); |
| 250 |
|
| 251 |
add_option('vimeography_default_settings', array( |
| 252 |
'source_type' => 'channel', |
| 253 |
'source_name' => 'hd', |
| 254 |
'video_count' => 20, |
| 255 |
'featured_video' => '', |
| 256 |
'cache_timeout' => 3600, |
| 257 |
'theme_name' => 'bugsauce', |
| 258 |
)); |
| 259 |
|
| 260 |
$sql = 'CREATE TABLE '.VIMEOGRAPHY_GALLERY_TABLE.' ( |
| 261 |
id mediumint(8) unsigned NOT NULL AUTO_INCREMENT, |
| 262 |
title varchar(150) NOT NULL, |
| 263 |
date_created datetime NOT NULL, |
| 264 |
is_active tinyint(1) NOT NULL, |
| 265 |
PRIMARY KEY (id) |
| 266 |
); |
| 267 |
CREATE TABLE '.VIMEOGRAPHY_GALLERY_META_TABLE.' ( |
| 268 |
id mediumint(8) unsigned NOT NULL AUTO_INCREMENT, |
| 269 |
gallery_id mediumint(8) unsigned NOT NULL, |
| 270 |
source_type varchar(50) NOT NULL, |
| 271 |
source_name varchar(50) NOT NULL, |
| 272 |
video_count mediumint(7) NOT NULL, |
| 273 |
featured_video int(9) unsigned DEFAULT NULL, |
| 274 |
cache_timeout mediumint(7) NOT NULL, |
| 275 |
theme_name varchar(50) NOT NULL, |
| 276 |
PRIMARY KEY (id) |
| 277 |
); |
| 278 |
'; |
| 279 |
|
| 280 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 281 |
dbDelta($sql); |
| 282 |
add_option("vimeography_db_version", VIMEOGRAPHY_VERSION); |
| 283 |
|
| 284 |
// Let's also move the vimeography_themes folder to wp-content/uploads |
| 285 |
$this->rcopy(VIMEOGRAPHY_PATH . 'vimeography-themes/' , VIMEOGRAPHY_THEME_PATH ); |
| 286 |
|
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Read the shortcode and return the output. |
| 291 |
* example: |
| 292 |
* [vimeography from='user' named='davekiss' theme='apple'] |
| 293 |
* |
| 294 |
* @access public |
| 295 |
* @param mixed $atts |
| 296 |
* @return void |
| 297 |
*/ |
| 298 |
public function vimeography_shortcode($atts) |
| 299 |
{ |
| 300 |
|
| 301 |
// Get admin panel options |
| 302 |
$default_settings = get_option('vimeography_default_settings'); |
| 303 |
|
| 304 |
// Get shortcode attributes |
| 305 |
$settings = shortcode_atts( array( |
| 306 |
'id' => '', |
| 307 |
'theme' => $default_settings['theme_name'], |
| 308 |
'featured' => $default_settings['featured_video'], |
| 309 |
'from' => $default_settings['source_type'], |
| 310 |
'named' => $default_settings['source_name'], |
| 311 |
'limit' => $default_settings['video_count'], |
| 312 |
'cache' => $default_settings['cache_timeout'], |
| 313 |
'width' => '', |
| 314 |
'height' => '', |
| 315 |
), $atts ); |
| 316 |
|
| 317 |
if (intval($settings['id'])) |
| 318 |
{ |
| 319 |
global $wpdb; |
| 320 |
$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;'); |
| 321 |
if ($gallery_info) |
| 322 |
{ |
| 323 |
$settings['theme'] = $gallery_info[0]->theme_name; |
| 324 |
$settings['featured'] = $gallery_info[0]->featured_video; |
| 325 |
$settings['from'] = $gallery_info[0]->source_type; |
| 326 |
$settings['named'] = $gallery_info[0]->source_name; |
| 327 |
$settings['limit'] = $gallery_info[0]->video_count; |
| 328 |
$settings['cache'] = $gallery_info[0]->cache_timeout; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
try |
| 333 |
{ |
| 334 |
require_once(VIMEOGRAPHY_PATH . 'lib/core.php'); |
| 335 |
$vimeography = Vimeography_Core::factory('videos', $settings); |
| 336 |
|
| 337 |
// if cache is set, render it. otherwise, get the json, set the cache, and render it |
| 338 |
if (($vimeography_data = $this->get_vimeography_cache($settings['id'])) === FALSE) |
| 339 |
{ |
| 340 |
// cache not set, let's do a new request to the vimeo API and cache it |
| 341 |
$vimeography_data = $vimeography->get('videos'); |
| 342 |
$transient = $this->set_vimeography_cache($settings['id'], $vimeography_data, $settings['cache']); |
| 343 |
} |
| 344 |
return $vimeography->render($vimeography_data); |
| 345 |
} |
| 346 |
catch (Vimeography_Exception $e) |
| 347 |
{ |
| 348 |
return "Vimeography error: ".$e->getMessage(); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* Get the JSON data stored in the Vimeography cache for the provided gallery id. |
| 354 |
* |
| 355 |
* @access public |
| 356 |
* @static |
| 357 |
* @param mixed $id |
| 358 |
* @return void |
| 359 |
*/ |
| 360 |
public static function get_vimeography_cache($id) |
| 361 |
{ |
| 362 |
return FALSE === ( $vimeography_cache_results = get_transient( 'vimeography_cache_'.$id ) ) ? FALSE : $vimeography_cache_results; |
| 363 |
} |
| 364 |
|
| 365 |
/** |
| 366 |
* Set the JSON data to the Vimeography cache for the provided gallery id. |
| 367 |
* |
| 368 |
* @access public |
| 369 |
* @static |
| 370 |
* @param mixed $id |
| 371 |
* @param mixed $data |
| 372 |
* @param mixed $cache_limit |
| 373 |
* @return void |
| 374 |
*/ |
| 375 |
public static function set_vimeography_cache($id, $data, $cache_limit) |
| 376 |
{ |
| 377 |
return set_transient( 'vimeography_cache_'.$id, $data, $cache_limit ); |
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
* Clear the Vimeography cache for the provided gallery id. |
| 382 |
* |
| 383 |
* @access public |
| 384 |
* @static |
| 385 |
* @param mixed $id |
| 386 |
* @return void |
| 387 |
*/ |
| 388 |
public static function delete_vimeography_cache($id) |
| 389 |
{ |
| 390 |
return delete_transient('vimeography_cache_'.$id); |
| 391 |
} |
| 392 |
|
| 393 |
// Function to remove folders and files |
| 394 |
private function rrmdir($dir) { |
| 395 |
if (is_dir($dir)) { |
| 396 |
$files = scandir($dir); |
| 397 |
foreach ($files as $file) |
| 398 |
if ($file != "." && $file != "..") $this->rrmdir("$dir/$file"); |
| 399 |
rmdir($dir); |
| 400 |
} |
| 401 |
else if (file_exists($dir)) unlink($dir); |
| 402 |
} |
| 403 |
|
| 404 |
// Function to Copy folders and files |
| 405 |
private function rcopy($source, $dst) { |
| 406 |
if (file_exists ( $dst )) |
| 407 |
$this->rrmdir ( $dst ); |
| 408 |
if (is_dir ( $source )) |
| 409 |
{ |
| 410 |
mkdir ( $dst ); |
| 411 |
$files = scandir ( $source ); |
| 412 |
foreach ( $files as $file ) |
| 413 |
if ($file != "." && $file != "..") |
| 414 |
$this->rcopy ( $source.'/'.$file, $dst.'/'.$file ); |
| 415 |
} else if (file_exists ( $source )) |
| 416 |
copy ( $source, $dst ); |
| 417 |
$this->rrmdir($source); |
| 418 |
} |
| 419 |
|
| 420 |
} |
| 421 |
|
| 422 |
new Vimeography; |