单击Fb共享按钮后,弹出窗口不会出现。什么是fb_dialog_box

时间:2014-07-10 05:38:15

标签: php facebook wordpress-plugin

请在Here查看演示问题我创建了自己的插件,可以在用户分享视频时看到视频。我已经制作了三个文件,一个是钩子,下一个是类,另一个是更新。我的插件99%功能在这里工作。见

<?php

class VideoLock {

    protected static $running = false;

    const PLUGIN_PREFIX = "VL";

    const SLUG = 'videolock';

    protected $sharesTableName;

    protected $viewsTableName;

    protected $_key;

    public function __construct() {
        //create the correctly prefixed table name
        global $wpdb;
        $this->sharesTableName = $wpdb->prefix."videolock_shares";
        $this->viewsTableName = $wpdb->prefix."videolock_views";

    }

    public function tinymceShortcodePlugin($plugin_array) {
       $plugin_array['videolock'] = $this->file('/assets/js/shortcodeplugin.js');
       return $plugin_array;
    }

    public function tinymceShortcodeButton($buttons) {
       array_push($buttons, "separator", "videolock");
       return $buttons;
    }

    public function init() {

        if ( ( current_user_can('edit_posts') || current_user_can('edit_pages') ) && 
            get_user_option('rich_editing') ) {
            //register admin editor shortcode creator
            add_filter("mce_external_plugins",  $this->callback('tinymceShortcodePlugin'));
            add_filter('mce_buttons',           $this->callback('tinymceShortcodeButton'));
        }

        //register admin tinymce shortcode insert plugin
        wp_enqueue_style($this->prefix('locker-style'), $this->file('/assets/css/blocker.css'));
        wp_enqueue_script($this->prefix('locker-script'), $this->file('/assets/js/blocker.js?v=2'), 
                          array('jquery','swfobject'));

        //enqueue swfobject if not present
        if( wp_script_is('swfobject') ) {
            wp_enqueue_script('swfobject', $this->file('/assets/js/swfobject.js'));
        }

        //register plugin options
        add_option('facebook-id');
        add_option('share-message', 'Unlock this video by sharing it on facebook !');
        add_option('video-size', '560x315');

        new VideoLockUpdate( $this->getKey() );
    }

    public static function getVersion() {

        $file = dirname(__FILE__) . '/version';
        if( file_exists($file)) {
            return trim(file_get_contents($file));
        }

        return '0';
    }

    public function getKey() {

        if( $this->_key ) {
            return $this->_key;
        }

        $file = dirname(__FILE__) . '/licence.php';

        if( file_exists($file)) {
            include $file;
            $this->_key = defined('VIDEOLOCK_LICENCE_KEY') ? VIDEOLOCK_LICENCE_KEY : null;
            return $this->_key;
        }

        return false;
    }

    public function install() {

        global $wpdb;

        $key = $this->getKey();

        if( !$key ) {
            wp_die('Fail to activate this plugin. You need a valid licence key for activation. For more informations contact us at <a href="mailto:support@wpvideolock.com">support@wpvideolock.com</a> or the <a href="http://www.wpvideolock.com/contact-us/">online form</a><br/>If you bought this plugin, please send your email address used for buying the plugin to resend a functional version.<br/>Thank you!');
        }

        $request = wp_remote_post('http://update.wpvideolock.com/', array('body' => array('action' => 'validate-key', 'key'=>$key)));
        if (!is_wp_error($request) || wp_remote_retrieve_response_code($request) === 200) {
            $response = trim($request['body']);
            if( $response != 'ok' ) {
                wp_die($response);
            }
        }

        update_option('facebook-id', '');
        update_option('share-message', 'Share on facebook to unlock this video .');
        update_option('video-size', '560x315');

        if(!in_array($this->sharesTableName,$wpdb->tables())) {

            $sql = "CREATE TABLE `" . $this->sharesTableName . "` (
                        id INT(11) NOT NULL AUTO_INCREMENT,
                        url VARCHAR(255) NOT NULL,
                        ip VARCHAR(40) NOT NULL,
                        post INT(11) NOT NULL,
                        time DATETIME,
                        `share_id` VARCHAR( 255 ) NOT NULL,
                        `gender` ENUM(  'female',  'male' ),
                       PRIMARY KEY ( id )
                    )";

            $wpdb->query($sql);
        }

