fgc-videos.php 8.22 KB
<?php
/*
  Plugin Name: Videos management
  Plugin URI: http://fgc.com
  Description: Videos management
  Version: 1.0
  Author: Fgc techlution
  Author URI: http://fgc.com
 */

// don't load directly
if (!function_exists('is_admin')) {
    header('Status: 403 Forbidden');
    header('HTTP/1.1 403 Forbidden');
    exit();
}

// Register Custom Post Type
function fgc_videos() {

    $labels = array(
        'name' => _x('Videos', 'Post Type General Name', 'videos'),
        'singular_name' => _x('Videos', 'Post Type Singular Name', 'videos'),
        'menu_name' => __('Videos', 'videos'),
        'name_admin_bar' => __('Forex Logos', 'forex_logo'),
        'parent_item_colon' => __('Parent Item:', 'forex_logo'),
        'all_items' => __('Videos', 'videos'),
        'add_new_item' => __('Add New video', 'videos'),
        'add_new' => __('Add New', 'videos'),
        'new_item' => __('New Item', 'videos'),
        'edit_item' => __('Edit Item', 'videos'),
        'update_item' => __('Update Item', 'videos'),
        'view_item' => __('View Item', 'videos'),
        'search_items' => __('Search Item', 'videos'),
        'not_found' => __('Not found', 'videos'),
        'not_found_in_trash' => __('Not found in Trash', 'videos'),
    );
    $args = array(
        'label' => __('Videos', 'videos'),
        'description' => __('Manage Videos', 'videos'),
        'labels' => $labels,
        'supports' => array('title', 'thumbnail'),
        //'taxonomies'          => array( 'category' ),
        'hierarchical' => false,
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'show_in_admin_bar' => true,
        'show_in_nav_menus' => true,
        'can_export' => true,
        'has_archive' => true,
        'exclude_from_search' => false,
        'publicly_queryable' => true,
        'capability_type' => 'page',
    );
    register_post_type('deadpan_video', $args);
}

add_action('init', 'fgc_videos', 0);

add_action('admin_init', 'fgc_video_admin');

function fgc_video_admin() {
    add_meta_box('video_meta_box', 'Video', 'fgc_display_video_meta_box', 'deadpan_video', 'normal', 'high'
    );
}

function fgc_display_video_meta_box($video) {
    // Retrieve current name of the Director and Movie Rating based on review ID
    $video_title = esc_html(get_post_meta($video->ID, 'video_title', true));
    $video_link = esc_html(get_post_meta($video->ID, 'video_link', true));
    ?>
    <table>
        <tr>
            <td style="width: 100%">Video title</td>
            <td><input type="text" size="80" name="video_title" value="<?php echo $video_title; ?>" /></td>
        </tr>
        <tr>
            <td style="width: 100%">Video link</td>
            <td><input type="text" size="80" name="video_link" value="<?php echo $video_link; ?>" /></td>
        </tr>
    </table>
    <?php
}

add_action('save_post', 'add_video_fields', 10, 2);

function add_video_fields($video_id, $video) {
    // Check post type for movie reviews
    if ($video->post_type == 'deadpan_video') {
        // Store data in post meta table if present in post data
        if (isset($_POST['video_title']) && $_POST['video_title'] != '') {
            update_post_meta($video_id, 'video_title', $_POST['video_title']);
        }
        if (isset($_POST['video_link']) && $_POST['video_link'] != '') {
            update_post_meta($video_id, 'video_link', $_POST['video_link']);
        }
    }
}

// if (isset($_GET['ajaxVideo']) && $_GET['video_id'] > 1) {
//     $html_video = wp_oembed_get(get_post_meta($_GET['video_id'], "video_link", true), array("width" => 700, "height" => 380));
//     echo str_replace("?feature=oembed", "?feature=oembed&rel=0&showinfo=0", $html_video);
//     exit();
// }

