version = time(); // ghost cache busters!
add_action( 'init', array( $this, 'on_init' ) );
add_action( 'jetpack_activate_module_videopress', array( $this, 'jetpack_module_activated' ) );
add_action( 'jetpack_deactivate_module_videopress', array( $this, 'jetpack_module_deactivated' ) );
}
/**
* Fires on init since is_connection_owner should wait until the user is initialized by $wp->init();
*/
function on_init() {
$options = $this->get_options();
// Only the connection owner can configure this module.
if ( $this->is_connection_owner() ) {
Jetpack::enable_module_configurable( $this->module );
Jetpack::module_configuration_load( $this->module, array( $this, 'jetpack_configuration_load' ) );
Jetpack::module_configuration_screen( $this->module, array( $this, 'jetpack_configuration_screen' ) );
}
// Only if the current user can manage the VideoPress library and one has been connected.
if ( $this->can( 'read_videos' ) && $options['blog_id'] ) {
add_action( 'wp_enqueue_media', array( $this, 'enqueue_admin_scripts' ) );
add_action( 'print_media_templates', array( $this, 'print_media_templates' ) );
// Load these at priority -1 so they're fired before Core's are.
add_action( 'wp_ajax_query-attachments', array( $this, 'wp_ajax_query_attachments' ), -1 );
add_action( 'wp_ajax_save-attachment', array( $this, 'wp_ajax_save_attachment' ), -1 );
add_action( 'wp_ajax_save-attachment-compat', array( $this, 'wp_ajax_save_attachment' ), -1 );
add_action( 'wp_ajax_delete-post', array( $this, 'wp_ajax_delete_post' ), -1 );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
}
if ( $this->can( 'upload_videos' ) && $options['blog_id'] ) {
add_action( 'wp_ajax_videopress-get-upload-token', array( $this, 'wp_ajax_videopress_get_upload_token' ) );
}
add_filter( 'videopress_shortcode_options', array( $this, 'videopress_shortcode_options' ) );
}
function wp_ajax_videopress_get_upload_token() {
if ( ! $this->can( 'upload_videos' ) )
return wp_send_json_error();
$result = $this->query( 'jetpack.vpGetUploadToken' );
if ( is_wp_error( $result ) )
return wp_send_json_error( array( 'message' => __( 'Could not obtain a VideoPress upload token. Please try again later.', 'jetpack' ) ) );
$response = $result;
if ( empty( $response['videopress_blog_id'] ) || empty( $response['videopress_token'] ) || empty( $response[ 'videopress_action_url' ] ) )
return wp_send_json_error( array( 'message' => __( 'Could not obtain a VideoPress upload token. Please try again later.', 'jetpack' ) ) );
return wp_send_json_success( $response );
}
/**
* Get VideoPress options
*/
function get_options() {
$defaults = array(
'blogs' => array(),
'blog_id' => 0,
'access' => '',
'allow-upload' => false,
'freedom' => false,
'hd' => false,
'meta' => array(
'max_upload_size' => 0,
),
);
$options = Jetpack_Options::get_option( $this->option_name, array() );
// If options have not been saved yet, check for older VideoPress plugin options.
if ( empty( $options ) ) {
$options['freedom'] = (bool) get_option( 'video_player_freedom', false );
$options['hd'] = (bool) get_option( 'video_player_high_quality', false );
}
$options = array_merge( $defaults, $options );
return $options;
}
/**
* Update VideoPress options
*/
function update_options( $options ) {
Jetpack_Options::update_option( $this->option_name, $options );
}
/**
* Runs when the VideoPress module is activated.
*/
function jetpack_module_activated() {
if ( ! $this->is_connection_owner() )
return;
$options = $this->get_options();
// Ask WordPress.com for a list of VideoPress blogs
$result = $this->query( 'jetpack.vpGetBlogs' );
if ( ! is_wp_error( $result ) )
$options['blogs'] = $result;
// If there's at least one available blog, let's use it.
if ( is_array( $options['blogs'] ) && count( $options['blogs'] ) > 0 )
$options['blog_id'] = $options['blogs'][0]['blog_id'];
$this->update_options( $options );
}
/**
* Runs when the VideoPress module is deactivated.
*/
function jetpack_module_deactivated() {
Jetpack_Options::delete_option( $this->option_name );
}
/**
* Remote Query
*
* Performs a remote XML-RPC query using Jetpack's IXR Client. And also
* appends some useful stuff about this setup to the query.
*
* @return the Jetpack_IXR_Client object after querying.
*/
function query( $method, $args = null ) {
$options = $this->get_options();
Jetpack::load_xml_rpc_client();
$xml = new Jetpack_IXR_Client( array(
'user_id' => JETPACK_MASTER_USER, // All requests are on behalf of the connection owner.
) );
$params = array(
'args' => $args,
'video_blog_id' => $options['blog_id'],
'caps' => array(),
);
// Let Jetpack know about our local caps.
foreach ( array( 'read_videos', 'edit_videos', 'delete_videos', 'upload_videos' ) as $cap )
if ( $this->can( $cap ) )
$params['caps'][] = $cap;
$xml->query( $method, $params );
if ( $xml->isError() )
return new WP_Error( 'xml_rpc_error', 'An XML-RPC error has occurred.' );
$response = $xml->getResponse();
// If there's any metadata with the response, save it for future use.
if ( is_array( $response ) && isset( $response['meta'] ) ) {
$options = $this->get_options();
if ( $response['meta'] !== $options['meta'] ) {
$options['meta'] = array_merge( $options['meta'], $response['meta'] );
$this->update_options( $options );
}
}
if ( is_array( $response ) && isset( $response['result'] ) )
return $response['result'];
return $response;
}
/**
* Runs before the VideoPress Configuration screen loads, useful
* to update options and yield errors.
*/
function jetpack_configuration_load() {
$this->enqueue_admin_scripts();
/**
* Save configuration
*/
if ( ! empty( $_POST['action'] ) && $_POST['action'] == 'videopress-save' ) {
check_admin_referer( 'videopress-settings' );
$options = $this->get_options();
if ( isset( $_POST['blog_id'] ) && in_array( $_POST['blog_id'], wp_list_pluck( $options['blogs'], 'blog_id' ) ) )
$options['blog_id'] = $_POST['blog_id'];
// Allow the None setting too.
if ( isset( $_POST['blog_id'] ) && $_POST['blog_id'] == 0 )
$options['blog_id'] = 0;
/**
* @see $this->can()
*/
if ( isset( $_POST['videopress-access'] ) && in_array( $_POST['videopress-access'], array( '', 'read', 'edit', 'delete' ) ) )
$options['access'] = $_POST['videopress-access'];
$options['freedom'] = isset( $_POST['videopress-freedom'] );
$options['hd'] = isset( $_POST['videopress-hd'] );
// Allow upload only if some level of access has been granted, and uploads were allowed.
$options['allow-upload'] = false;
if ( ! empty( $options['access'] ) && isset( $_POST['videopress-upload'] ) )
$options['allow-upload'] = true;
$this->update_options( $options );
Jetpack::state( 'message', 'module_configured' );
wp_safe_redirect( Jetpack::module_configuration_url( $this->module ) );
}
/**
* Refresh the list of available WordPress.com blogs
*/
if ( ! empty( $_GET['videopress'] ) && $_GET['videopress'] == 'refresh-blogs' ) {
check_admin_referer( 'videopress-settings' );
$options = $this->get_options();
$result = $this->query( 'jetpack.vpGetBlogs' );
if ( ! is_wp_error( $result ) ) {
$options['blogs'] = $result;
$this->update_options( $options );
}
wp_safe_redirect( Jetpack::module_configuration_url( $this->module ) );
}
}
/**
* Renders the VideoPress Configuration screen in Jetpack.
*/
function jetpack_configuration_screen() {
$options = $this->get_options();
$refresh_url = wp_nonce_url( add_query_arg( 'videopress', 'refresh-blogs' ), 'videopress-settings' );
?>
Add Media button in the post editor.', 'jetpack' ); ?>