        if(!in_array($this->viewsTableName,$wpdb->tables())) {

            $sql = "CREATE TABLE IF NOT EXISTS `" . $this->viewsTableName . "` (
                      `post_id` int(11) NOT NULL,
                      `views` int(11) NOT NULL,
                      `date` date NOT NULL,
                      UNIQUE KEY `post_id` (`post_id`,`date`)
                    )";

            $wpdb->query($sql);
        }
    }

    public function uninstall() {

        global $wpdb;

        $backup = false; // Take it from options after user change settings for the plugin to create backup

        if( $backup ) {

            $sql = "SELECT * FROM `" . $this->sharesTableName . "`";
            $results = $wpdb->get_results($sql);
            if( count($results) ) {
                $uploads_dir = wp_upload_dir();
                $backup_file = $uploads_dir['basedir'].'/videolock_backup.txt';
                file_put_contents($backup_file,serialize($results));
            }
        }

        $wpdb->query("DROP TABLE `" . $this->sharesTableName . "`");
        $wpdb->query("DROP TABLE `" . $this->viewsTableName . "`");

        delete_option('facebook-id', '');
        delete_option('share-message', 'Share on facebook to unlock this video .');
        delete_option('video-size', '560x315');
    }

    public function getYoutubeId( $url ) {

        $url = strip_tags($url);
        $url = parse_url($url);
        $query = $url['query'];
        //If video is in a format like this
        // http://www.youtube.com/watch?v=fKmuUDQdcXc
        if($query) {
            $id = str_replace("v=","",$query);
            return $id;
        }
        //if video is in a format like this
        // http://youtu.be/fKmuUDQdcXc
        else {
            $path = $url['path'];
            $id = str_replace("/","",$path);
            return $id;
        }
    }

    public function shortcode( $atts ) {

        global $wpdb;

        //extract attributes to function scope
        extract( shortcode_atts( array(
            'url' => false,
            'id' => false,
            'width' => '420',
            'height' => '315',
            'expire' => 0,
            'message' => get_option('share-message'),
        ), $atts ) );

        //do not show anything if to id or url
        if( $url === false && $id === false ) {
            return null;
        }

        //extract the video id in case url is passed
        if( $url ) {
            $id = $this->getYoutubeId($url);
        }

        $image = 'http://img.youtube.com/vi/' . $id . '/0.jpg';

        $view = new View( dirname(__FILE__) . '/views/blocker.php' );

        $view->image = $image;
        $view->id = $id;
        $view->url = $url;
        $view->width = $width;
        $view->height = $height;
        $view->message = $message;
        $view->expire = $expire;

        //if there werent any views today insert it
        //else increment it
        $sql = "INSERT INTO `" . $this->viewsTableName . "`
            (`post_id`,`views`,`date`)
                VALUES
            ('" . get_the_ID() . "',1,CURDATE())
            ON DUPLICATE KEY UPDATE views=views+1;";

        $wpdb->query($sql);

        $view->helper('isLocked', $this->callback('isLocked'));

        return $view->render();
    }

    public function actionLinks( $links, $file ) {

        $url = admin_url( 'admin.php?page=videolock/dashboard.php' );
        if ( $file == plugin_basename( dirname(__FILE__).'/videolock.php' ) ) {
            $links[] = '<a href="' . $url . '">'.__( 'Dashboard' ).'</a>';
        }

        return $links;
    }

    public function isLocked( $postid = null ) {

        global $wpdb;

        $postid = is_numeric($postid) ? $postid : get_the_ID();

        $sql = "SELECT COUNT(*) AS total FROM `" . $this->sharesTableName . "` WHERE `post`=%d AND `ip`=%s AND `url`=%s LIMIT 1";

        $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

        $sql = $wpdb->prepare($sql, $postid, $_SERVER['REMOTE_ADDR'], $url);

        $result = $wpdb->get_results($sql);

        if( isset($result[0]) ) {
            return $result[0]->total == '0';
        }

        return true;
    }

    public function share() {

        global $wpdb; // this is how you get access to the database

        $url= $wpdb->escape($_POST['link']);
        $ip = $_SERVER['REMOTE_ADDR'];
        $postId = intval($_POST['post']);
        $share = $_POST['share'];
        $gender = null;

        $share_parts=explode('_', $share);

        if(count($share_parts)) {
            $share_user = $share_parts[0];
            if($share_user) {
                try {
                    $user_data = json_decode(@file_get_contents('http://graph.facebook.com/' . $share_user));
                $gender = isset($user_data->gender) ? $user_data->gender : null;
                }catch(Exception $e) {}
            }
        }

        $result = $wpdb->insert($this->sharesTableName,array(
            'url'       => $url,
            'ip'        => $ip,
            'post'      => $postId,
            'time'      => current_time('mysql', 1),
            'share_id'  => $share,
            'gender'    => $gender
        ));

        echo $result ? '1':'0';

        die();
    }

    public function shareLogged() {
        //not implemented yet for logged users
        $this->share();
    }

    public function queueAdminScripts() {
        wp_localize_script( $this->prefix('blocker-jsobj'), 'ajax_object',
            array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );


        wp_enqueue_script($this->prefix('dashboard-script'), $this->file('/assets/js/dashboard.js'), array('jquery', $this->prefix('flot')));
        wp_enqueue_script($this->prefix('flot'), $this->file('/assets/js/flot/jquery.flot.js'));
        wp_enqueue_script($this->prefix('flot.time'), $this->file('/assets/js/flot/jquery.flot.time.js'));
    }

    public function createDashboard() {
        global $wpdb;

        $sql = "SELECT SUM(views) as views, post_id FROM `" . $this->viewsTableName . "` GROUP BY post_id";
        $postsViews = $wpdb->get_results($sql);

        wp_enqueue_style($this->prefix('fresh'), '/wp-admin/css/colors-fresh.min.css?ver=3.7.1');
        wp_enqueue_style($this->prefix('dashboard-theme'), $this->file('/assets/css/dashboard.css'));

        include dirname(__FILE__) . '/views/dashboard.php';
    }

    public function createAdminMenu() {

        $menu_icon = $this->file('/assets/images/logo.png');
        $content_callback = $this->callback('createDashboard');
        //add admin left pannel menu
        add_menu_page( __('Video Lock'), 
                   __('Video Lock'),
                   __('manage_options'),
                   'videolock/dashboard.php', $content_callback , $menu_icon );
    }

    private function _getShares($post,$date) {

        global $wpdb;

        //get timestamp from date
        $timestamp = strtotime($date);

        //calculate start and end of the day
        $startTime =  date('Y-m-d 00:00:00',$timestamp);
        $endTime = date('Y-m-d 23:59:59',$timestamp);

        //select the shares for the day of the timestamp
        $sql = "SELECT COUNT(*) as shares FROM `" . $this->sharesTableName . "`
                WHERE `post` = %d AND `time` BETWEEN '$startTime' AND '$endTime'";
        $sql = $wpdb->prepare($sql, $post);
        $result = $wpdb->get_results($sql);

        if( isset($result[0]) ) {
            return $result[0]->shares;
        }
        else return 0;

    }
    public function shareStats() {

        global $wpdb;

        $response = array();
        $postId = $_POST['post'];

        //get views per day
        $sql = "SELECT views,`date` AS `date` FROM `" . $this->viewsTableName . "` WHERE `post_id`=%d";
        $sql = $wpdb->prepare($sql, $postId);
        $dates = $wpdb->get_results($sql);

        $today = date('Y-m-d');
        $hasToday = false;

        //generate response
        foreach($dates as $date) {

            //check if today exists (if not we can add it later)
            if($date->date == $today) {
                $hasToday = true;
            }

            $jsDate = strtotime($date->date)*1000;
            $response['views'][]  = array($jsDate ,$date->views);
            $response['shares'][] = array($jsDate , $this->_getShares( $postId, $date->date ));
        }

        //if we don't have today,
        //add it as 0 to the graph
        if(!$hasToday) {
            $today = strtotime($today) * 1000;
            $response['views'][]  = array( $today , 0 );
            $response['shares'][] = array( $today , 0 );
        }

        echo json_encode($response);
        exit();
    }

    public function saveSettings() {

        $settings = $_POST['data'];

        update_option('facebook-id', $settings[0]['value']);
        update_option('share-message', $settings[1]['value']);
        update_option('video-size', $settings[2]['value']);
        if($settings[2]['value'] == 'custom') {
            update_option('video-width', $settings[3]['value']);
            update_option('video-height', $settings[4]['value']);
        }
        else {
            delete_option('video-height');
            delete_option('video-width');
        }
    }

    private function _noFacebookId(){

        $facebookId = get_option('facebook-id');

        if(empty($facebookId) || !$facebookId )
            return true;
        else
            return false;
    }

    function adminNotice() {
        echo '<div class="updated">
                <p>You have successfully installed WordPress Video Lock Plugin.
                Please take a minute to <a href="admin.php?page=videolock/dashboard.php#fbid">add your facebook API key</a> and get started</p>
            </div>
        ';

    }


    private function file( $name ) {

        if( strpos($name, '/') !== 0 ) {
            $name = '/' . $name;
        }

        return plugins_url() . '/videolock' . $name;
    }

    private function prefix( $string ) {
        return VideoLock::PLUGIN_PREFIX . $string;
    }

    private function callback($method) {
        return array($this, $method);
    }

    public function run() {

        if( self::$running ) {
            throw new Exception("The Youtube Locker already running!");
        }

        //get the plugin script name for install/uninstall hooks
        $pluginFile = dirname(__FILE__) . '/videolock.php';

        //installation / dezinstalation hooks
        register_activation_hook( $pluginFile,  $this->callback('install') );
        register_deactivation_hook( $pluginFile,$this->callback('uninstall') );

        //short codes
        add_shortcode( 'youtubelocker',         $this->callback('shortcode') );
        add_shortcode( 'videolock',             $this->callback('shortcode') );

        //filters
        add_filter('plugin_action_links',       $this->callback('actionLinks'), 10, 2 );

        //add ajax callback
        add_action('wp_ajax_nopriv_video_share',$this->callback('share') );
        add_action('wp_ajax_video_share',       $this->callback('shareLogged') );
        add_action('wp_ajax_videolock_save_settings', $this->callback('saveSettings') );
        add_action('wp_ajax_videolock_share_stats', $this->callback('shareStats'));
        add_action('admin_enqueue_scripts',     $this->callback('queueAdminScripts') );
        add_action('admin_menu',                $this->callback('createAdminMenu') );

        if($this->_noFacebookId()) {
            add_action('admin_notices',             $this->callback('adminNotice') );
        }

        add_action('init',                      $this->callback('init'), 0);




        self::$running = true;
    }

}

但不知怎的,它没有按预期工作。当用户第一次分享视频时,它就像魅力一样,但当同一个用户共享另一个视频时会出错。它没有打开sharer.php它打开一个fb对话框并加载无限时间。我希望你们能帮助我解决这个问题。在插件完成后,该插件将在wp.org中免费提供 cheerz

0 个答案:

没有答案
相关问题