if (isset($_GET['saveDeadpan'])) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'deadpan_mailchimp';
    $firstname = $_REQUEST['first_name'];
    $email = $_REQUEST['email'];
    $data = array(
        'firstname' => $firstname,
        'email' => $email,
    );
    $exist_email = $wpdb->get_row("SELECT id FROM $table_name WHERE email = '$email'");
    if (!$exist_email) {
        if ($firstname != '' && $email != '') {
            $wpdb->insert($table_name, $data);
            $data_mailchimp = array(
                "FNAME" => $firstname,
                "LNAME" => '',
                "MMERGE3" => $firstname
            );
            //fgc_postToMailchimp($email, $data_mailchimp);
            $msg = '<span class="success-message">Thanks! The winner will be drawn on November 30th, 2016 and notified by email.</span>';
        } else {
            $msg = '<span class="error-message">Your details have been unsuccessfully submitted. Please try again.</span>';
        }
    } else {
        $msg = '<span class="error-message">Hi ' . $firstname . ', we already have your email address in our system and we’ve registered your interest in this competition.</span>';
    }
    echo $msg;
    exit();
}

function create_plugin_database_table() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'deadpan_mailchimp';
    $sql = "CREATE TABLE $table_name (
 id int(11) unsigned NOT NULL AUTO_INCREMENT,
 firstname varchar(200) NOT NULL,
 email varchar(200) NOT NULL,
 PRIMARY KEY  (id)
 );";

    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta($sql);
}

register_activation_hook(__FILE__, 'create_plugin_database_table');

function fgc_postToMailchimp($email, $data = array()) {
    if ($email == '')
        return;
    include_once('class.rest_api.php');

    $api_key = '';
    $list_id = '';
    $api = explode('-', $api_key);
    $config = array('dcAPI' => $api[1], 'keyAPI' => $api[0]);

    $restApi = new RestApi($config);
    $email_struct = new StdClass();
    $email_struct->email = $email;

    $args = array(
        'apikey' => $api_key,
        'id' => $list_id,
        'email' => $email_struct,
        'merge_vars' => $data,
        'double_optin' => false,
        'send_welcome' => true
    );
    $result = $restApi->runMethod($args, 'lists/subscribe');
}

add_action('admin_menu', 'fgc_create_post_short_description');
add_action('save_post', 'fgc_save_post_short_description', 10, 2);

function fgc_create_post_short_description() {
    add_meta_box('short-description', 'Short Description', 'fgc_short_description', 'post', 'normal', 'high');
}

function fgc_short_description($object, $box) {
    ?>
    <p>
        <label for="short_description">Short Description</label>
        <br />
        <textarea name="short_description" id="short_description" cols="60" rows="4" tabindex="30" style="width: 97%;"><?php echo wp_specialchars(get_post_meta($object->ID, 'Short Description', true), 1); ?></textarea>
        <input type="hidden" name="short_description_none" value="<?php echo wp_create_nonce(plugin_basename(__FILE__)); ?>" />
    </p>
    <?php
}

function fgc_save_post_short_description($post_id, $post) {

    if (!wp_verify_nonce($_POST['short_description_none'], plugin_basename(__FILE__)))
        return $post_id;

    if (!current_user_can('edit_post', $post_id))
        return $post_id;

    $meta_value = get_post_meta($post_id, 'Short Description', true);
    $new_meta_value = stripslashes($_POST['short_description']);

    if ($new_meta_value && '' == $meta_value)
        add_post_meta($post_id, 'Short Description', $new_meta_value, true);

    elseif ($new_meta_value != $meta_value)
        update_post_meta($post_id, 'Short Description', $new_meta_value);

    elseif ('' == $new_meta_value && $meta_value)
        delete_post_meta($post_id, 'Short Description', $meta_value);
}

function fgc_get_short_description($post_id) {
    echo get_post_meta($post_id, 'Short Description', true);
}

function fgc_get_value_param($url, $name) {
    parse_str(parse_url($url, PHP_URL_QUERY), $vars);
    return isset($vars[$name]) ? $vars[$name] : false;
}

function get_first_video() {
    global $wpdb;
    $first_result = $wpdb->get_row("SELECT ID FROM $wpdb->posts WHERE post_type = 'deadpan_video' AND post_status='publish' ORDER BY ID ASC LIMIT 0,1");
    if ($first_result->ID > 0) {
        return $first_result->ID;
    } else {
        return 0;
    }
}

add_filter('oembed_result', 'modify_YT_embed_url');

function modify_YT_embed_url($html) {
    return str_replace("?feature=oembed", "?feature=oembed&rel=0&showinfo=0", $html);
}