| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly |
| 4 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 5 |
|
| 6 |
/** |
| 7 |
* Controller for the New Gallery admin page. |
| 8 |
* |
| 9 |
* @extends Vimeography_Base |
| 10 |
*/ |
| 11 |
class Vimeography_Gallery_New extends Vimeography_Base { |
| 12 |
|
| 13 |
/** |
| 14 |
* Vimeo library instance |
| 15 |
* |
| 16 |
* @var singleton |
| 17 |
*/ |
| 18 |
private $_vimeo; |
| 19 |
|
| 20 |
/** |
| 21 |
* The default client ID OR the user's access token. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
private $_token; |
| 26 |
|
| 27 |
/** |
| 28 |
* [$_gallery_id description] |
| 29 |
* @var [type] |
| 30 |
*/ |
| 31 |
protected $_gallery_id; |
| 32 |
|
| 33 |
/** |
| 34 |
* [__construct description] |
| 35 |
*/ |
| 36 |
public function __construct() { |
| 37 |
if ( ( $this->_token = get_option('vimeography_pro_access_token') ) === FALSE ) : |
| 38 |
$this->_vimeo = new \Vimeography\Vimeo( NULL, NULL, VIMEOGRAPHY_ACCESS_TOKEN ); |
| 39 |
else : |
| 40 |
$this->_vimeo = new \Vimeography\Vimeo( NULL, NULL, $this->_token ); |
| 41 |
endif; |
| 42 |
|
| 43 |
if ( isset( $_POST['vimeography_new_gallery_settings'] ) ) { |
| 44 |
try { |
| 45 |
$input = $this->_validate_form( $_POST['vimeography_new_gallery_settings'] ); |
| 46 |
$this->_gallery_id = self::_create_vimeography_gallery($input); |
| 47 |
|
| 48 |
do_action('vimeography-pro/create-gallery', $this->_gallery_id); |
| 49 |
|
| 50 |
//$trigger = $this->_vimeography_subscribe_to_trigger($input['resource_uri'], $gallery_id); |
| 51 |
|
| 52 |
if ( isset( $_GET['redirect'] ) && $_GET['redirect'] == 'welcome' ) { |
| 53 |
wp_redirect( get_admin_url().'options.php?page=vimeography-welcome&step=2&id=' . $this->_gallery_id ); |
| 54 |
} else { |
| 55 |
wp_redirect( get_admin_url().'admin.php?page=vimeography-edit-galleries&id=' . $this->_gallery_id . '&created=1' ); |
| 56 |
} |
| 57 |
|
| 58 |
exit; |
| 59 |
} catch (Vimeography_Exception $e) { |
| 60 |
require_once(ABSPATH . 'wp-admin/admin-header.php'); |
| 61 |
$this->messages[] = array( |
| 62 |
'type' => 'error', |
| 63 |
'heading' => __('Heads up!', 'vimeography'), |
| 64 |
'message' => $e->getMessage() |
| 65 |
); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Returns several security form fields for the new gallery form. |
| 72 |
* |
| 73 |
* @access public |
| 74 |
* @return mixed |
| 75 |
*/ |
| 76 |
public function nonce() { |
| 77 |
return wp_nonce_field('vimeography-gallery-action','vimeography-gallery-verification'); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Checks the incoming form to make sure it is completed. |
| 82 |
* |
| 83 |
* @access protected |
| 84 |
* @return array $input |
| 85 |
*/ |
| 86 |
protected function _validate_form($input) { |
| 87 |
if ( check_admin_referer('vimeography-gallery-action','vimeography-gallery-verification') ) { |
| 88 |
if ( empty( $input['gallery_title'] ) || empty( $input['source_url'] ) ) { |
| 89 |
throw new Vimeography_Exception( __('Make sure you fill out all of the fields below!', 'vimeography') ); |
| 90 |
} |
| 91 |
|
| 92 |
$themes = array_map( array($this, 'get_theme_name'), $this->themes() ); |
| 93 |
|
| 94 |
if ( ! in_array( $input['gallery_theme'], $themes ) ) { |
| 95 |
throw new Vimeography_Exception( __('The theme you are trying to use is not installed or activated.', 'vimeography') ); |
| 96 |
} |
| 97 |
|
| 98 |
$input['gallery_theme'] == strtolower( sanitize_text_field( $input['gallery_theme'] ) ); |
| 99 |
$input['resource_uri'] = Vimeography::validate_vimeo_source( $input['source_url'] ); |
| 100 |
return $input; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Maps the Vimeography themes array above and fetches only the name for each theme. |
| 106 |
* |
| 107 |
* @since 1.4.2 |
| 108 |
* @param array $theme |
| 109 |
* @return string Theme name |
| 110 |
*/ |
| 111 |
private function get_theme_name($theme) { |
| 112 |
return $theme['name']; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Creates a new gallery entry in the database. |
| 117 |
* |
| 118 |
* @access private |
| 119 |
* @static |
| 120 |
* @return int gallery ID if success, exception if failure |
| 121 |
*/ |
| 122 |
private static function _create_vimeography_gallery($input) { |
| 123 |
global $wpdb; |
| 124 |
|
| 125 |
$result = $wpdb->insert( $wpdb->vimeography_gallery, array( 'title' => $input['gallery_title'], 'date_created' => current_time('mysql'), 'is_active' => 1 ) ); |
| 126 |
|
| 127 |
if (! $result) { |
| 128 |
throw new Vimeography_Exception( |
| 129 |
__("We couldn't create a new gallery. Try upgrading or reinstalling the Vimeography plugin.", 'vimeography') |
| 130 |
); |
| 131 |
} else { |
| 132 |
$gallery_id = $wpdb->insert_id; |
| 133 |
$theme = apply_filters('vimeography.gallery.new.theme', $input['gallery_theme'] ); |
| 134 |
|
| 135 |
$result = $wpdb->insert( $wpdb->vimeography_gallery_meta, array( |
| 136 |
'gallery_id' => $gallery_id, |
| 137 |
'source_url' => $input['source_url'], |
| 138 |
'resource_uri' => $input['resource_uri'], |
| 139 |
'featured_video' => NULL, |
| 140 |
'gallery_width' => NULL, |
| 141 |
'video_limit' => 25, |
| 142 |
'cache_timeout' => 3600, |
| 143 |
'theme_name' => $theme ) ); |
| 144 |
|
| 145 |
if (! $result) { |
| 146 |
throw new Vimeography_Exception( |
| 147 |
__("We couldn't create a new gallery. Try upgrading or reinstalling the Vimeography plugin.", 'vimeography') |
| 148 |
); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return $gallery_id; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* When the user enters the source location when creating a new gallery. |
| 157 |
* |
| 158 |
* Won't work publically yet, because the user needs to be authenticated to subscribe to push notifications. |
| 159 |
* Also, does not currently work with albums. |
| 160 |
* |
| 161 |
* @return [type] [description] |
| 162 |
*/ |
| 163 |
private function _vimeography_subscribe_to_trigger($resource, $gallery_id) { |
| 164 |
|
| 165 |
$callback = network_site_url() . '/vimeography/' . $gallery_id . '/refresh/'; |
| 166 |
|
| 167 |
$response = $this->_vimeo->request( '/triggers', array( |
| 168 |
'actions' => 'added, removed', |
| 169 |
'callback' => $callback, |
| 170 |
'resource_uri' => $resource .'/videos' |
| 171 |
), 'POST' ); |
| 172 |
|
| 173 |
echo '<pre>'; |
| 174 |
var_dump($response); |
| 175 |
echo '</pre>'; |
| 176 |
die; |
| 177 |
|
| 178 |
switch ($response['status']) { |
| 179 |
case 201: |
| 180 |
//successful |
| 181 |
return TRUE; |
| 182 |
break; |
| 183 |
case 403: |
| 184 |
if ($this->_token === FALSE) : |
| 185 |
// Trigger unsuccessful, rely on 304 headers. |
| 186 |
break; |
| 187 |
// This line will only work when the Vimeo API supports triggers without being authenticated |
| 188 |
// Though, the user could technically be subscribing to a collection that isn't actually supported in PRO, either. |
| 189 |
// So be specific in which sources are currently supported. |
| 190 |
//throw new Vimeography_Exception('Vimeography PRO allows you to show videos from all of your users, channels, albums, & groups.'); |
| 191 |
else: |
| 192 |
throw new Vimeography_Exception(__("Looks like you don't have the permission to subscribe to this collection.", 'vimeography')); |
| 193 |
endif; |
| 194 |
break; |
| 195 |
case 405: case 500: |
| 196 |
// Unsupported container uri |
| 197 |
throw new Vimeography_Exception(__('The resource that was entered is currently unsupported.', 'vimeography') ); |
| 198 |
break; |
| 199 |
default: |
| 200 |
throw new Vimeography_Exception( serialize($response) ); |
| 201 |
break; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
} |
| 206 |
|