1, 'set_featured' => 1, 'post_types' => array( 'post' ), 'custom_field' => '' ); function __construct() { // Activation and deactivation hooks register_activation_hook( VIDEO_THUMBNAILS_PATH . '/video-thumbnails.php', array( &$this, 'plugin_activation' ) ); register_deactivation_hook( VIDEO_THUMBNAILS_PATH . '/video-thumbnails.php', array( &$this, 'plugin_deactivation' ) ); // Set current options add_action( 'plugins_loaded', array( &$this, 'set_options' ) ); // Add options page to menu add_action( 'admin_menu', array( &$this, 'admin_menu' ) ); // Initialize options add_action( 'admin_init', array( &$this, 'initialize_options' ) ); // Custom field detection callback add_action( 'wp_ajax_video_thumbnail_custom_field_detection', array( &$this, 'custom_field_detection_callback' ) ); // Ajax clear all callback add_action( 'wp_ajax_clear_all_video_thumbnails', array( &$this, 'ajax_clear_all_callback' ) ); // Ajax test callbacks add_action( 'wp_ajax_video_thumbnail_provider_test', array( &$this, 'provider_test_callback' ) ); // Provider test add_action( 'wp_ajax_video_thumbnail_image_download_test', array( &$this, 'image_download_test_callback' ) ); // Saving media test add_action( 'wp_ajax_video_thumbnail_delete_test_images', array( &$this, 'delete_test_images_callback' ) ); // Delete test images add_action( 'wp_ajax_video_thumbnail_markup_detection_test', array( &$this, 'markup_detection_test_callback' ) ); // Markup input test // Admin scripts add_action( 'admin_enqueue_scripts', array( &$this, 'admin_scripts' ) ); // Add "Go Pro" call to action to settings footer add_action( 'video_thumbnails/settings_footer', array( 'Video_Thumbnails_Settings', 'settings_footer' ) ); } // Activation hook function plugin_activation() { add_option( 'video_thumbnails', $this->default_options ); } // Deactivation hook function plugin_deactivation() { delete_option( 'video_thumbnails' ); } // Set options & possibly upgrade function set_options() { // Get the current options from the database $options = get_option( 'video_thumbnails' ); // If there aren't any options, load the defaults if ( ! $options ) $options = $this->default_options; // Check if our options need upgrading $options = $this->upgrade_options( $options ); // Set the options class variable $this->options = $options; } function upgrade_options( $options ) { // Boolean for if options need updating $options_need_updating = false; // If there isn't a settings version we need to check for pre 2.0 settings if ( ! isset( $options['version'] ) ) { // Check for post type setting $post_types = get_option( 'video_thumbnails_post_types' ); // If there is a a post type option we know there should be others if ( $post_types !== false ) { $options['post_types'] = $post_types; delete_option( 'video_thumbnails_post_types' ); $options['save_media'] = get_option( 'video_thumbnails_save_media' ); delete_option( 'video_thumbnails_save_media' ); $options['set_featured'] = get_option( 'video_thumbnails_set_featured' ); delete_option( 'video_thumbnails_set_featured' ); $options['custom_field'] = get_option( 'video_thumbnails_custom_field' ); delete_option( 'video_thumbnails_custom_field' ); } // Updates the options version to 2.0 $options['version'] = '2.0'; $options_need_updating = true; } if ( version_compare( $options['version'], VIDEO_THUMBNAILS_VERSION, '<' ) ) { $options['version'] = VIDEO_THUMBNAILS_VERSION; $options_need_updating = true; } // Save options to database if they've been updated if ( $options_need_updating ) { update_option( 'video_thumbnails', $options ); } return $options; } function admin_menu() { add_options_page( __( 'Video Thumbnails Options', 'video-thumbnails' ), __( 'Video Thumbnails', 'video-thumbnails' ), 'manage_options', 'video_thumbnails', array( &$this, 'options_page' ) ); } function admin_scripts( $hook ) { if ( 'settings_page_video_thumbnails' == $hook ) { wp_enqueue_style( 'video-thumbnails-settings-css', plugins_url( '/css/settings.css', VIDEO_THUMBNAILS_PATH . '/video-thumbnails.php' ), false, VIDEO_THUMBNAILS_VERSION ); wp_enqueue_script( 'video_thumbnails_settings', plugins_url( 'js/settings.js' , VIDEO_THUMBNAILS_PATH . '/video-thumbnails.php' ), array( 'jquery' ), VIDEO_THUMBNAILS_VERSION ); wp_localize_script( 'video_thumbnails_settings', 'video_thumbnails_settings_language', array( 'detection_failed' => __( 'We were unable to find a video in the custom fields of your most recently updated post.', 'video-thumbnails' ), 'working' => __( 'Working...', 'video-thumbnails' ), 'retest' => __( 'Retest', 'video-thumbnails' ), 'ajax_error' => __( 'AJAX Error:', 'video-thumbnails' ), 'clear_all_confirmation' => __( 'Are you sure you want to clear all video thumbnails? This cannot be undone.', 'video-thumbnails' ), ) ); global $video_thumbnails; $provider_slugs = array(); foreach ( $video_thumbnails->providers as $provider ) { $provider_slugs[] = $provider->service_slug; } wp_localize_script( 'video_thumbnails_settings', 'video_thumbnails_provider_slugs', array( 'provider_slugs' => $provider_slugs ) ); } } function custom_field_detection_callback() { if ( current_user_can( 'manage_options' ) ) { echo $this->detect_custom_field(); } die(); } function detect_custom_field() { global $video_thumbnails; $latest_post = get_posts( array( 'posts_per_page' => 1, 'post_type' => $this->options['post_types'], 'orderby' => 'modified', ) ); $latest_post = $latest_post[0]; $custom = get_post_meta( $latest_post->ID ); foreach ( $custom as $name => $values ) { foreach ($values as $value) { if ( $video_thumbnails->get_first_thumbnail_url( $value ) ) { return $name; } } } } function ajax_clear_all_callback() { if ( !current_user_can( 'manage_options' ) ) die(); if ( wp_verify_nonce( $_POST['nonce'], 'clear_all_video_thumbnails' ) ) { global $wpdb; // Clear images from media library $media_library_items = get_posts( array( 'showposts' => -1, 'post_type' => 'attachment', 'meta_key' => 'video_thumbnail', 'meta_value' => '1', 'fields' => 'ids' ) ); foreach ( $media_library_items as $item ) { wp_delete_attachment( $item, true ); } echo '
✔ ' . sprintf( _n( '1 attachment deleted', '%s attachments deleted', count( $media_library_items ), 'video-thumbnails' ), count( $media_library_items ) ) . '
'; // Clear custom fields $custom_fields_cleared = $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key='_video_thumbnail'" ); echo '✔ ' . sprintf( _n( '1 custom field cleared', '%s custom fields cleared', $custom_fields_cleared, 'video-thumbnails' ), $custom_fields_cleared ) . '
'; } else { echo '✖ ' . __( 'Error: Could not verify nonce.', 'video-thumbnails' ) . '
'; } die(); } function get_file_hash( $url ) { $response = wp_remote_get( $url ); if( is_wp_error( $response ) ) { $result = false; } else { $result = md5( $response['body'] ); } return $result; } function provider_test_callback() { if ( !current_user_can( 'manage_options' ) ) die(); global $video_thumbnails; ?>' . $test_case['name'] . ' | '; $markup = apply_filters( 'the_content', $test_case['markup'] ); $result = $video_thumbnails->get_first_thumbnail_url( $markup ); if ( is_wp_error( $result ) ) { $error_string = $result->get_error_message(); echo '✗ ' . __( 'Failed', 'video-thumbnails' ) . ' | '; echo '';
echo ' ' . $error_string . ' | ';
$failed++;
} else {
$result_hash = false;
if ( $result == $test_case['expected'] ) {
$matched = true;
} else {
$result_hash = $this->get_file_hash( $result );
$matched = ( $result_hash == $test_case['expected_hash'] ? true : false );
}
if ( $matched ) {
echo '✔ ' . __( 'Passed', 'video-thumbnails' ) . ' | '; } else { echo '✗ ' . __( 'Failed', 'video-thumbnails' ) . ' | '; } echo '';
if ( $result ) {
echo '' . __( 'View Image', 'video-thumbnails' ) . '';
}
if ( $result_hash ) {
echo ' ' . $result_hash . ' ';
}
echo ' | ';
}
echo '
✔ ' . __( 'Attachment created', 'video-thumbnails' ) . '
'; echo '' . __( 'View in Media Library', 'video-thumbnails' ) . '
'; echo '' . __( 'View full size', 'video-thumbnails' ) . ''; echo ''; } die(); } // End saving media test callback function delete_test_images_callback() { if ( !current_user_can( 'manage_options' ) ) die(); global $wpdb; // Clear images from media library $media_library_items = get_posts( array( 'showposts' => -1, 'post_type' => 'attachment', 'meta_key' => 'video_thumbnail_test_image', 'meta_value' => '1', 'fields' => 'ids' ) ); foreach ( $media_library_items as $item ) { wp_delete_attachment( $item, true ); } echo '✔ ' . sprintf( _n( '1 attachment deleted', '%s attachments deleted', count( $media_library_items ), 'video-thumbnails' ), count( $media_library_items ) ) . '
'; die(); } // End delete test images callback function markup_detection_test_callback() { if ( !current_user_can( 'manage_options' ) ) die(); $new_thumbnail = null; global $video_thumbnails; $markup = apply_filters( 'the_content', stripslashes( $_POST['markup'] ) ); $new_thumbnail = $video_thumbnails->get_first_thumbnail_url( $markup ); if ( $new_thumbnail == null ) { // No thumbnail echo '✖ ' . __( 'No thumbnail found', 'video-thumbnails' ) . '
'; } elseif ( is_wp_error( $new_thumbnail ) ) { // Error finding thumbnail echo '✖ ' . __( 'Error Details:', 'video-thumbnails' ) . ' ' . $new_thumbnail->get_error_message() . '
'; } else { // Found a thumbnail $remote_response = wp_remote_head( $new_thumbnail ); if ( is_wp_error( $remote_response ) ) { // WP Error trying to read image from remote server echo '✖ ' . __( 'Thumbnail found, but there was an error retrieving the URL.', 'video-thumbnails' ) . '
'; echo '' . __( 'Error Details:', 'video-thumbnails' ) . ' ' . $remote_response->get_error_message() . '
'; } elseif ( $remote_response['response']['code'] != '200' ) { // Response code isn't okay echo '✖ ' . __( 'Thumbnail found, but it may not exist on the source server. If opening the URL below in your web browser returns an error, the source is providing an invalid URL.', 'video-thumbnails' ) . '
'; echo '' . __( 'Thumbnail URL:', 'video-thumbnails' ) . ' ' . $new_thumbnail . ''; } else { // Everything is okay! echo '
✔ ' . __( 'Thumbnail found! Image should appear below.', 'video-thumbnails' ) . ' ' . __( 'View full size', 'video-thumbnails' ) . '
'; echo ''; } } die(); } // End markup detection test callback function initialize_options() { add_settings_section( 'general_settings_section', __( 'General Settings', 'video-thumbnails' ), array( &$this, 'general_settings_callback' ), 'video_thumbnails' ); $this->add_checkbox_setting( 'save_media', __( 'Save Thumbnails to Media Library', 'video-thumbnails' ), __( 'Checking this option will download video thumbnails to your server', 'video-thumbnails' ) ); $this->add_checkbox_setting( 'set_featured', __( 'Automatically Set Featured Image', 'video-thumbnails' ), __( 'Check this option to automatically set video thumbnails as the featured image (requires saving to media library)', 'video-thumbnails' ) ); // Get post types $post_types = get_post_types( null, 'names' ); // Remove certain post types from array $post_types = array_diff( $post_types, array( 'attachment', 'revision', 'nav_menu_item' ) ); $this->add_multicheckbox_setting( 'post_types', __( 'Post Types', 'video-thumbnails' ), $post_types ); $this->add_text_setting( 'custom_field', __( 'Custom Field (optional)', 'video-thumbnails' ), '' . __( 'Automatically Detect', 'video-thumbnails' ) . ' ' . __( 'Enter the name of the custom field where your embed code or video URL is stored.', 'video-thumbnails' ) ); register_setting( 'video_thumbnails', 'video_thumbnails', array( &$this, 'sanitize_callback' ) ); } function sanitize_callback( $input ) { $current_settings = get_option( 'video_thumbnails' ); $output = array(); // General settings if ( !isset( $input['provider_options'] ) ) { foreach( $current_settings as $key => $value ) { if ( $key == 'version' OR $key == 'providers' ) { $output[$key] = $current_settings[$key]; } elseif ( isset( $input[$key] ) ) { $output[$key] = $input[$key]; } else { $output[$key] = ''; } } } // Provider settings else { $output = $current_settings; unset( $output['providers'] ); $output['providers'] = $input['providers']; } return $output; } function general_settings_callback() { echo '' . __( 'These options configure where the plugin will search for videos and what to do with thumbnails once found.', 'video-thumbnails' ) . '
'; } function add_checkbox_setting( $slug, $name, $description ) { add_settings_field( $slug, $name, array( &$this, 'checkbox_callback' ), 'video_thumbnails', 'general_settings_section', array( 'slug' => $slug, 'description' => $description ) ); } function checkbox_callback( $args ) { $html = ''; echo $html; } function add_multicheckbox_setting( $slug, $name, $options ) { add_settings_field( $slug, $name, array( &$this, 'multicheckbox_callback' ), 'video_thumbnails', 'general_settings_section', array( 'slug' => $slug, 'options' => $options ) ); } function multicheckbox_callback( $args ) { if ( is_array( $this->options[$args['slug']] ) ) { $selected_types = $this->options[$args['slug']]; } else { $selected_types = array(); } $html = ''; foreach ( $args['options'] as $option ) { $checked = ( in_array( $option, $selected_types ) ? 'checked="checked"' : '' ); $html .= '