Advertisement
Guest User

PWAL

a guest
May 20th, 2014
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 124.15 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Pay With a Like
  4. Description: Allows protecting posts/pages until visitor likes the page or parts of the page with Facebook, Linkedin, Twitter or Google +1.
  5. Plugin URI: http://premium.wpmudev.org/project/pay-with-a-like
  6. Version: 2.0.1
  7. Author: WPMU DEV
  8. Author URI: http://premium.wpmudev.org/
  9. TextDomain: pwal
  10. Domain Path: /languages
  11. WDP ID: 7330
  12. */
  13.  
  14. /*
  15. Copyright 2009-2014 Incsub (http://incsub.com)
  16. Author - Paul Menard
  17.  
  18. This program is free software; you can redistribute it and/or modify
  19. it under the terms of the GNU General Public License (Version 2 - GPLv2) as published by
  20. the Free Software Foundation.
  21.  
  22. This program is distributed in the hope that it will be useful,
  23. but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25. GNU General Public License for more details.
  26.  
  27. You should have received a copy of the GNU General Public License
  28. along with this program; if not, write to the Free Software
  29. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  30. */
  31.  
  32. //include_once( dirname(__FILE__) .'/pwal-uninstall.php');
  33.  
  34. if ( !class_exists( 'PayWithaLike' ) ) {
  35.  
  36. class PayWithaLike {
  37.  
  38.     var $version                    =   "2.0.1";
  39.     var $pwal_js_data               =   array();
  40.     var $_pagehooks                 =   array();
  41.     var $_options_defaults          =   array();
  42.     var $options                    =   array();
  43.  
  44.     var $doing_set_cookie;
  45.     var $cookie_key                 =   'pay_with_a_like';
  46.     var $cookies                    =   array();
  47.  
  48.     var $facebook_sdk_ref;
  49.     var $facebook_user_profile;
  50.  
  51.     var $_registered_scripts        = array();
  52.     var $_registered_styles         = array();
  53.  
  54.     var $sitewide_id;
  55.  
  56.     /**
  57.      * Constructor
  58.      */
  59.     function __construct() {
  60.         // Constants
  61.         $this->plugin_name = "pay-with-a-like";
  62.         $this->plugin_dir = WP_PLUGIN_DIR . '/' . $this->plugin_name;
  63.         $this->plugin_url = plugins_url( '/' . $this->plugin_name );
  64.         $this->page = 'settings_page_' . $this->plugin_name;
  65.         $this->doing_set_cookie = false;
  66.  
  67.         $this->sitewide_id              = defined('PWAL_SITEWIDE_ID') ? PWAL_SITEWIDE_ID : 'XXX123456789';
  68.  
  69.  
  70.         $this->facebook_sdk_ref = false;
  71.         $this->facebook_user_profile = false;
  72.  
  73.         register_activation_hook( __FILE__, array( &$this, 'install' ) );
  74.         register_deactivation_hook( __FILE__, array( &$this, 'uninstall' ) );
  75.  
  76.         // Support for WPMU DEV Dashboard plugin
  77.         global $wpmudev_notices;
  78.         $wpmudev_notices[] = array( 'id'=> 7330, 'name'=> 'Pay With a Like', 'screens' => array( 'toplevel_page_pay-with-a-like', 'pay-with-a-like_page_pay-with-a-like-buttons', 'pay-with-a-like_page_pay-with-a-like-statistics', 'pay-with-a-like_page_pay-with-a-like-customization' ) );
  79.         include_once( dirname(__FILE__) . '/lib/dash-notices/wpmudev-dash-notification.php' );
  80.  
  81.         add_action( 'init', array(&$this, 'init'));
  82.  
  83.         // Admin side actions
  84.         add_action( 'admin_notices', array($this, 'no_button') );                       // Warn admin if no Social button is selected
  85.         add_action( 'admin_notices', array($this, 'notice_settings') );                 // Notice admin to make some settings
  86.         add_filter( 'plugin_row_meta', array(&$this,'set_plugin_meta'), 10, 2 );        // Add settings link on plugin page
  87.         add_action( 'admin_menu', array( &$this, 'admin_menu' ) );                      // Creates admin settings window
  88.  
  89.         add_action( 'add_meta_boxes', array( &$this, 'add_custom_box' ) );              // Add meta box to posts
  90.         //add_action( 'wp_ajax_delete_stats', array( &$this, 'delete_stats' ) );        // Clear statistics
  91.         add_action( 'wp_ajax_pwal_export_stats', array( &$this, 'export_stats' ) );     // Export statistics
  92.         add_action( 'admin_print_scripts-'. $this->page , array(&$this,'admin_scripts'));
  93.  
  94.  
  95.         add_action( 'wp_head', array(&$this, 'wp_head') );
  96.         add_action( 'wp_enqueue_scripts', array(&$this, 'wp_enqueue_scripts') );
  97.         add_action( 'admin_enqueue_scripts', array(&$this, 'admin_enqueue_scripts') );
  98.  
  99.         add_action( 'plugins_loaded', array(&$this, 'localization') );          // Localize the plugin
  100.  
  101.         add_action( 'save_post', array( &$this, 'save_postmeta' ), 10, 2 );             // Calls post meta addition function on each save
  102.  
  103.  
  104.         add_filter( 'the_content', array( &$this, 'content' ), 8 );             // Manipulate the content.
  105.         add_filter( 'the_content', array($this, 'clear'), 130 );                // Clear if a shortcode is left
  106.  
  107.         add_shortcode('pwal', array($this, 'pwal_shortcode'));
  108.  
  109. //      add_action( 'wp_ajax_nopriv_pwal_action', array(&$this, 'set_cookie') );// Ajax request after a button is clicked
  110. //      add_action( 'wp_ajax_pwal_action', array(&$this, 'set_cookie') );       // Ajax request after a button is clicked
  111.  
  112.         add_action( 'wp_ajax_nopriv_pwal_buttons_action', array(&$this, 'handle_buttons_action') );// Ajax request after a button is clicked
  113.         add_action( 'wp_ajax_pwal_buttons_action', array(&$this, 'handle_buttons_action') );        // Ajax request after a button is clicked
  114.  
  115.  
  116.         add_action( 'wp_footer', array(&$this, 'footer') );
  117.  
  118.         // tinyMCE stuff
  119.         add_action( 'wp_ajax_pwalTinymceOptions', array(&$this, 'tinymce_options') );
  120.         add_action( 'admin_init', array(&$this, 'load_tinymce') );
  121.  
  122.         add_action( 'wp_ajax__aff_getstats', array(&$this,'ajax__aff_getstats') );
  123.  
  124.         // By default assume that pages are cachable (Cache plugins are allowed)
  125.         $this->buttons_added = false;
  126.         $this->footer_script = "";
  127.  
  128.  
  129.         // Load the Facebook PHP SDK. So we can check if the user has previously liked the content
  130. /*
  131.         include_once( dirname(__FILE__) .'/lib/facebook-php-sdk/facebook.php');
  132.  
  133.         $facebook = new PWALFacebook(array(
  134.             'appId'  => '489708481139071',
  135.             'secret' => '094e908608b9085916a9f474a03ef730',
  136.             'cookie' => true,
  137.         ));
  138.  
  139.         $user = $facebook->getUser();
  140.         if ($user) {
  141.             try {
  142.                 //echo "user<pre>"; print_r($user); echo "</pre>";
  143.  
  144.                 // Proceed knowing you have a logged in user who's authenticated.
  145.                 //$user_profile = $facebook->api('/me');
  146.                 //if ($user_profile) {
  147.                 //  echo "user_profile<pre>"; print_r($user_profile); echo "</pre>";
  148.  
  149.                     //Create Query
  150.                     //$params = array(
  151.                     //  'method'    =>  'fql.query',
  152.                     //  'query'     => 'SELECT url, user_id FROM url_like WHERE user_id = me()'
  153.                     //);
  154.  
  155.                     //$params = array(
  156.                     //  'method'    =>  'fql.query',
  157.                     //  'query'     => 'SELECT created_time, page_id, profile_section, type, uid FROM page_fan WHERE uid = me()'
  158.                     //);
  159.  
  160. //                  $params = array(
  161. //                      'method'    =>  'fql.query',
  162. //                      'query'     => 'SELECT created_time, page_id, profile_section, type, uid FROM page_fan WHERE uid = me() AND page_id='. 110774245616525
  163. //                  );
  164.  
  165.                     $params = array(
  166.                         'method'    =>  'fql.query',
  167.                         'query'     => "SELECT name,page_id, page_url,pic_square FROM page WHERE username = 'museedulouvre'"
  168.                     );
  169.  
  170.                     echo "params<pre>"; print_r($params); echo "</pre>";
  171.  
  172.                     //Run Query
  173.                     $result = $facebook->api($params);
  174.                     echo "result<pre>"; print_r($result); echo "</pre>";
  175.                     //}
  176.  
  177.             } catch (PWALFacebookApiException $e) {
  178.                 error_log($e);
  179.                 $user = null;
  180.             }
  181.         }
  182.         die();
  183. */
  184.     }
  185.  
  186.     function PayWithaLike() {
  187.         $this->__construct();
  188.     }
  189.  
  190.     function init() {
  191.  
  192.         $this->set_option_defaults();
  193.  
  194.         // Read all options at once
  195.         $this->options          = get_option( 'pwal_options', array() );
  196.         $this->options['salt']  = get_option( 'pay_with_a_like_salt' );
  197.  
  198.         $this->pwal_js_data['debug']        = "false";
  199.         if (isset($_GET['PWAL_DEBUG']))
  200.             $this->pwal_js_data['debug']    = "true";
  201.  
  202.         $this->options = wp_parse_args( $this->options, $this->_options_defaults );
  203.  
  204.         if (($this->options['use_facebook'])
  205.          && ($this->options['facebook_api_key']) && (!empty($this->options['facebook_api_key']))
  206.          && ($this->options['facebook_api_secret']) && (!empty($this->options['facebook_api_secret']))) {
  207.             $this->options['facebook_api_use'] = 'true';
  208.             //$this->facebook_sdk_setup();
  209.         } else {
  210.             $this->options['facebook_api_use'] = 'false';
  211.             $this->options['facebook_auth_polling'] = 'no';
  212.         }
  213.         //unset($this->options['post_types']);
  214.         if (!isset($this->options['post_types'])) {
  215.             $this->options['post_types'] = array();
  216.  
  217.             if ((isset($this->options['post_default'])) && ($this->options['post_default'] == 'enable')) {
  218.                 $this->options['post_types']['post'] = 'enable';
  219.             }
  220.             if ((isset($this->options['page_default'])) && ($this->options['page_default'] == 'enable')) {
  221.                 $this->options['post_types']['page'] = 'enable';
  222.             }
  223.             if ((isset($this->options['custom_default'])) && ($this->options['custom_default'] == 'enable')) {
  224.                 //echo "in here<br />";
  225.                 $post_types = get_post_types(array('public' => true), 'objects');
  226.                 //echo "post_types<pre>"; print_r($post_types); echo "</pre>";
  227.                 if (!empty($post_types)) {
  228.                     foreach($post_types as $slug => $post_type) {
  229.                         if (($slug == 'attachment') || ($slug == 'post') || ($slug == "page")) {
  230.                             continue;
  231.                         }
  232.                         $this->options['post_types'][$slug] = 'enable';
  233.                     }
  234.                 }
  235.             }
  236.         }
  237.  
  238.         if (!isset($this->options['show_metabox'])) {
  239.             $this->options['show_metabox'] = $this->options['post_types'];
  240.         }
  241.  
  242.         $this->load_cookie_likes();
  243.  
  244.         //echo "options<pre>"; print_r($this->options); echo "</pre>";
  245.         //die();
  246.     }
  247.  
  248.     function set_option_defaults() {
  249.  
  250.         if ( !$salt = get_option( "pay_with_a_like_salt" ) ) {
  251.             $salt = mt_rand();
  252.             add_option( "pay_with_a_like_salt", $salt ); // Save it to be used until it is cleared manually
  253.         }
  254.  
  255.         $this->_options_defaults = array(
  256.             'post_default'                  =>  'enable',
  257.             'page_default'                  =>  '',
  258.             'custom_default'                =>  '',
  259.             'method'                        =>  'automatic',
  260.             'excerpt'                       =>  100,
  261.             'content_reload'                =>  'refresh',
  262.             'admin'                         =>  'true',
  263.             'home'                          =>  '',
  264.             'multi'                         =>  'true',
  265.             'authorized'                    =>  '',
  266.             'level'                         =>  'editor',
  267.             'bot'                           =>  '',
  268.             'cookie'                        =>  24,
  269.             'social_buttons'                =>   array(
  270.                                                     'facebook'  =>  __('Facebook',  'pwal'),
  271.                                                     'linkedin'  =>  __('Linkedin',  'pwal'),
  272.                                                     'twitter'   =>  __('Twitter',   'pwal'),
  273.                                                     'google'    =>  __('Google+1',  'pwal')
  274.                                                 ),
  275.             'social_button_sort'                =>  '', // empty will load based on social_button order
  276.             'use_facebook'                      =>  'true',
  277.             'use_linkedin'                      =>  'true',
  278.             'use_twitter'                       =>  'true',
  279.             'use_google'                        =>  'true',
  280.             'url_to_like'                       =>  '',
  281.             'salt'                              =>  $salt,
  282.             'sitewide'                          =>  '',
  283.             'description'                       =>  __('To see the full content, share this page by clicking one of the buttons below','pwal'),
  284.             'random'                            =>  '',
  285.             'no_visit'                          =>  '',
  286.             'load_facebook'                     =>  'true',
  287.             'load_linkedin'                     =>  'true',
  288.             'load_twitter'                      =>  'true',
  289.             'load_google'                       =>  'true',
  290.             'container_width'                   =>  '',
  291.             'container_height'                  =>  '',
  292.             'container_border_width'            =>  '1',
  293.             'container_border_style'            =>  'solid',
  294.             'container_border_color'            =>  '#E3E3E3',
  295.             'show_facebook_comment_popup'       =>  'false',
  296.             'facebook_api_key'                  =>  '',
  297.             'facebook_api_secret'               =>  '',
  298.             'facebook_fan_pages'                =>  array(),
  299.             'facebook_layout_style'             =>  'box_count',
  300.             'facebook_auth_polling'             =>  'yes',
  301.             'facebook_auth_polling_interval'    =>  '1',
  302.             'linkedin_layout_style'             =>  'top',
  303.             'linkedin_button_lang'              =>  '',
  304.             'twitter_layout_style'              =>  'vertical',
  305.             //'twitter_button_size'             =>  'medium',
  306.             'twitter_message'                   =>  '',
  307.             'twitter_button_lang'               =>  '',
  308.             'google_layout_style'               =>  'tall-bubble',
  309.             'google_button_lang'                =>  '',
  310.             //'facebook_color_scheme'           =>  '',
  311.             'facebook_verb'                     =>  'like',
  312.             'facebook_button_lang'              =>  '',
  313.             'facebook_include_share'            =>  '',
  314.             'facebook_include_faces'            =>  ''
  315.         );
  316.  
  317.  
  318.     }
  319.  
  320.     function wp_head() {
  321.         if (is_admin()) return;
  322.  
  323.         if ( $this->options["use_linkedin"] && $this->options["load_linkedin"] ) {
  324.             $locale = $this->options["linkedin_button_lang"];
  325.             if (!empty($locale)) {
  326.                 ?><script src="//platform.linkedin.com/in.js" type="text/javascript">lang: <?php echo $locale ?></script><?php
  327.             }
  328.         }
  329.     }
  330.  
  331. //  function admin_enqueue_scripts() {
  332.         //wp_enqueue_style( 'pay-with-a-like-admin-css', plugins_url('/css/pay-with-a-like-admin.css', __FILE__), array(), $this->version);
  333.         //wp_enqueue_script( 'pay-with-a-like-admin-js', plugins_url('/js/pay-with-a-like-admin.js', __FILE__), array(), $this->version);
  334. //  }
  335.  
  336.  
  337.     /**
  338.     * Make it possible to save status of postboxes
  339.     */
  340.     function admin_enqueue_scripts() {
  341.         wp_enqueue_script('jquery');
  342.         wp_enqueue_script('postbox');
  343.     }
  344.  
  345.     /**
  346.     * Add Settings link to the plugin page
  347.     * @ http://wpengineer.com/1295/meta-links-for-wordpress-plugins/
  348.     */
  349.     function set_plugin_meta($links, $file) {
  350.         // create link
  351.         $plugin = plugin_basename(__FILE__);
  352.         if ($file == $plugin) {
  353.             return array_merge(
  354.                 $links,
  355.                 array( sprintf( '<a href="options-general.php?page=%s">%s</a>', $this->plugin_name, __('Settings') ) )
  356.             );
  357.         }
  358.         return $links;
  359.     }
  360.  
  361.     /**
  362.      * Load css and js
  363.      */
  364.     function wp_enqueue_scripts() {
  365.  
  366.         if (is_admin()) return;
  367.  
  368.         //echo "in ". __FILE__ .": ". __FUNCTION__ .": ". __LINE__ ."<br />";
  369.         wp_enqueue_script('jquery');
  370.         wp_enqueue_script( 'pay-with-a-like-js', plugins_url('/js/pay-with-a-like.js', __FILE__), array('jquery'), $this->version, true);
  371.         $this->_registered_scripts['pay-with-a-like-js'] = 'pay-with-a-like-js';
  372.  
  373.         //echo "user_facebook[". $this->options["use_facebook"] ."] load_facebook[". $this->options["load_facebook"] ."]<br />";
  374.         if ( $this->options["use_facebook"] && $this->options["load_facebook"] ) {
  375.  
  376.             $locale = $this->options["facebook_button_lang"];
  377.             if (empty($locale)) {
  378.                 $locale = get_locale();
  379.                 $locale = preg_replace('/-/', '_', $locale);
  380.  
  381.                 // Fix for de_DE_Sie type locale
  382.                 if ( substr_count( $locale, '_' ) > 1 ) {
  383.                     $l = explode( '_', $locale );
  384.                     $locale = $l[0] . '_' . $l[1];
  385.                 }
  386.                 $locale = apply_filters( 'pwal_fb_locale', $locale );
  387.             }
  388.  
  389.             //wp_enqueue_script('facebook-all', 'http://connect.facebook.net/' . $locale . '/all.js', array( 'jquery' ) );
  390.             // We don't enqueue the JS just yet. We pass the JS url to out script where it will be loaded dynamcially. If needed.
  391.             //$this->pwal_js_data['facebook-all-js'] = '//connect.facebook.net/' . $locale . '/all.js&status=0';
  392.  
  393.         }
  394.  
  395.         // See also the wp_head function in thei class. The logic is such that when a button_lang is defined we need load
  396.         // the in.js via the wp_head call because within the <script></script> wrapper we need to pass in the language. We
  397.         // do this in wp_head ONLY if the button_lang value is not empty. If it is empty we instea us the code just below
  398.         // to properly enqueue the JS. damn linkedin!
  399.         if ( $this->options["use_linkedin"] && $this->options["load_linkedin"] ) {
  400.             if (empty($this->options["linkedin_button_lang"])) {
  401.                 wp_enqueue_script( 'linkedin', '//platform.linkedin.com/in.js', array( 'jquery' ), '', true );
  402.                 //$this->pwal_js_data['linkedin-js'] = '//platform.linkedin.com/in.js';
  403.  
  404.                 $this->_registered_scripts['linkedin'] = 'linkedin';
  405.             }
  406.         }
  407.  
  408.         if ( $this->options["use_twitter"] && $this->options["load_twitter"] ) {
  409.             wp_enqueue_script( 'twitter', '//platform.twitter.com/widgets.js', array( 'jquery' ), '', true );
  410.             $this->_registered_scripts['twitter'] = 'twitter';
  411.  
  412.         }
  413.  
  414.         if ( $this->options["use_google"] && $this->options["load_google"] ) {
  415.             //wp_enqueue_script( 'google-plusone', '//apis.google.com/js/plusone.js', array( 'jquery' ), '1.0', true );
  416.             $this->pwal_js_data['google-plusone-js'] = '//apis.google.com/js/plusone.js';
  417.             $google_button_lang = $this->options["google_button_lang"];
  418.             if (!empty($google_button_lang)) {
  419.                 $this->pwal_js_data['google_button_lang'] = $google_button_lang;
  420.             }
  421.         }
  422.  
  423.         do_action("pwal_additional_button_scripts");
  424.  
  425.         if ( current_theme_supports( 'pay_with_a_like_style' ) )
  426.             return;
  427.  
  428.         $uploads = wp_upload_dir();
  429.         if ( !$uploads['error'] && file_exists( $uploads['basedir'] . "/". $this->plugin_name .".css" ) ) {
  430.             wp_enqueue_style( $this->plugin_name, $uploads['baseurl']. "/". $this->plugin_name .".css", array(), $this->version );
  431.             //$this->_registered_styles[$this->plugin_name] = $this->plugin_name;
  432.         } else if ( file_exists( $this->plugin_dir. "/css/front.css" ) ) {
  433.             wp_enqueue_style( $this->plugin_name, $this->plugin_url. "/css/front.css", array(), $this->version );
  434.             //$this->_registered_styles[$this->plugin_name] = $this->plugin_name;
  435.         }
  436.     }
  437.  
  438.     /**
  439.      * Localize the plugin
  440.      */
  441.     function localization() {
  442.         // Load up the localization file if we're using WordPress in a different language
  443.         // Place it in this plugin's "languages" folder and name it "pwal-[value in wp-config].mo"
  444.         //load_plugin_textdomain( 'pwal', false, '/pay-with-a-like/languages/' );
  445.         load_plugin_textdomain('pwal', false, dirname(plugin_basename(__FILE__)).'/languages/');
  446.     }
  447.  
  448.     function get_user_role_highest_level($user_role_capabilities = array()) {
  449.         $user_role_hightest_level = 0;
  450.  
  451.         foreach($user_role_capabilities as $capability => $is_set) {
  452.             if (strncasecmp($capability, 'level_', strlen('level_')) == 0) {
  453.                 $capability_int = intval(str_replace('level_', '', $capability));
  454.                 if ($capability_int > $user_role_hightest_level)
  455.                     $user_role_hightest_level = $capability_int;
  456.             }
  457.         }
  458.         return $user_role_hightest_level;
  459.     }
  460.  
  461.     function pwal_shortcode($atts, $content = null) {
  462.         //echo "DEBUG: in ". __FUNCTION__ .": ". __LINE__ ."<br />";
  463.         //echo "atts (before)<pre>"; print_r($atts); echo "</pre>";
  464.         //echo "content[". $content ."]<br />";
  465.  
  466.         $default_atts = array(
  467.             'id'                =>  '',
  468.             'post_id'           =>  '',
  469.             'wpautop'           =>  'yes',
  470.             'content_reload'    =>  '',
  471.             'container_width'   =>  '',
  472.             'description'       =>  ''
  473.         );
  474.         $atts = shortcode_atts( $default_atts, $atts );
  475.         $atts['method'] = 'tool';
  476.         $atts['content_id'] = $atts['id'];
  477.         unset($atts['id']);
  478.  
  479.         // IF there is no hidden content. Then no point continuing.
  480.         if ((!$content) || (empty($content))) return '';
  481.  
  482.         // We need to see if we are here from a post_content shortcode or some template calling do_shortcode
  483.         //$post_id = 0;
  484.  
  485.         if (empty($atts['post_id'])) {
  486.             global $post;
  487.  
  488.             if ((isset($post->post_content)) && (!empty($post->post_content)) && (has_shortcode( $post->post_content, 'pwal' ))) {
  489.  
  490.                 $pattern = get_shortcode_regex();
  491.                 preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches, PREG_SET_ORDER );
  492.                 if (!empty($matches)) {
  493.                     //echo "matches<pre>"; print_r($matches); echo "</pre>";
  494.                     foreach($matches as $match_set) {
  495.                         if ($match_set[2] = 'pwal') {
  496.                             $shortcode_atts = shortcode_parse_atts( $match_set[3] );
  497.                             //echo "shortcode_atts<pre>"; print_r($shortcode_atts); echo "</pre>";
  498.                             if ((is_array($shortcode_atts)) && (isset($shortcode_atts['id']))
  499.                              && ($shortcode_atts['id'] == $atts['content_id'])) {
  500.                                 $atts['post_id'] = $post->ID;
  501.                                 break;
  502.                             }
  503.                         }
  504.                     }
  505.                 }
  506.             }
  507.         }
  508.  
  509.         if (!empty($atts['post_id'])) {
  510.             if ((!isset($atts['description'])) || (empty($atts['description']))) {
  511.                 $description = get_post_meta( $atts['post_id'], 'pwal_description', true );
  512.                 if (!empty($description)) {
  513.                     $atts['description'] = $description;
  514.                 }
  515.             }
  516.  
  517.             if ((!isset($atts['content_reload'])) || (empty($atts['content_reload']))) {
  518.                 $content_reload = get_post_meta( $post->ID, 'pwal_content_reload', true );
  519.                 if (!empty($content_reload)) {
  520.                     $atts['content_reload'] = $content_reload;
  521.                 }
  522.             }
  523.  
  524.             if ((!isset($atts['container_width'])) || (empty($atts['container_width']))) {
  525.                 $container_width = get_post_meta( $post->ID, 'pwal_container_width', true );
  526.                 if (!empty($container_width)) {
  527.                     $atts['container_width'] = $container_width;
  528.                 }
  529.             }
  530.         }
  531.  
  532.         if ((!isset($atts['description'])) || (empty($atts['description']))) {
  533.             $atts['description'] = $this->options['description'];
  534.         }
  535.         if ((!isset($atts['content_reload'])) || (empty($atts['content_reload']))) {
  536.             $atts['content_reload'] = $this->options['content_reload'];
  537.         }
  538.         if ((!isset($atts['container_width'])) || (empty($atts['container_width']))) {
  539.             $atts['container_width'] = $this->options['container_width'];
  540.         }
  541.  
  542.         if (!defined( 'DOING_AJAX' ) || !DOING_AJAX) {
  543.             if ($this->pwal_js_data['debug'] == 'true') {
  544.                 echo "PWAL_DEBUG: ". __FUNCTION__ .": atts<pre>". print_r($atts, true) ."</pre>";
  545.             }
  546.         }
  547.  
  548.         if (!empty($atts['post_id'])) {
  549.             $display_buttons = $this->can_display_buttons($post);
  550.             if (!defined( 'DOING_AJAX' ) || !DOING_AJAX) {
  551.                 if ($this->pwal_js_data['debug'] == 'true') {
  552.                     echo "PWAL_DEBUG: ". __FUNCTION__ .": can_display_buttons returned". var_dump($display_buttons, true);
  553.                 }
  554.             }
  555.         } else {
  556.             $display_buttons = true;
  557.         }
  558.         $display_buttons_filtered = apply_filters('pwal_display_buttons', $display_buttons, $atts['post_id'], $atts['content_id']);
  559.         if ($display_buttons_filtered != $display_buttons) {
  560.             if (!defined( 'DOING_AJAX' ) || !DOING_AJAX) {
  561.                 if ($this->pwal_js_data['debug'] == 'true') {
  562.                     echo "PWAL_DEBUG: ". __FUNCTION__ .": pwal_display_buttons filter returned [". var_dump($display_buttons_filtered, true) ."] ";
  563.                 }
  564.             }
  565.         }
  566.         if (!$display_buttons) {
  567.             if (!defined( 'DOING_AJAX' ) || !DOING_AJAX) {
  568.                 if ($this->pwal_js_data['debug'] == 'true') {
  569.                     echo "PWAL_DEBUG: ". __FUNCTION__ .": display_buttons is FALSE returning hidden content.";
  570.                 }
  571.             }
  572.             return $this->the_content_filter($content, $atts['wpautop']);
  573.         }
  574.  
  575.         // If "Sitewide Like" is selected
  576.         if ( $this->options["sitewide"] ) {
  577.             foreach ( $this->cookie_likes['data'] as $like ) {
  578.                 if ( $like["post_id"] == md5( $this->sitewide_id . $this->options["salt"] ) ) {
  579.                     if ($this->pwal_js_data['debug'] == 'true') {
  580.                         echo "PWAL_DEBUG: ". __FUNCTION__ .": sitewide cookie exists<br />";
  581.                     }
  582.                     return $this->the_content_filter($content, $atts['wpautop']);
  583.                 }
  584.             }
  585.             if ($this->pwal_js_data['debug'] == 'true')
  586.                 echo "PWAL_DEBUG: ". __FUNCTION__ .": sitewide cookie not found<br />";
  587.         } else {
  588.             if ($this->pwal_js_data['debug'] == 'true')
  589.                 echo "PWAL_DEBUG: ". __FUNCTION__ .": option(sitewide): disabled<br />";
  590.         }
  591.  
  592.         foreach ( $this->cookie_likes['data'] as $like ) {
  593.             if (!empty($atts['post_id'])) {
  594.                 if ( $like["post_id"] == md5( $atts['post_id'] . $this->options["salt"] ) ) {
  595.                     if ($this->pwal_js_data['debug'] == 'true') {
  596.                         echo "PWAL_DEBUG: ". __FUNCTION__ .": found like cookie for post_id[". $atts['post_id'] ."]<br />";
  597.                     }
  598.                     return $this->the_content_filter($content, $atts['wpautop']);
  599.                 }
  600.             } else {
  601.                 if ( $like["content_id"] == $atts['content_id'] ) {
  602.                     if ($this->pwal_js_data['debug'] == 'true') {
  603.                         echo "PWAL_DEBUG: ". __FUNCTION__ .": found like cookie for content_id[". $atts['content_id'] ."]<br />";
  604.                     }
  605.                     return $this->the_content_filter($content, $atts['wpautop']);
  606.                 }
  607.             }
  608.         }
  609.         //if ($this->pwal_js_data['debug'] == 'true') {
  610.         //  echo "PWAL_DEBUG: ". __FUNCTION__ .": not found like cookie for post_id[". $atts['post_id'] ."]<br />";
  611.         //}
  612.  
  613.         //echo "DEBUG: in ". __FUNCTION__ .": ". __LINE__ ."<br />";
  614.         //echo "atts<pre>"; print_r($atts); echo "</pre>";
  615.         //echo "content[". $content ."]<br />";
  616.  
  617.         delete_transient('pwal_'.$atts['content_id']);
  618.         set_transient( 'pwal_'.$atts['content_id'], $content, 1 * HOUR_IN_SECONDS );
  619.         $pwal_content = '<div id="pwal_content_wrapper_'. $atts['content_id'] .'" class="pwal_content_wrapper">'. $this->render_buttons( $atts ) .'</div>';
  620.  
  621.         return $pwal_content;
  622.     }
  623.  
  624.     function the_content_filter($content, $wpautop = 'yes') {
  625.  
  626.         $content = stripslashes($content);
  627.         if ((has_filter('the_content', 'wpautop')) && ($wpautop == 'no')) {
  628.             $wpautop_org_state = true;
  629.             remove_filter( 'the_content', 'wpautop' );
  630.         } else if ((!has_filter('the_content', 'wpautop')) && ($wpautop == 'yes')) {
  631.             $wpautop_org_state = false;
  632.             add_filter( 'the_content', 'wpautop' );
  633.         }
  634.         $content = apply_filters( 'the_content', $content );
  635.         $content = str_replace( ']]>', ']]&gt;', $content );
  636.  
  637.         if ($wpautop_org_state == true) {
  638.             add_filter( 'the_content', 'wpautop' );
  639.         } else if ($wpautop_org_state == false) {
  640.             remove_filter( 'the_content', 'wpautop' );
  641.         }
  642.         return $content;
  643.     }
  644.  
  645.  
  646.     function can_display_buttons($post) {
  647.  
  648.         // Show the bot full content, if selected so
  649.         if ($this->options["bot"] == 'true') {
  650.             if ($this->pwal_js_data['debug'] == 'true')
  651.                 echo "PWAL_DEBUG: ". __FUNCTION__ .": option(bot): true<br />";
  652.  
  653.             if ($this->is_bot()) {
  654.                 if ($this->pwal_js_data['debug'] == 'true')
  655.                     echo "PWAL_DEBUG: ". __FUNCTION__ .": is_bot: true<br />";
  656.                 return false;
  657.             } else {
  658.                 if ($this->pwal_js_data['debug'] == 'true')
  659.                     echo "PWAL_DEBUG: ". __FUNCTION__ .": is_bot: false<br />";
  660.             }
  661.         } else {
  662.             if ($this->pwal_js_data['debug'] == 'true')
  663.                 echo "PWAL_DEBUG: ". __FUNCTION__ .": option(bot): false<br />";
  664.         }
  665.  
  666.         if ( $this->options["sitewide"] ) {
  667.             if ($this->pwal_js_data['debug'] == 'true')
  668.                 echo "PWAL_DEBUG: ". __FUNCTION__ .": option(sitewide): enabled<br />";
  669.  
  670.             $post_id = $this->sitewide_id;
  671.             // Check if this post is liked or sitewide like is selected
  672.             foreach ( $this->cookie_likes['data'] as $like ) {
  673.                 // Cookie is already encrypted, so we are looking if post_id matches to the encryption
  674.                 if ( $like["post_id"] == md5( $post_id . $this->options["salt"] ) ) {
  675.  
  676.                     if ($this->pwal_js_data['debug'] == 'true')
  677.                         echo "PWAL_DEBUG: ". __FUNCTION__ .": sitewide cookie exists<br />";
  678.  
  679.                     return false;
  680.                 }
  681.             }
  682.             if ($this->pwal_js_data['debug'] == 'true')
  683.                 echo "PWAL_DEBUG: ". __FUNCTION__ .": sitewide cookie not found<br />";
  684.         } else {
  685.             if ($this->pwal_js_data['debug'] == 'true')
  686.                 echo "PWAL_DEBUG: ". __FUNCTION__ .": option(sitewide): disabled<br />";
  687.         }
  688.  
  689.         if (get_current_user_id()) {
  690.             global $current_user;
  691.  
  692.             // Show the admin full content, if selected so
  693.             //echo "admin[". $this->options["admin"] ."]<br />";
  694.             if ( $this->options["admin"] == 'true') {
  695.                 if ($this->pwal_js_data['debug'] == 'true')
  696.                     echo "PWAL_DEBUG: ". __FUNCTION__ .": option(admin): true<br />";
  697.  
  698.                 if ((current_user_can('administrator')) || (is_super_admin())) {
  699.                     if ($this->pwal_js_data['debug'] == 'true')
  700.                         echo "PWAL_DEBUG: ". __FUNCTION__ .": current user is admin/super admin<br />";
  701.  
  702.                     return false;
  703.  
  704.                 } else {
  705.                     if ($this->pwal_js_data['debug'] == 'true')
  706.                         echo "PWAL_DEBUG: ". __FUNCTION__ .": current user is not admin/super admin<br />";
  707.                 }
  708.             }
  709.  
  710.             //echo "authorized[". $this->options['authorized'] ."]<br />";
  711.             if ( $this->options["authorized"] == 'true') {
  712.                 if ($this->pwal_js_data['debug'] == 'true')
  713.                     echo "PWAL_DEBUG: ". __FUNCTION__ .": option(authorized): true<br />";
  714.  
  715.                 //echo "current_user<pre>"; print_r($current_user); echo "</pre>";
  716.                 $current_user_role_level = $this->get_user_role_highest_level($current_user->allcaps);
  717.                 if (!$current_user_role_level) {
  718.                     $current_user_role_level = 0;
  719.                 }
  720.                 if ($this->pwal_js_data['debug'] == 'true')
  721.                     echo "PWAL_DEBUG: ". __FUNCTION__ .": current_user_role_level[". $current_user_role_level ."]<br />";
  722.  
  723.                 //echo "level[". $this->options['level'] ."]<br />";
  724.                 $log_user_role_level = intval(str_replace('level_', '', $this->options['level']));
  725.                 if (!$log_user_role_level) {
  726.                     $log_user_role_level = 0;
  727.                 }
  728.                 if ($this->pwal_js_data['debug'] == 'true')
  729.                     echo "PWAL_DEBUG: ". __FUNCTION__ .": log_user_role_level[". $log_user_role_level ."]<br />";
  730.  
  731.                 // If the current user level is less than our limit return the empty content.
  732.                 if ($log_user_role_level <= $current_user_role_level ) {
  733.                     return false;
  734.                 }
  735.             }
  736.         } else {
  737.             if ($this->pwal_js_data['debug'] == 'true')
  738.                 echo "PWAL_DEBUG: ". __FUNCTION__ .": user not logged in<br />";
  739.         }
  740.  
  741.         $post_info = array(
  742.             'post'      =>  false,
  743.             'post_type' =>  false
  744.         );
  745.  
  746.         if ((is_object( $post )) && isset($post->ID) && ($post->ID > 0)) {
  747.             if ($this->pwal_js_data['debug'] == 'true') {
  748.                 echo "PWAL_DEBUG: ". __FUNCTION__ .": post->ID: [". $post->ID ."]<br />";
  749.             }
  750.  
  751.             // Check if this post is liked or sitewide like is selected
  752.             foreach ( $this->cookie_likes['data'] as $like ) {
  753.                 // Cookie is already encrypted, so we are looking if post_id matches to the encryption
  754.                 if ( $like["post_id"] == md5( $post->ID . $this->options["salt"] ) ) {
  755.                     if ($this->pwal_js_data['debug'] == 'true') {
  756.                         echo "PWAL_DEBUG: ". __FUNCTION__ .": found like cookie for post_id[". $post->ID ."]<br />";
  757.                     }
  758.                     return false;
  759.                 }
  760.             }
  761.             if ($this->pwal_js_data['debug'] == 'true') {
  762.                 echo "PWAL_DEBUG: ". __FUNCTION__ .": not found like cookie for post_id[". $post->ID ."]<br />";
  763.             }
  764.  
  765.             if (get_post_meta( $post->ID, 'pwal_enable', true ) == 'enable') {
  766.                 $post_info['post'] = true;
  767.             }
  768.  
  769.             if (isset($this->options['post_types'][$post->post_type])) {
  770.                 $post_info['post'] = true;
  771.             }
  772.         }
  773.  
  774.         if ( is_singular($this->options['post_types'])) {
  775.             if ($this->pwal_js_data['debug'] == 'true') {
  776.                 echo "PWAL_DEBUG: ". __FUNCTION__ .": is_singular<br />";
  777.             }
  778.  
  779.             if ($post_info['post_type'] != true) {
  780.                 if ($this->pwal_js_data['debug'] == 'true') {
  781.                     echo "PWAL_DEBUG: ". __FUNCTION__ .": post type [". $post->post_type ."] not enabled<br />";
  782.                 }
  783.             } else {
  784.                 if ($this->pwal_js_data['debug'] == 'true') {
  785.                     echo "PWAL_DEBUG: ". __FUNCTION__ .": post type [". $post->post_type ."] is enabled<br />";
  786.                 }
  787.             }
  788.  
  789.             if ($post_info['post'] != true) {
  790.                 if ($this->pwal_js_data['debug'] == 'true') {
  791.                     echo "PWAL_DEBUG: ". __FUNCTION__ .": post not enabled<br />";
  792.                 }
  793.             } else {
  794.                 if ($this->pwal_js_data['debug'] == 'true') {
  795.                     echo "PWAL_DEBUG: ". __FUNCTION__ .": post is enabled<br />";
  796.                 }
  797.             }
  798.  
  799.             if ($this->pwal_js_data['debug'] == 'true') {
  800.                 echo "post_info<pre>"; print_r($post_info); echo "</pre>";
  801.             }
  802.  
  803.             if (($post_info['post'] != true) && ($post_info['post_type'] != true))
  804.                 return false;
  805.  
  806.         } else if (is_front_page()) {
  807.             if ($this->pwal_js_data['debug'] == 'true') {
  808.                 echo "PWAL_DEBUG: ". __FUNCTION__ .": is_front_page: true<br />";
  809.             }
  810.  
  811.             if ($this->options["home"] != 'true') {
  812.                 if ($this->pwal_js_data['debug'] == 'true') {
  813.                     echo "PWAL_DEBUG: ". __FUNCTION__ .": option(home) not true<br />";
  814.                 }
  815.                 return false;
  816.             } else {
  817.                 if ($this->pwal_js_data['debug'] == 'true') {
  818.                     echo "PWAL_DEBUG: ". __FUNCTION__ .": option(home) is true<br />";
  819.                 }
  820.             }
  821.  
  822.             if ($post_info['post_type'] != true) {
  823.                 if ($this->pwal_js_data['debug'] == 'true') {
  824.                     echo "PWAL_DEBUG: ". __FUNCTION__ .": post type [". $post->post_type ."] not enabled<br />";
  825.                 }
  826.             } else {
  827.                 if ($this->pwal_js_data['debug'] == 'true') {
  828.                     echo "PWAL_DEBUG: ". __FUNCTION__ .": post type [". $post->post_type ."] is enabled<br />";
  829.                 }
  830.             }
  831.  
  832.             if ($post_info['post'] != true) {
  833.                 if ($this->pwal_js_data['debug'] == 'true') {
  834.                     echo "PWAL_DEBUG: ". __FUNCTION__ .": post not enabled<br />";
  835.                 }
  836.             } else {
  837.                 if ($this->pwal_js_data['debug'] == 'true') {
  838.                     echo "PWAL_DEBUG: ". __FUNCTION__ .": post id enabled<br />";
  839.                 }
  840.             }
  841.  
  842.             if (($post_info['post'] != true) && ($post_info['post_type'] != true))
  843.                 return false;
  844.  
  845.         } else if ( is_archive() ) {
  846.             if ($this->pwal_js_data['debug'] == 'true') {
  847.                 echo "PWAL_DEBUG: ". __FUNCTION__ .": is_archive<br />";
  848.             }
  849.  
  850.             if ($this->options["multi"] != 'true') {
  851.                 if ($this->pwal_js_data['debug'] == 'true') {
  852.                     echo "PWAL_DEBUG: ". __FUNCTION__ .": option(multi): not true<br />";
  853.                 }
  854.                 return false;
  855.             } else {
  856.                 if ($this->pwal_js_data['debug'] == 'true') {
  857.                     echo "PWAL_DEBUG: ". __FUNCTION__ .": option(multi): true<br />";
  858.                 }
  859.             }
  860.  
  861.             if ($post_info['post_type'] != true) {
  862.                 if ($this->pwal_js_data['debug'] == 'true') {
  863.                     echo "PWAL_DEBUG: ". __FUNCTION__ .": post type [". $post->post_type ."] not enabled<br />";
  864.                 }
  865.             } else {
  866.                 if ($this->pwal_js_data['debug'] == 'true') {
  867.                     echo "PWAL_DEBUG: ". __FUNCTION__ .": post type [". $post->post_type ."] is enabled<br />";
  868.                 }
  869.             }
  870.  
  871.             if ($post_info['post'] != true) {
  872.                 if ($this->pwal_js_data['debug'] == 'true') {
  873.                     echo "PWAL_DEBUG: ". __FUNCTION__ .": post not enabled<br />";
  874.                 }
  875.             } else {
  876.                 if ($this->pwal_js_data['debug'] == 'true') {
  877.                     echo "PWAL_DEBUG: ". __FUNCTION__ .": post is enabled<br />";
  878.                 }
  879.             }
  880.  
  881.             if (($post_info['post'] != true) && ($post_info['post_type'] != true))
  882.                 return false;
  883.         }
  884.         return true;
  885.     }
  886.  
  887.  
  888.     /**
  889.      * Changes the content according to selected settings
  890.      *
  891.      */
  892.     function content( $content, $force=false ) {
  893.         if (is_admin()) return $content;
  894.  
  895.         //echo "DEBUG: in ". __FUNCTION__ .": ". __LINE__ ."<br />";
  896.         //return $content;
  897.  
  898.         global $post;
  899.  
  900.         // Find method
  901.         $method = get_post_meta( $post->ID, 'pwal_method', true );
  902.         if ($method == 'tool') {
  903.             // If this post is using the select tool (shortcode) the we let it pass and catch the processing there
  904.             if (stristr($content, '[pwal') !== false) {
  905.                 return $content;
  906.             }
  907.         }
  908.         // IF the post doesn't have a method defined use the global
  909.         if ( empty($method) ) {
  910.             $method = $this->options["method"];
  911.         }
  912.         // But check the post type to make sure it supports an excerpt.
  913.         if ($method == "manual") {
  914.             // If not set the method to auto.
  915.             if (!post_type_supports($post->post_type, 'excerpt')) {
  916.                 $method = 'automatic';
  917.             } else if ( empty($post->post_excerpt) ) {
  918.                 $method = 'automatic';
  919.             }
  920.         }
  921.         //echo "method[". $method ."]<br />";
  922.  
  923.  
  924.         // Unsupported post type
  925.         if ( !is_object( $post ) && !$content )
  926.             return $content;
  927.  
  928.  
  929.         $display_buttons = $this->can_display_buttons($post);
  930.         $display_buttons = apply_filters('pwal_display_buttons', $display_buttons, $post->ID, $post->ID);
  931.         if (!$display_buttons)
  932.             return $content;
  933.  
  934.  
  935.         $pwal_enable = $this->is_content_enabled( $post->ID );
  936.         if ($pwal_enable != 'enable') {
  937.             return $this->clear($content);
  938.         }
  939.  
  940.         // If user liked, show content. 'Tool' option has its own logic
  941.  
  942.         // "sitewide like" is selected
  943. //      if ( $this->options["sitewide"] )
  944. //          $post_id = $this->sitewide_id;
  945. //      else
  946. //          $post_id = $post->ID;
  947.  
  948.         // Check if this post is liked or sitewide like is selected
  949. //      foreach ( $this->cookie_likes['data'] as $like ) {
  950. //          // Cookie is already encrypted, so we are looking if post_id matches to the encryption
  951. //          if ( $like["post_id"] == md5( $post_id . $this->options["salt"] ) ) {
  952. //              return $this->clear($content);
  953. //              break;
  954. //          }
  955. //      }
  956.  
  957.         $description = get_post_meta( $post->ID, 'pwal_description', true );
  958.         if (empty($description))
  959.             $description = $this->options['description'];
  960.  
  961.         $content_reload = get_post_meta( $post->ID, 'pwal_content_reload', true );
  962.         if (empty($content_reload))
  963.             $content_reload = $this->options['content_reload'];
  964.  
  965.         $container_width = get_post_meta( $post->ID, 'pwal_container_width', true );
  966.         if (empty($container_width))
  967.             $container_width = $this->options['container_width'];
  968.  
  969.         $buttons_atts = array(
  970.             'content_id'        =>  $post->ID,
  971.             'post_id'           =>  $post->ID,
  972.             'container_width'   =>  $container_width,
  973.             'description'       =>  $description,
  974.             'content_reload'    =>  $content_reload
  975.         );
  976.  
  977.         //echo "button_atts<pre>"; print_r($buttons_atts); echo "</pre>";
  978.  
  979.         //if ( $this->facebook_check_like_post($post->ID) ) {
  980.         //  return $this->clear($content);
  981.         //  break;
  982.         //}
  983.  
  984.         //if ( $this->facebook_check_like_fan_pages() ) {
  985.         //  return $this->clear($content);
  986.         //  break;
  987.         //}
  988.  
  989.         // If we are here, it means content will be restricted.
  990.         // Now prepare the restricted output
  991.         if ( $method == "automatic" ) {
  992.             $buttons_atts['method'] = 'automatic';
  993.  
  994.             $content = preg_replace( '%\[pwal(.*?)\](.*?)\[( *)\/pwal( *)\]%is', '$2', $content ); // Clean shortcode
  995.             $temp_arr = explode( " ", $content );
  996.  
  997.             // If the article is shorter than excerpt, show full content
  998.             // Zero value is also valid
  999.             $excerpt_len = get_post_meta( $post->ID, 'pwal_excerpt', true );
  1000.             if ( empty($excerpt_len) ) {
  1001.                 $excerpt_len = $this->options["excerpt"];
  1002.             }
  1003.  
  1004.             if ( count( $temp_arr ) <= $excerpt_len ) {
  1005.                 if ($this->doing_set_cookie !== true) {
  1006.                     return $this->clear($content);
  1007.                 } else {
  1008.                     return '';
  1009.                 }
  1010.             }
  1011.  
  1012.             // Otherwise prepare excerpt
  1013.             $e = "";
  1014.             //for ( $n=0; $n<$excerpt_len; $n++ ) {
  1015.             //  $e .= $temp_arr[$n] . " ";
  1016.             //}
  1017.             //echo "e[". $e ."]<br />";
  1018.  
  1019.             $e_arr = array_slice($temp_arr, 0, intval($excerpt_len)-1);
  1020.             if ($e_arr) {
  1021.                 $e = join(' ', $e_arr);
  1022.             }
  1023.             //echo "e[". $e ."]<br />";
  1024.  
  1025.  
  1026.             // If a tag is broken, try to complete it within reasonable limits, i.e. 50 words
  1027.             if ( substr_count( $e, '<') != substr_count( $e, '>' ) ) {
  1028.                 // Save existing excerpt
  1029.                 $e_saved = $e;
  1030.                 $found = false;
  1031.                 for ( $n=$excerpt_len; $n<$excerpt_len+50; $n++ ) {
  1032.                     if ( isset( $temp_arr[$n] ) ) {
  1033.                         $e .= $temp_arr[$n] . " ";
  1034.                         if ( substr_count( $e, '<') == substr_count( $e, '>' ) ) {
  1035.                             $found = true;
  1036.                             break;
  1037.                         }
  1038.                     }
  1039.                 }
  1040.                 // Revert back to original excerpt if a fix is not found
  1041.                 if ( !$found )
  1042.                     $e = $e_saved;
  1043.             }
  1044.  
  1045.             if ($this->doing_set_cookie !== true) {
  1046.  
  1047.                 return '<div id="pwal_content_wrapper_'. $post->ID .'" class="pwal_content_wrapper">'. $e . $this->render_buttons( $buttons_atts ) .'</div>';
  1048.  
  1049.             } else {
  1050.                 //if (count($temp_arr) > ($n+1)) {
  1051.                 //  $e = join(' ', array_slice($temp_arr, $n));
  1052.                 //  //echo "e[". $e ."]<br />";
  1053.                 //  return $e;
  1054.                 //}
  1055.                 return $content;
  1056.             }
  1057.         }
  1058.         else if ( $method == "manual" ) {
  1059.             $buttons_atts['method'] = 'automatic';
  1060.             //echo "post method[". 'manual' ."] ID[". $post->ID ."]<br />";
  1061.  
  1062.             if ($this->doing_set_cookie !== true) {
  1063.                 return '<div id="pwal_content_wrapper_'. $post->ID .'" class="pwal_content_wrapper">'. $post->post_excerpt . $this->render_buttons( $buttons_atts ) .'</div>';
  1064.             } else {
  1065.                 return $this->clear($content);
  1066.             }
  1067.         }
  1068.         return $this->clear($content); // Script cannot come to this point, but just in case.
  1069.     }
  1070.  
  1071.     // Checked via Facebook PHP (server side) if the user has previously liked the post URL. If yes we can load the full content.
  1072.     function facebook_check_like_post($post_id = 0) {
  1073.  
  1074.         if (!$post_id) {
  1075.             return false;
  1076.         }
  1077.  
  1078.         if ((!$this->options['use_facebook']) || (!$this->options['facebook_api_key']) || (!$this->options['facebook_api_secret'])) {
  1079.             return false;
  1080.         }
  1081.  
  1082.         if (!$this->facebook_sdk_ref) {
  1083.             return false;
  1084.         }
  1085.  
  1086.         $this->facebook_sdk_load_user();
  1087.         if (!$this->facebook_user_profile) {
  1088.             return false;
  1089.         }
  1090.  
  1091.         $post_url = get_permalink($post_id);
  1092.  
  1093.         //Create Query
  1094.         $params = array(
  1095.             'method'    =>  'fql.query',
  1096.             'query'     =>  "SELECT url, user_id FROM url_like WHERE user_id = me() AND url='". $post_url. "'"
  1097.         );
  1098.         //echo "params<pre>"; print_r($params); echo "</pre>";
  1099.  
  1100.         //Run Query
  1101.         $result = $this->facebook_sdk_ref->api($params);
  1102.         //echo "result<pre>"; print_r($result); echo "</pre>";
  1103.         if ($result) {
  1104.             foreach($result as $result_item) {
  1105.                 if ($result_item['url'] == $post_url) {
  1106.                     return true;
  1107.                 }
  1108.             }
  1109.         }
  1110.     }
  1111.  
  1112.     function facebook_check_like_fan_pages() {
  1113.  
  1114.         if ((!$this->options['use_facebook']) || (!$this->options['facebook_api_key']) || (!$this->options['facebook_api_secret'])) {
  1115.             return false;
  1116.         }
  1117.  
  1118.         if (!$this->facebook_sdk_ref) {
  1119.             return false;
  1120.         }
  1121.  
  1122.         $this->facebook_sdk_load_user();
  1123.         if (!$this->facebook_user_profile) {
  1124.             return false;
  1125.         }
  1126.  
  1127.         if ((!$this->options['facebook_fan_pages']) || (!count($this->options['facebook_fan_pages'])))
  1128.             return false;
  1129.  
  1130.         $facebook_fan_pages = join(',', $this->options['facebook_fan_pages']);
  1131.  
  1132.         //Create Query
  1133.         // Use to check if FB user liked a FB page.
  1134.         $params = array (
  1135.             'method'    =>  'fql.query',
  1136.             'query'     => 'SELECT created_time, page_id, profile_section, type, uid FROM page_fan WHERE uid = me() AND page_id IN ('. $facebook_fan_pages .')'
  1137.         );
  1138.  
  1139.         //echo "params<pre>"; print_r($params); echo "</pre>";
  1140.  
  1141.         //Run Query
  1142.         $results = $this->facebook_sdk_ref->api($params);
  1143.         //echo "results<pre>"; print_r($results); echo "</pre>";
  1144.  
  1145.         if ($results) {
  1146.             foreach($results as $result) {
  1147.                 if (array_search($result['page_id'], $this->options['facebook_fan_pages']) !== false) {
  1148.                     //echo "match found (at least one)<br />";
  1149.                     return true;
  1150.                 }
  1151.             }
  1152.         }
  1153.     }
  1154.  
  1155. //  function facebook_sdk_setup() {
  1156. //      if (!$this->facebook_sdk_ref) {
  1157. //          //include_once( dirname(__FILE__) .'/lib/facebook-php-sdk/facebook.php');
  1158. //
  1159. //          $this->facebook_sdk_ref = new PWALFacebook(array(
  1160. //              'appId'  => $this->options['facebook_api_key'],
  1161. //              'secret' => $this->options['facebook_api_secret'],
  1162. //              )
  1163. //          );
  1164. //      }
  1165. //  }
  1166.  
  1167.     function facebook_sdk_load_user() {
  1168.         //echo "facebook_sdk_ref<pre>"; print_r($this->facebook_sdk_ref); echo "</pre>";
  1169.  
  1170.         $user_id = $this->facebook_sdk_ref->getUser();
  1171.         //echo "user_id[". $user_id ."]<br />";
  1172.  
  1173.         if ($user_id) {
  1174.             try {
  1175.                 $this->facebook_user_profile = $this->facebook_sdk_ref->api('/me','GET');
  1176.  
  1177.             } catch(PWALFacebookApiException $e) {
  1178.                 //echo "ERROR:<pre>"; print_r($e); echo "</pre>";
  1179.                 return false;
  1180.             }
  1181.         } else {
  1182.             //echo "user_id is invalid<br />";
  1183.             return false;
  1184.         }
  1185.     }
  1186.  
  1187.     /**
  1188.      * Try to clear remaining shortcodes
  1189.      *
  1190.      */
  1191.     function clear( $content ) {
  1192.         // Don't even try to touch an object, just in case
  1193.         if ( is_object( $content ) )
  1194.             return $content;
  1195.         else {
  1196.             $content = preg_replace( '%\[pwal(.*?)\]%is', '', $content );
  1197.             $content = preg_replace( '%\[\/pwal\]%is', '', $content );
  1198.             return $content;
  1199.         }
  1200.     }
  1201.  
  1202.     /**
  1203.      *  Number of active buttons
  1204.      */
  1205.     function button_count( ) {
  1206.         $n = 0;
  1207.         if ( $this->options["use_facebook"] )
  1208.             $n++;
  1209.         if ( $this->options["use_linkedin"] )
  1210.             $n++;
  1211.         if ( $this->options["use_twitter"] )
  1212.             $n++;
  1213.         if ( $this->options["use_google"] )
  1214.             $n++;
  1215.         $n = apply_filters( "pwal_active_button_count", $n );
  1216.         return $n;
  1217.     }
  1218.  
  1219.     function get_buttons_show() {
  1220.         $pwal_buttons_show = array();
  1221.  
  1222.         // We make a copy of the buttons because we run an unset on the ones used.
  1223.         $social_buttons_tmp = $this->options['social_buttons'];
  1224.  
  1225.         if (!empty($this->options['social_button_sort'])) {
  1226.             foreach($this->options['social_button_sort'] as $button_key) {
  1227.  
  1228.                 if (isset($social_buttons_tmp[$button_key])) {
  1229.                     $pwal_buttons_show[$button_key] = $social_buttons_tmp[$button_key];
  1230.                     unset($social_buttons_tmp[$button_key]);
  1231.                 }
  1232.             }
  1233.         }
  1234.  
  1235.         // Append any missing ones to the end of the show array.
  1236.         if (!empty($social_buttons_tmp)) {
  1237.             foreach($social_buttons_tmp as $button_key => $button_label) {
  1238.                 $pwal_buttons_show[$button_key] = $social_buttons_tmp[$button_key];
  1239.             }
  1240.         }
  1241.         foreach($pwal_buttons_show as $button_key => $button_label) {
  1242.             if ((!isset($this->options['use_'.$button_key])) || ($this->options['use_'.$button_key] != 'true'))
  1243.                 unset($pwal_buttons_show[$button_key]);
  1244.         }
  1245.  
  1246.         return $pwal_buttons_show;
  1247.     }
  1248.     /**
  1249.      *  Add button html codes and handle post based embedded scripts
  1250.      */
  1251.     function render_buttons( $atts) {
  1252.         //global $post;
  1253.  
  1254.         //echo "atts (before)<pre>"; print_r($atts); echo "</pre>";
  1255.         $default_atts = array(
  1256.             'content_id'        =>  0,
  1257.             'post_id'           =>  0,
  1258.             'container_width'   =>  $this->options['container_width'],
  1259.             'description'       =>  $this->options['description'],
  1260.             'content_reload'    =>  $this->options['content_reload'],
  1261.             'method'            =>  $this->options['method']
  1262.         );
  1263.         $atts = shortcode_atts( $default_atts, $atts );
  1264.         //echo "atts (after)<pre>"; print_r($atts); echo "</pre>";
  1265.  
  1266.         $n = $this->button_count();
  1267.         //echo "button_count[". $n ."]<br />";
  1268.         if ( $n == 0 )
  1269.             return; // No button selected. Nothing to do.
  1270.  
  1271.         // Check if the post has a alternate URL to Like
  1272.         $url_to_like = '';
  1273.         if ((isset($atts['post_id'])) && (!empty($atts['post_id']))) {
  1274.             $url_to_like = get_post_meta( $atts['post_id'], 'pwal_url_to_like', true );
  1275.         }
  1276.        
  1277.         // Added by mike
  1278.         // Check if the post has a alternate URL to Like
  1279.         $furl_to_like = '';
  1280.         if ((isset($atts['post_id'])) && (!empty($atts['post_id']))) {
  1281.             $furl_to_like = get_post_meta( $atts['post_id'], 'pwal_furl_to_like', true );
  1282.         }
  1283.  
  1284.         // Check if the post has a alternate URL to Like
  1285.         $gurl_to_like = '';
  1286.         if ((isset($atts['post_id'])) && (!empty($atts['post_id']))) {
  1287.             $gurl_to_like = get_post_meta( $atts['post_id'], 'pwal_gurl_to_like', true );
  1288.         }
  1289.         // End Added by Mike
  1290.  
  1291.         // If url to like is left empty and Random Like is not selected, take existing post's url
  1292.         if (empty($url_to_like)) {
  1293.  
  1294.             // If Random Like is selected, find a random, published post/page
  1295.             if ( $this->options["random"] || apply_filters( 'pwal_force_random', false ) ) {
  1296.                 global $wpdb;
  1297.  
  1298.                 if (count($this->options['post_types'])) {
  1299.                     $post_types_sql = '';
  1300.                     foreach($this->options['post_types'] as $post_type) {
  1301.                         if (!empty($post_types_sql)) $post_types_sql .= ',';
  1302.                         $post_types_sql .= "'". $post_type ."'";
  1303.                     }
  1304.                 }
  1305.  
  1306.                 $result = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE (post_type IN ('. $post_types_sql .')) AND post_status='publish' ORDER BY RAND()");
  1307.                 if ( $result != null ) {
  1308.                     $url_to_like = get_permalink( $result->ID );
  1309.                 } else {
  1310.                     return;
  1311.                 }
  1312.             }
  1313.  
  1314.             if (empty($url_to_like)) {
  1315.                 if (!empty($this->options["url_to_like"])) {
  1316.                     $url_to_like =  $this->options["url_to_like"];
  1317.                 }
  1318.             }
  1319.  
  1320.             if (empty($url_to_like)) {
  1321.                 //echo "here atts<pre>"; print_r($atts); echo "</pre>";
  1322.                 if ($atts['post_id']) {
  1323.                     $url_to_like = get_permalink( $atts['post_id'] );
  1324.                 } else {
  1325.                     $url_to_like = add_query_arg('PWAL_DEBUG', '1');
  1326.                     $url_to_like = remove_query_arg('PWAL_DEBUG', $url_to_like);
  1327.                 }
  1328.             }
  1329.         }
  1330.  
  1331.         $url_to_like = remove_query_arg('PWAL_DEBUG', $url_to_like);
  1332.         $url_to_like = apply_filters( 'pwal_url_to_like', $url_to_like );
  1333.  
  1334.         // Added by Mike
  1335.         $furl_to_like = apply_filters( 'pwal_furl_to_like', $furl_to_like );
  1336.         $gurl_to_like = apply_filters( 'pwal_gurl_to_like', $gurl_to_like );
  1337.         // End adde by Mike
  1338.  
  1339.         if ($this->pwal_js_data['debug'] == 'true') {
  1340.             echo "PWAL_DEBUG: ". __FUNCTION__ .": url_to_like[". $url_to_like ."]<br />";
  1341.         }
  1342.  
  1343.         //echo "url_to_like[". $url_to_like ."]<br />";
  1344.  
  1345.         //echo "atts content_width is empty<br/>";
  1346.         if (empty($atts['container_width'])) {
  1347.  
  1348.             if ((isset($atts['post_id'])) && (!empty($atts['post_id']))) {
  1349.                 $atts['container_width'] = get_post_meta( $atts['post_id'], 'pwal_container_width', true );
  1350.             }
  1351.  
  1352.             if (empty($atts['container_width'])) {
  1353.                 global $content_width;
  1354.                 if ((isset($content_width)) && (!empty($content_width))) {
  1355.                     $atts['container_width'] = $content_width .'px';
  1356.                 }
  1357.             }
  1358.         }
  1359.         //echo "atts<pre>"; print_r($atts); echo "</pre>";
  1360.  
  1361.         if (!empty($atts['container_width']))
  1362.             $pwal_container_style = ' width:'. $this->check_size_qualifier($atts['container_width']) .';';
  1363.         else
  1364.             $pwal_container_style = '';
  1365.         //echo "pwal_container_style[". $pwal_container_style ."]<br />";
  1366.         if (!empty($pwal_container_style)) {
  1367.             $pwal_container_style = ' style="'. $pwal_container_style .'" ';
  1368.         }
  1369.  
  1370.         $content  = '<div class="pwal_container" id="pwal_container_'. $atts['content_id'] .'"'. $pwal_container_style .'>';
  1371.         if ( $atts['description'] )
  1372.             $content .= '<div class="pwal_description">'. $atts['description'] . '</div>';
  1373.         $content .= '<div class="pwal_buttons">';
  1374.         $script   = "";
  1375.  
  1376.         // Now show the buttons.
  1377.         $pwal_buttons_show = $this->get_buttons_show();
  1378.         if ((is_array($pwal_buttons_show)) && (!empty($pwal_buttons_show))) {
  1379.             foreach($pwal_buttons_show as $button_key => $button_label) {
  1380.  
  1381.                 //$content .= "<li class='pwal_list_item_". $n ."'>";
  1382.  
  1383.                 switch($button_key) {
  1384.                     case 'facebook':
  1385.                         if ( $this->options["use_facebook"] ) {
  1386.                             //echo "options<pre>"; print_r($this->options); echo "</pre>";
  1387.  
  1388.                             //$content .= "<div class='pwal_button pwal_button_".$n." pwal_button_facebook'><fb:like href='". $url_to_like ."' ref='pwal_facebook_". $atts['content_id'] ."' class='pwal_facebook_iframe' id='pwal_facebook_". $atts['content_id'] ."'  ";
  1389.                            
  1390.                             // Added by Mike
  1391.                             $content .= "<div class='pwal_button pwal_button_".$n." pwal_button_facebook'><fb:like href='";
  1392.                             if (!empty($furl_to_like)) {
  1393.                                 $content .= $furl_to_like;
  1394.                             } else {
  1395.                                 $content .= "http://www.customurl.com";
  1396.                             }
  1397.  
  1398.                             $content .= "' ref='pwal_facebook_". $atts['content_id'] ."' class='pwal_facebook_iframe' id='pwal_facebook_". $atts['content_id'] ."'  ";
  1399.                             // End Added by Mike
  1400.  
  1401.                             if (!empty($this->options['facebook_layout_style'])) {
  1402.                                 $content .= ' layout="'. $this->options['facebook_layout_style'] .'"';
  1403.                             }
  1404.                             //if (!empty($this->options['facebook_color_scheme'])) {
  1405.                             //  $content .= ' colorscheme="'. $this->options['facebook_color_scheme'] .'"';
  1406.                             //}
  1407.                             if (!empty($this->options['facebook_verb'])) {
  1408.                                 $content .= ' action="'. $this->options['facebook_verb'] .'"';
  1409.                             }
  1410.                             if ($this->options['facebook_include_faces'] == 'yes') {
  1411.                                 $content .= ' show_faces="true"';
  1412.                             }
  1413.                             if ($this->options['facebook_include_share'] == 'yes') {
  1414.                                 $content .= ' share="true"';
  1415.                             }
  1416.                             $content .= "></div>";
  1417.                         }
  1418.                         break;
  1419.  
  1420.                     case 'linkedin':
  1421.                         if ( $this->options["use_linkedin"] ) {
  1422.                             $linkedin_layout_style = $this->options['linkedin_layout_style'];
  1423.                             if (empty($linkedin_layout_style)) {
  1424.                                 $linkedin_layout_style = 'top';
  1425.                             }
  1426.                             if ($linkedin_layout_style != 'none') {
  1427.                                 $linkedin_data_counter = " data-counter='". $linkedin_layout_style ."' ";
  1428.                             }
  1429.  
  1430.                             $content .= "<div class='pwal_button pwal_button_".$n." pwal_button_linkedin'><script type='IN/Share' ". $linkedin_data_counter ." data-url='". $url_to_like ."' data-onsuccess='pwal_linkedin_callback_". $atts['content_id'] ."'></script><script type='text/javascript'>function pwal_linkedin_callback_". $atts['content_id'] ."(){ wpmudev_pwal.setup_linkedin_js('". $atts['content_id'] ."'); } </script></div>";
  1431.                         }
  1432.                         break;
  1433.  
  1434.                     case 'twitter':
  1435.                         if ( $this->options["use_twitter"] ) {
  1436.                             $twitter_layout_style = $this->options['twitter_layout_style'];
  1437.                             if (empty($twitter_layout_style)) {
  1438.                                 $twitter_layout_style = 'vertical';
  1439.                             }
  1440.  
  1441.                             //$twitter_button_size = $this->options['twitter_button_size'];
  1442.                             //if (empty($twitter_button_size)) {
  1443.                                 $twitter_button_size = 'medium';
  1444.                             //}
  1445.                             $twitter_button_lang = $this->options['twitter_button_lang'];
  1446.                             if (empty($twitter_button_lang)) {
  1447.                                 $twitter_button_lang = get_locale();;
  1448.                             }
  1449.  
  1450.                             $twitter_message = $this->options['twitter_message'];
  1451.                             if (!empty($twitter_message)) {
  1452.                                 if ((isset($atts['post_id'])) && (intval($atts['post_id']))) {
  1453.                                     $twitter_message = str_replace('[POST_TITLE]', get_the_title($atts['post_id']), $twitter_message);
  1454.                                 } else {
  1455.                                     $twitter_message = str_replace('[POST_TITLE]', '', $twitter_message);
  1456.                                 }
  1457.                                 $twitter_message = str_replace('[SITE_TITLE]', get_option('blogname'), $twitter_message);
  1458.                                 $twitter_message = str_replace('[SITE_TAGLINE]', get_option('blogdescription'), $twitter_message);
  1459.                             }
  1460.                             $twitter_message = apply_filters('pwal_twitter_message', $twitter_message, $atts['post_id']);
  1461.                             if (!empty($twitter_message)) {
  1462.                                 $twitter_data_message = ' data-text="'. htmlentities($twitter_message) .'" ';
  1463.                             } else {
  1464.                                 $twitter_data_message = '';
  1465.                             }
  1466.  
  1467.                             $content .= "<div class='pwal_button pwal_button_".$n." pwal_button_twitter' id='pwal_twitter_". $atts['content_id'] ."' ><a href='https://twitter.com/share' class='twitter-share-button' ". $twitter_data_message ." data-url='". $url_to_like ."' data-size='". $twitter_button_size ."' data-count='". $twitter_layout_style ."' data-lang='". $twitter_button_lang ."'>Tweet</a></div>";
  1468.                         }
  1469.                         break;
  1470.  
  1471.                     case 'google':
  1472.                         if ( $this->options["use_google"] ) {
  1473.  
  1474.                             $google_layout_style = $this->options['google_layout_style'];
  1475.                             if (empty($google_layout_style)) {
  1476.                                     $google_layout_style = 'tall-bubble';
  1477.                             }
  1478.                             list($google_button_size, $google_button_annotation) = explode('-', $google_layout_style);
  1479.  
  1480.                             //$content .= "<div class='pwal_button pwal_button_".$n." pwal_button_google'><g:plusone size='". $google_button_size ."' href='". $url_to_like ."' annotation='". $google_button_annotation ."' callback='pwal_google_callback_". $atts['content_id'] ."'></g:plusone><script type='text/javascript'> function pwal_google_callback_". $atts['content_id'] ."(data){ wpmudev_pwal.google_plus_callback_js('".$atts['content_id'] ."', data); } </script></div>";
  1481.                            
  1482.                             // Added by Mike
  1483.                             $content .= "<div class='pwal_button pwal_button_".$n." pwal_button_google'><g:plusone size='". $google_button_size ."' href='";
  1484.                             if (!empty($gurl_to_like)) {
  1485.                                 $content .= $gurl_to_like;
  1486.                             } else {
  1487.                                 $content .= "http://www.customurl.com";
  1488.                             }
  1489.                             $content .= "' annotation='". $google_button_annotation ."' callback='pwal_google_callback_". $atts['content_id'] ."'></g:plusone><script type='text/javascript'> function pwal_google_callback_". $atts['content_id'] ."(data){ wpmudev_pwal.google_plus_callback_js('".$atts['content_id'] ."', data); } </script></div>";
  1490.                             // End Added by Mike
  1491.  
  1492.                                        
  1493.                         }
  1494.                         break;
  1495.                 }
  1496.                 //$content .= '</li>';
  1497.             }
  1498.         }
  1499.         $content .= "</div>";
  1500.  
  1501.         if (!empty($content)) {
  1502.             $content .= "</div>";
  1503.  
  1504.             $content = apply_filters( "pwal_render_button_html", $content, $atts['content_id'], $atts['post_id'] );
  1505.  
  1506.             if (!isset($this->pwal_js_data['buttons']))
  1507.                 $this->pwal_js_data['buttons']      = array();
  1508.  
  1509.             if (!isset($this->pwal_js_data['buttons'][$atts['content_id']])) {
  1510.                 $this->pwal_js_data['buttons'][$atts['content_id']]                     = array();
  1511.                 $this->pwal_js_data['buttons'][$atts['content_id']]['content_id']       = $atts['content_id'];
  1512.                 $this->pwal_js_data['buttons'][$atts['content_id']]['post_id']          = $atts['post_id'];
  1513.                 $this->pwal_js_data['buttons'][$atts['content_id']]['href']             = $url_to_like;
  1514.                 $this->pwal_js_data['buttons'][$atts['content_id']]['content_reload']   = $atts['content_reload'];
  1515.                 $this->pwal_js_data['buttons'][$atts['content_id']]['method']           = $atts['method'];
  1516.             }
  1517.  
  1518.             //$script  .= " wpmudev_pwal.register_button_href('". $atts['id'] ."', '". $atts['post_id'] ."','". $url_to_like ."'); ";
  1519.             //$script  = apply_filters( "pwal_render_button_script", $script, $atts['id'], $atts['post_id'] );
  1520.  
  1521.             // Save scripts to be added to the footer
  1522.             //$this->footer_script .= $script;
  1523.  
  1524.             // Flag that we have already included some common scripts
  1525.             $this->buttons_added = true;
  1526.         }
  1527.         return $content;
  1528.     }
  1529.  
  1530.     /**
  1531.      *  Add embedded scripts to the footer and compress them
  1532.      */
  1533.     function footer() {
  1534.  
  1535.         if ($this->buttons_added == true) {
  1536.             $this->pwal_js_data['options']      = $this->options;
  1537.  
  1538.             // For the facebook fan pages we remove all the details and use only the page ids.
  1539.             if ((isset($this->pwal_js_data['options']['facebook_fan_pages'])) && (!empty($this->pwal_js_data['options']['facebook_fan_pages']))) {
  1540.                 $this->pwal_js_data['options']['facebook_fan_pages'] = array_keys($this->pwal_js_data['options']['facebook_fan_pages']);
  1541.             }
  1542.             $this->pwal_js_data['ajax_url']         = admin_url('admin-ajax.php');
  1543.             $this->pwal_js_data['ajax-nonce']       = wp_create_nonce("pwal-ajax-nonce");
  1544.             $this->pwal_js_data['cookie_key']       = $this->cookie_key;
  1545.  
  1546.             if ( defined('COOKIEPATH') ) $cookiepath = COOKIEPATH;
  1547.             else $cookiepath = "/";
  1548.             $this->pwal_js_data['COOKIEPATH']   = $cookiepath;
  1549.  
  1550.             if ( defined('COOKIEDOMAIN') ) $cookiedomain = COOKIEDOMAIN;
  1551.             else $cookiedomain = '';
  1552.             $this->pwal_js_data['COOKIEDOMAIN'] = $cookiedomain;
  1553.  
  1554.             wp_localize_script('pay-with-a-like-js', 'pwal_data', $this->pwal_js_data);
  1555.             echo "<script type='text/javascript'>jQuery(document).ready(function($) { ". $this->footer_script. " }); </script>";
  1556.             wp_print_scripts('pay-with-a-like-js');
  1557.         } else {
  1558.             if (!empty($this->_registered_scripts)) {
  1559.                 foreach($this->_registered_scripts as $_handle) {
  1560.                     wp_dequeue_script($_handle);
  1561.                 }
  1562.             }
  1563.         }
  1564.  
  1565.     }
  1566.  
  1567.     function handle_buttons_action() {
  1568.  
  1569.         if (!check_ajax_referer( 'pwal-ajax-nonce', 'nonce', false )) {
  1570.             $reply_data = array();
  1571.             $reply_data['errorStatus']  = true;
  1572.             $reply_data['errorText']    = __('Something went wrong. Please refresh the page and try again. (nonce)', 'pwal');
  1573.             die( json_encode( $reply_data ) );
  1574.         }
  1575.  
  1576.         $this->doing_set_cookie = true;
  1577.  
  1578.         if (!isset($_POST['pwal_info_items'])) {
  1579.             $reply_data = array();
  1580.             $reply_data['errorStatus']  = true;
  1581.             $reply_data['errorText']    = __('Something went wrong. Please refresh the page and try again. (pwal_info_items)', 'pwal');
  1582.             die( json_encode( $reply_data ) );
  1583.         }
  1584.  
  1585.         $reply_data = array();
  1586.         $reply_data['current_time'] = current_time('mysql', true);
  1587.  
  1588.         // Set our global reply status. We will also have one per pwal_info item
  1589.         $reply_data['errorStatus']  = false;
  1590.         $reply_data['errorText']    = '';
  1591.  
  1592.         $reply_data['pwal_info_items'] = $_POST['pwal_info_items'];
  1593.  
  1594.         if ((is_array($reply_data['pwal_info_items'])) && (count($reply_data['pwal_info_items']))) {
  1595.  
  1596.             foreach($reply_data['pwal_info_items'] as $pwal_id => $pwal_info) {
  1597.                 $pwal_info['reply_data']['errorStatus']     = false;
  1598.                 $pwal_info['reply_data']['errorText']       = '';
  1599.  
  1600.                 if (!isset($pwal_info['post_id'])) {
  1601.                     $pwal_info['reply_data']['errorStatus'] = true;
  1602.                     $pwal_info['reply_data']['errorText']   = __('Something went wrong. Please refresh the page and try again. (post_id)', 'pwal');
  1603.  
  1604.                 } else {
  1605.                     $pwal_info['post_id'] = intval($pwal_info['post_id']);
  1606.                 }
  1607.  
  1608.                 if (!isset($pwal_info['content_id'])) {
  1609.                     $pwal_info['reply_data']['errorStatus'] = true;
  1610.                     $pwal_info['reply_data']['errorText']   = __('Something went wrong. Please refresh the page and try again. (content_id)', 'pwal');
  1611.                 } else {
  1612.                     $pwal_info['content_id'] = intval($pwal_info['content_id']);
  1613.                 }
  1614.  
  1615.                 if (intval($pwal_info['post_id'])) {
  1616.                     $post = get_post(intval($pwal_info['post_id']));
  1617.                     //if (!$post) {
  1618.                     //  $pwal_info['reply_data']['errorStatus']     = true;
  1619.                     //  $pwal_info['reply_data']['errorText']   = __('Something went wrong. Please refresh the page and try again. (invalid post_id)', 'pwal');
  1620.                     //}
  1621.                 } else {
  1622.                     // In the rare cases where the post_id is zero like when using the pwal template or shortcode outside of at post...
  1623.                     // We set the post_id to be the value of the content_id since that is ALWAYS present.
  1624.                     $pwal_info['post_id'] = $pwal_info['id'];
  1625.                 }
  1626.  
  1627.                 if ($pwal_info['reply_data']['errorStatus'] == false) {
  1628.                     $this->handle_button_cookie($pwal_info);
  1629.                     $this->handle_button_statistics($pwal_info);
  1630.                     $pwal_info['content'] = $this->handle_button_content($pwal_info, $post);
  1631.  
  1632.                     // Do this last to allow external processes to be triggered by the PWAL button action
  1633.                     do_action("pwal_ajax_request", $pwal_info['post_id'], $pwal_info['content_id']);
  1634.                 }
  1635.                 $reply_data['pwal_info_items'][$pwal_id] = $pwal_info;
  1636.                 $this->set_cookie_likes();
  1637.             }
  1638.             $reply_data['cookie'] = $this->cookie_likes;
  1639.         }
  1640.         die( json_encode( $reply_data ) );
  1641.     }
  1642.  
  1643.     function handle_button_cookie($pwal_info) {
  1644.  
  1645.         //echo "pwal_info<pre>"; print_r($pwal_info); echo "</pre>";
  1646.         //echo "options<pre>"; print_r($this->options); echo "</pre>";
  1647.  
  1648.         $new_like = array(
  1649.             'content_id'    => $pwal_info['content_id'],
  1650.             'post_id'       => md5( $pwal_info['post_id'] . $this->options["salt"] )
  1651.         );
  1652.         //echo "new_like<pre>"; print_r($new_like); echo "</pre>";
  1653.  
  1654.         // Always save the "sitewide like" thing
  1655.         $new_like_sitewide = array(
  1656.             'content_id'    => $this->sitewide_id,
  1657.             'post_id'       => md5( $this->sitewide_id . $this->options["salt"] )
  1658.         );
  1659.         //echo "new_like_sitewide<pre>"; print_r($new_like_sitewide); echo "</pre>";
  1660.  
  1661.         // Check if user has got likes saved in the cookie
  1662.  
  1663. //      echo "DEBUG: new_like<pre>"; print_r($new_like); echo "</pre>";
  1664. //      echo "DEBUG: cookie_likes<pre>"; print_r($this->cookie_likes); echo "</pre>";
  1665. //      die();
  1666.  
  1667.         // Prevent cookie growing with duplicate entries
  1668.         $duplicate = false;
  1669.         foreach ( $this->cookie_likes['data'] as $like ) {
  1670.             if ( isset( $like['content_id'] ) && $like['content_id'] == $new_like['content_id'] ) {
  1671.                 //echo "DEBUG: Found Duplicate<br />";
  1672.                 $duplicate = true;
  1673.                 break; // One match is enough
  1674.             }
  1675.         }
  1676.         //echo "DEBUG: duplicate[". $duplicate ."]<br />";
  1677.         //die();
  1678.  
  1679.         // IF we don't have a duplicate for the liked item we add it.
  1680.         if ( !$duplicate )
  1681.             $this->cookie_likes['data'][$new_like['content_id']] = $new_like;
  1682.  
  1683.  
  1684.         /** Now processing the Sitewide Like cookie. We always add this  */
  1685.         $duplicate_sitewide = false;
  1686.         foreach ( $this->cookie_likes['data'] as $like ) {
  1687.             if ( isset( $like['content_id'] ) && $like['content_id'] == $new_like_sitewide['content_id'] ) {
  1688.                 $duplicate_sitewide = true;
  1689.                 break;
  1690.             }
  1691.         }
  1692.         if ( !$duplicate_sitewide )
  1693.             $this->cookie_likes['data'][$new_like_sitewide['content_id']] = $new_like_sitewide;
  1694.  
  1695.         // Clear empty entries, just in case
  1696.         $this->cookie_likes['data'] = array_filter( $this->cookie_likes['data'] );
  1697.         //$reply_data['likes'] = $this->cookie_likes;
  1698.  
  1699.         //echo "cookie_likes<pre>"; print_r($this->cookie_likes); echo "</pre>";
  1700.         //return $likes;
  1701.     }
  1702.  
  1703.     function load_cookie_likes() {
  1704.         if ( isset( $_COOKIE[$this->cookie_key] ) ) {
  1705.             //if (!defined( 'DOING_AJAX' ) || !DOING_AJAX) {
  1706.             //  if ($this->pwal_js_data['debug'] == 'true') {
  1707.             //      echo "PWAL_DEBUG: ". __FUNCTION__ .": _COOKIE[". $this->cookie_key ."]<pre>". print_r($_COOKIE[$this->cookie_key], true) ."</pre>";
  1708.             //  }
  1709.             //}
  1710.             //$this->cookie_likes = unserialize( stripslashes( $_COOKIE[$this->cookie_key] ) );
  1711.             $this->cookie_likes = json_decode( stripslashes( $_COOKIE[$this->cookie_key] ), true );
  1712.             //$this->chat_user = json_decode(stripslashes($_COOKIE['wpmudev-chat-user']), true);
  1713.         } else {
  1714.             $this->cookie_likes = array();
  1715.         }
  1716.  
  1717.         //if (!defined( 'DOING_AJAX' ) || !DOING_AJAX) {
  1718.         //  if ($this->pwal_js_data['debug'] == 'true') {
  1719.         //      echo "PWAL_DEBUG: ". __FUNCTION__ .": cookie_likes<pre>". print_r($this->cookie_likes, true) ."</pre>";
  1720.         //  }
  1721.         //}
  1722.  
  1723.         $upgrade_cookies = false;
  1724.         if (!isset($this->cookie_likes['version'])) {
  1725.             $this->cookie_likes['version'] = $this->version;
  1726.             $upgrade_cookies = true;
  1727.         }
  1728.  
  1729.         if (!isset($this->cookie_likes['data'])) {
  1730.             $this->cookie_likes['data'] = array();
  1731.             $upgrade_cookies = true;
  1732.         }
  1733.         if ($upgrade_cookies == true) {
  1734.             $cookie_likes = $this->cookie_likes;
  1735.             $this->cookie_likes = array();
  1736.             if (isset($cookie_likes['version'])) {
  1737.                 $this->cookie_likes['version'] = $cookie_likes['version'];
  1738.                 unset($cookie_likes['version']);
  1739.             }
  1740.             if (isset($cookie_likes['data'])) {
  1741.                 $this->cookie_likes['data'] = $cookie_likes['data'];
  1742.                 unset($cookie_likes['data']);
  1743.             }
  1744.  
  1745.             foreach($cookie_likes as $cookie_id => $cookie_like) {
  1746.                 //echo "cookie_like<pre>"; print_r($cookie_like); echo "</pre>";
  1747.  
  1748.                 if (is_array($cookie_like)) {
  1749.                     if ((isset($cookie_like['post_id'])) && (isset($cookie_like['content_id']))) {
  1750.                         // We found a data item. So save it there.
  1751.                         $this->cookie_likes['data'][$cookie_like['content_id']] = $cookie_like;
  1752.                         //unset($this->cookie_likes[$cookie_id]);
  1753.                     }
  1754.                 }
  1755.             }
  1756.             $this->set_cookie_likes();
  1757.         }
  1758.  
  1759.         //if (!defined( 'DOING_AJAX' ) || !DOING_AJAX) {
  1760.         //  if ($this->pwal_js_data['debug'] == 'true') {
  1761.         //      echo "PWAL_DEBUG: ". __FUNCTION__ .": cookie_likes<pre>". print_r($this->cookie_likes, true) ."</pre>";
  1762.         //  }
  1763.         //}
  1764.  
  1765.         //echo "after cookie_likes<pre>"; print_r($this->cookie_likes); echo "</pre>";
  1766.         //die();
  1767.     }
  1768.  
  1769.  
  1770.     function set_cookie_likes() {
  1771.         // Let admin set cookie expire at the end of session
  1772.         if ( $this->options['cookie'] == 0  || trim( $this->options['cookie'] ) == '' )
  1773.             $expire = 0;
  1774.         else
  1775.             $expire = time() + 3600 * $this->options['cookie'];
  1776.  
  1777.         if ( defined('COOKIEPATH') ) $cookiepath = COOKIEPATH;
  1778.         else $cookiepath = "/";
  1779.         if ( defined('COOKIEDOMAIN') ) $cookiedomain = COOKIEDOMAIN;
  1780.         else $cookiedomain = '';
  1781.  
  1782.         // Always update the version to the cookie in case we change the structre later.
  1783.         $this->cookie_likes['version'] = $this->version;
  1784.  
  1785.         // Setting cookie works in ajax request!!
  1786.         //@setcookie("pay_with_a_like", serialize($this->cookie_likes), $expire, $cookiepath, $cookiedomain);
  1787.  
  1788.         // It looks like FF cannot write the cookie immediately. Let's put a delay.
  1789.         //sleep(1);
  1790.     }
  1791.  
  1792.     function handle_button_statistics($pwal_info) {
  1793.         if ((isset($pwal_info["service"])) && (!empty($pwal_info["service"]))) {
  1794.  
  1795.             // We can handle statistics here, as this is ajax request and it will not affect performance
  1796.             $statistics = get_option( "pwal_statistics" );
  1797.             if ( !is_array( $statistics  ) )
  1798.                 $statistics = array();
  1799.  
  1800.             global $blog_id;
  1801.  
  1802.             // Let's try to be economical in key usage
  1803.             $statistics[] = array(
  1804.                             'b'     => $blog_id,
  1805.                             'p'     => $pwal_info['post_id'],
  1806.                             'c'     => $pwal_info['content_id'],
  1807.                             's'     => $pwal_info['service'],
  1808.                             'i'     => $_SERVER['REMOTE_ADDR'],
  1809.                             't'     => current_time('timestamp')
  1810.                             );
  1811.  
  1812.             $statistics = apply_filters( "pwal_ajax_statistics", $statistics, $pwal_info["post_id"], $pwal_info["service"] );
  1813.  
  1814.             update_option( "pwal_statistics", $statistics );
  1815.         }
  1816.     }
  1817.  
  1818.     function handle_button_content($pwal_info, $post) {
  1819.  
  1820.         $content = '';
  1821.  
  1822.         //echo "in ". __FUNCTION__ ."<br />";
  1823.         //echo "pwal_info<pre>"; print_r($pwal_info); echo "</pre>";
  1824.         //echo "post_content[". $post->post_content ."]<br />";
  1825.  
  1826.         if ((isset($pwal_info['content_reload'])) && ($pwal_info['content_reload'] == "ajax")) {
  1827.             if ((isset($pwal_info['method'])) && ($pwal_info['method'] == 'tool')) {
  1828.  
  1829.                 //$pwal_info['content'] = '';
  1830.                 //if ((is_object($post)) && (has_shortcode( $post->post_content, 'pwal' ))) {
  1831.                 //
  1832.                 //  $pattern = get_shortcode_regex();
  1833.                 //  preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches, PREG_SET_ORDER );
  1834.                 //  if (!empty($matches)) {
  1835.                 //      //echo "matches<pre>"; print_r($matches); echo "</pre>";
  1836.                 //      foreach($matches as $match_set) {
  1837.                 //          if ($match_set[2] = 'pwal') {
  1838.                 //              $shortcode_atts = shortcode_parse_atts( $match_set[3] );
  1839.                 //              //echo "shortcode_atts<pre>"; print_r($shortcode_atts); echo "</pre>";
  1840.                 //              if ((is_array($shortcode_atts)) && (isset($shortcode_atts['id']))
  1841.                 //               && ($shortcode_atts['id'] == $pwal_info['content_id'])) {
  1842.                 //                  //echo "matches<pre>"; print_r($matches); echo "</pre>";
  1843.                 //
  1844.                 //                  $content = $this->pwal_shortcode($shortcode_atts, $match_set[5]);
  1845.                 //                  //$content = $match_set[5];
  1846.                 //                  //if ($shortcode_atts['wpautop'] == 'yes') {
  1847.                 //                  //  $content = wpautop($content);
  1848.                 //                  //}
  1849.                 //
  1850.                 //                  break;
  1851.                 //              }
  1852.                 //          }
  1853.                 //      }
  1854.                 //  }
  1855.                 //} else {
  1856.                 //
  1857.                 //}
  1858.  
  1859.                 $trans_key = 'pwal_'. trim($pwal_info['content_id']);
  1860.                 //echo "trans_key[". $trans_key ."]<br />";
  1861.                 $content = get_transient( $trans_key );
  1862.  
  1863.                 //echo "content from transient [". $content ."]<br />";
  1864.  
  1865.                 if (!empty($content)) {
  1866.                     $content = apply_filters('the_content', $content);
  1867.                     //echo "content after filters [". $content ."]<br />";
  1868.                 }
  1869.  
  1870.                 if (!empty($content)) {
  1871.                     $content = str_replace(']]>', ']]&gt;', $content);
  1872.                     //echo "content after str_replace [". $content ."]<br />";
  1873.                 }
  1874.  
  1875.             } else {
  1876.                 $content = apply_filters('the_content', $post->post_content);
  1877.                 $content = str_replace(']]>', ']]&gt;', $content);
  1878.             }
  1879.         }
  1880.         return $content;
  1881.     }
  1882.  
  1883.  
  1884.     /**
  1885.      *  Custom box create call
  1886.      *
  1887.      */
  1888.     function add_custom_box( ) {
  1889.         $pwal_name = __('Pay With a Like', 'pwal');
  1890.  
  1891.         if (isset($this->options['show_metabox'])) {
  1892.             foreach($this->options['show_metabox'] as $slug => $enabled) {
  1893.                 add_meta_box( 'pwal_metabox', $pwal_name, array( &$this, 'custom_box' ), $slug, 'side', 'high' );
  1894.             }
  1895.             wp_enqueue_style( 'pay-with-a-like-admin-css', plugins_url('/css/pay-with-a-like-admin.css', __FILE__), array(), $this->version);
  1896.             wp_enqueue_script( 'pay-with-a-like-admin-js', plugins_url('/js/pay-with-a-like-admin.js', __FILE__), array(), $this->version, true);
  1897.  
  1898.         } else {
  1899.  
  1900.             //echo "options<pre>"; print_r($this->options); echo "</pre>";
  1901.             if ($this->options['post_default'] == "enable")
  1902.                 add_meta_box( 'pwal_metabox', $pwal_name, array( &$this, 'custom_box' ), 'post', 'side', 'high' );
  1903.  
  1904.             if ($this->options['page_default'] == "enable")
  1905.                 add_meta_box( 'pwal_metabox', $pwal_name, array( &$this, 'custom_box' ), 'page', 'side', 'high' );
  1906.  
  1907.             $args = array(
  1908.                 'public'   => true,
  1909.                 '_builtin' => false
  1910.             );
  1911.  
  1912.             if ($this->options['custom_default'] == "enable") {
  1913.                 $post_types = get_post_types( $args );
  1914.                 if ( is_array( $post_types ) ) {
  1915.                     foreach ($post_types as $post_type )
  1916.                         add_meta_box( 'pwal_metabox', $pwal_name, array( &$this, 'custom_box' ), $post_type, 'side', 'high' );
  1917.                 }
  1918.             }
  1919.         }
  1920.     }
  1921.  
  1922.     /**
  1923.      *  Custom box html codes
  1924.      *
  1925.      */
  1926.     function custom_box( $post ) {
  1927.         //echo "post<pre>"; print_r($post); echo "</pre>";
  1928.         // IF the post is not a new post
  1929.         if ($post->post_status != 'auto-draft') {
  1930.             ?><p><a href="<?php echo admin_url('admin.php?page=pay-with-a-like-statistics&post='. $post->ID); ?>"><?php _e('Show Like Statistics', 'pwal'); ?></a></p><?php
  1931.         }
  1932.  
  1933.         wp_nonce_field( plugin_basename(__FILE__), 'pwal_nonce' );
  1934.  
  1935.         $pwal_enable = get_post_meta( $post->ID, 'pwal_enable', true );
  1936.         if (isset($this->options['post_types'][$post->post_type])) {
  1937.             $global_enable_label    =   __("Default:", "pwal") .' '. __('Enabled','pwal');
  1938.             $global_enable_val      =   'global_enable';
  1939.         } else {
  1940.             $global_enable_label    = __("Default:", "pwal") .' '. __('Disabled','pwal');
  1941.             $global_enable_val      =   'global_disable';
  1942.         }
  1943.  
  1944.         $pwal_enable_values = array(
  1945.             $global_enable_val  =>  $global_enable_label,
  1946.             'enable'            =>  __('Enabled', 'pwal'),
  1947.             'disable'           =>  __('Disabled', 'pwal')
  1948.         );
  1949.         ?>
  1950.         <p id="section_pwal_enable"><label for="pwal_enable"><?php _e('Enabled?', 'pwal'); ?></label><br />
  1951.         <select name="pwal_enable" id="pwal_enable">
  1952.         <?php
  1953.             foreach($pwal_enable_values as $val => $label) {
  1954.                 $selected = selected($pwal_enable, $val);
  1955.  
  1956.                 ?><option value="<?php echo $val; ?>" <?php echo $selected ?> ><?php echo $label ?></option><?php
  1957.             }
  1958.         ?>
  1959.         </select></p>
  1960.         <?php
  1961.         $pwal_method            = get_post_meta( $post->ID, 'pwal_method', true );
  1962.         $global_method_val      =   'global_'. $this->options['method'];
  1963.         $global_method_label    =   __("Default:", "pwal") .' '. ucwords($this->options['method']);
  1964.         if (!post_type_supports($post->post_type, 'excerpt'))  {
  1965.             $global_method_val      =   'global_automatic';
  1966.             $global_method_label    =   __("Default:", "pwal") .' '. __('Automatic', 'pwal');
  1967.         }
  1968.  
  1969.         $pwal_method_values = array(
  1970.             $global_method_val  =>  $global_method_label,
  1971.             'automatic'         =>  __('Automatic excerpt', 'pwal'),
  1972.             'manual'            =>  __('Manual excerpt', 'pwal'),
  1973.             'tool'              =>  __('Use selection tool', 'pwal')
  1974.         );
  1975.  
  1976.         if (!post_type_supports($post->post_type, 'excerpt'))
  1977.             unset($pwal_method_values['manual']);
  1978.         ?>
  1979.         <p id="section_pwal_method"><label for="pwal_method"><?php _e('Excerpt Method', 'pwal'); ?></label><br />
  1980.         <select name="pwal_method" id="pwal_method">
  1981.         <?php
  1982.             foreach($pwal_method_values as $val => $label) {
  1983.                 $selected = selected($pwal_method, $val);
  1984.  
  1985.                 ?><option value="<?php echo $val; ?>" <?php echo $selected ?> ><?php echo $label ?></option><?php
  1986.             }
  1987.         ?>
  1988.         </select></p>
  1989.         <?php
  1990.  
  1991.         $global_excerpt_length = $this->options['excerpt'];
  1992.  
  1993.         $pwal_excerpt = get_post_meta( $post->ID, 'pwal_excerpt', true );
  1994.         ?>
  1995.         <p id="section_pwal_excerpt"><label for="pwal_excerpt"><?php _e('Excerpt length (words)', 'pwal') ?></label><br />
  1996.         <input type="text" name="pwal_excerpt" id="pwal_excerpt" value="<?php echo $pwal_excerpt; ?>" /><br />
  1997.         <span class="pwal-global-value description"><?php echo __('Default', 'pwal'). ': '. $global_excerpt_length ?></span></p>
  1998.  
  1999.         <?php
  2000.  
  2001.  
  2002.         $global_container_width = $this->options['container_width'];
  2003.         if (empty($global_container_width)) $global_container_width = '100%';
  2004.  
  2005.         $pwal_container_width = get_post_meta( $post->ID, 'pwal_container_width', true );
  2006.  
  2007.         ?>
  2008.         <p id="section_pwal_container_width"><label for="pwal_container_width"><?php _e('Button Container Width', 'pwal') ?></label><br />
  2009.         <input type="text" name="pwal_container_width" id="pwal_container_width" value="<?php echo $pwal_container_width; ?>" /><br />
  2010.         <span class="pwal-global-value description"><?php echo __('Default', 'pwal'). ': '. $global_container_width ?></span></p>
  2011.         <?php
  2012.  
  2013.         $pwal_url_to_like = get_post_meta( $post->ID, 'pwal_url_to_like', true );
  2014.         ?>
  2015.         <p id="section_pwal_url_to_like"><label for="pwal_url_to_like"><?php _e('Alt. URL to Like', 'pwal') ?> </label><br />
  2016.         <input type="text" name="pwal_url_to_like" id="pwal_url_to_like" value="<?php echo $pwal_url_to_like; ?>" /><br />
  2017.         <span class="pwal-global-value description"><?php echo __('If blank current post URL is used', 'pwal') ?></span></p>
  2018.         <?php
  2019.        
  2020.         // Added by Mike
  2021.  
  2022.         $pwal_furl_to_like = get_post_meta( $post->ID, 'pwal_furl_to_like', true );
  2023.         ?>
  2024.         <p id="section_pwal_furl_to_like"><label for="pwal_furl_to_like"><?php _e('Facebook URL to Like', 'pwal') ?> </label><br />
  2025.         <input type="text" name="pwal_furl_to_like" id="pwal_furl_to_like" value="<?php echo $pwal_furl_to_like; ?>" /><br />
  2026.         <span class="pwal-global-value description"><?php echo __('If blank Facebook Page URL is used', 'pwal') ?></span></p>
  2027.         <?php
  2028.         $pwal_gurl_to_like = get_post_meta( $post->ID, 'pwal_gurl_to_like', true );
  2029.         ?>
  2030.         <p id="section_pwal_gurl_to_like"><label for="pwal_gurl_to_like"><?php _e('Google URL to Like', 'pwal') ?> </label><br />
  2031.         <input type="text" name="pwal_gurl_to_like" id="pwal_gurl_to_like" value="<?php echo $pwal_gurl_to_like; ?>" /><br />
  2032.         <span class="pwal-global-value description"><?php echo __('If blank website URL is used', 'pwal') ?></span></p>
  2033.  
  2034.         <?php
  2035.         // End of Added by Mike
  2036.  
  2037.  
  2038.         $global_reload_val      =   'global_'. strtolower( $this->options['content_reload'] );
  2039.  
  2040.         if (strtolower( $this->options['content_reload'] ) == 'ajax')
  2041.             $global_reload_label    =   __("Default:", "pwal") .' '. strtoupper( $this->options['content_reload'] );
  2042.         else
  2043.             $global_reload_label    =   __("Default:", "pwal") .' '. ucwords( $this->options['content_reload'] );
  2044.         $pwal_reload_values = array(
  2045.             $global_reload_val  =>  $global_reload_label,
  2046.             'ajax'              =>  __("AJAX","pwal"),
  2047.             'refresh'           =>  __("Refresh","pwal")
  2048.         );
  2049.  
  2050.         $content_reload = get_post_meta( $post->ID, 'pwal_content_reload', true );
  2051.         ?>
  2052.         <p id="section_pwal_reload"><label for="pwal_content_reload"><?php _e('Reload Content', 'pwal'); ?></label><br />
  2053.         <select name="pwal_content_reload" id="pwal_content_reload">
  2054.         <?php
  2055.             foreach($pwal_reload_values as $val => $label) {
  2056.                 $selected = selected($content_reload, $val);
  2057.  
  2058.                 ?><option value="<?php echo $val; ?>" <?php echo $selected ?> ><?php echo $label ?></option><?php
  2059.             }
  2060.         ?>
  2061.         </select></p>
  2062.         <?php
  2063.  
  2064.         $pwal_description = get_post_meta( $post->ID, 'pwal_description', true );
  2065.         ?>
  2066.         <p id="section_pwal_description"><label for="pwal_description"><?php _e('Description', 'pwal') ?></label><br />
  2067.         <textarea name="pwal_description" id="pwal_description"><?php echo $pwal_description ?></textarea><br />
  2068.         <span class="pwal-global-value description"><?php echo __('If blank global setting is used', 'pwal') ?></span></p>
  2069.         <?php
  2070.     }
  2071.  
  2072.  
  2073.     /**
  2074.      *  Saves post meta values
  2075.      *
  2076.      */
  2077.     function save_postmeta( $post_id, $post ) {
  2078.  
  2079.         //echo "post_id[". $post_id."]<br />";
  2080.         //echo "post<pre>"; print_r($post); echo "</pre>";
  2081.         //echo "_GET<pre>"; print_r($_GET); echo "</pre>";
  2082.         //echo "_POST<pre>"; print_r($_POST); echo "</pre>";
  2083.         //die();
  2084.  
  2085.         if ( !wp_verify_nonce( @$_POST['pwal_nonce'], plugin_basename(__FILE__) ) )
  2086.             return $post_id;
  2087.         if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
  2088.             return $post_id;
  2089.  
  2090.         // Check permissions
  2091.         if ( 'page' == $_POST['post_type'] ) {
  2092.             if ( !current_user_can( 'edit_page', $post_id ) ) {
  2093.                 return $post_id;
  2094.             }
  2095.         }
  2096.         elseif ( !current_user_can( 'edit_post', $post_id ) )
  2097.             return $post_id;
  2098.  
  2099.         // Auth ok
  2100.  
  2101.         if ( isset( $_POST['pwal_enable'] ) ) {
  2102.             if (( $_POST['pwal_enable'] == 'enable' ) || ( $_POST['pwal_enable'] == 'disable' )) {
  2103.                 update_post_meta( $post_id, 'pwal_enable', esc_attr($_POST['pwal_enable']) );
  2104.             } else {
  2105.                 delete_post_meta( $post_id, 'pwal_enable' );
  2106.             }
  2107.         } else {
  2108.             delete_post_meta( $post_id, 'pwal_enable' );
  2109.         }
  2110.  
  2111.         //echo "pwal_method[". $_POST['pwal_method'] ."]<br />";
  2112.         //die();
  2113.         if ( isset( $_POST['pwal_method'] ) ) {
  2114.             if (( $_POST['pwal_method'] == 'automatic' ) || ( $_POST['pwal_method'] == 'manual' ) || ( $_POST['pwal_method'] == 'tool' )) {
  2115.                 update_post_meta( $post_id, 'pwal_method', esc_attr($_POST['pwal_method']) );
  2116.             } else {
  2117.                 delete_post_meta( $post_id, 'pwal_method' );
  2118.             }
  2119.         }
  2120.         //die();
  2121.  
  2122.         if ( isset( $_POST['pwal_excerpt'] ) ) {
  2123.             if ( !empty($_POST['pwal_excerpt']) ) {
  2124.                 update_post_meta( $post_id, 'pwal_excerpt', intval($_POST['pwal_excerpt']) );
  2125.             } else {
  2126.                 delete_post_meta( $post_id, 'pwal_excerpt' );
  2127.             }
  2128.         } else {
  2129.             delete_post_meta( $post_id, 'pwal_excerpt' );
  2130.         }
  2131.  
  2132.         if ( isset( $_POST['pwal_url_to_like'] ) ) {
  2133.             if ( $_POST['pwal_url_to_like'] != '') {
  2134.                 update_post_meta( $post_id, 'pwal_url_to_like', esc_url($_POST['pwal_url_to_like']) );
  2135.             } else {
  2136.                 delete_post_meta( $post_id, 'pwal_url_to_like' );
  2137.             }
  2138.         } else {
  2139.             delete_post_meta( $post_id, 'pwal_url_to_like' );
  2140.         }
  2141.  
  2142.         // Added by Mike
  2143.         if ( isset( $_POST['pwal_furl_to_like'] ) ) {
  2144.             if ( $_POST['pwal_furl_to_like'] != '') {
  2145.                 update_post_meta( $post_id, 'pwal_furl_to_like', esc_url($_POST['pwal_furl_to_like']) );
  2146.             } else {
  2147.                 delete_post_meta( $post_id, 'pwal_furl_to_like' );
  2148.             }
  2149.         } else {
  2150.             delete_post_meta( $post_id, 'pwal_furl_to_like' );
  2151.         }
  2152.  
  2153.  
  2154.         if ( isset( $_POST['pwal_gurl_to_like'] ) ) {
  2155.             if ( $_POST['pwal_gurl_to_like'] != '') {
  2156.                 update_post_meta( $post_id, 'pwal_gurl_to_like', esc_url($_POST['pwal_gurl_to_like']) );
  2157.             } else {
  2158.                 delete_post_meta( $post_id, 'pwal_gurl_to_like' );
  2159.             }
  2160.         } else {
  2161.             delete_post_meta( $post_id, 'pwal_gurl_to_like' );
  2162.         }
  2163.         // End added by Mike
  2164.        
  2165.         if ( isset( $_POST['pwal_container_width'] ) ) {
  2166.             if ( $_POST['pwal_container_width'] != '') {
  2167.                 update_post_meta( $post_id, 'pwal_container_width', esc_attr($_POST['pwal_container_width']) );
  2168.             } else {
  2169.                 delete_post_meta( $post_id, 'pwal_container_width' );
  2170.             }
  2171.         } else {
  2172.             delete_post_meta( $post_id, 'pwal_container_width' );
  2173.         }
  2174.  
  2175.         if ( isset( $_POST['pwal_content_reload'] ) ) {
  2176.             if ( ($_POST['pwal_content_reload'] == 'ajax') || ( $_POST['pwal_content_reload'] == 'refresh' ) ) {
  2177.                 update_post_meta( $post_id, 'pwal_content_reload', esc_attr($_POST['pwal_content_reload']) );
  2178.             } else {
  2179.                 delete_post_meta( $post_id, 'pwal_content_reload' );
  2180.             }
  2181.         } else {
  2182.             delete_post_meta( $post_id, 'pwal_content_reload' );
  2183.         }
  2184.  
  2185.  
  2186.         if ( isset( $_POST['pwal_description'] ) ) {
  2187.             if ( $_POST['pwal_description'] != '') {
  2188.                 update_post_meta( $post_id, 'pwal_description', esc_attr($_POST['pwal_description']) );
  2189.             } else {
  2190.                 delete_post_meta( $post_id, 'pwal_description' );
  2191.             }
  2192.         } else {
  2193.             delete_post_meta( $post_id, 'pwal_description' );
  2194.         }
  2195.     }
  2196.  
  2197.  
  2198.     /**
  2199.      *  Set some default settings
  2200.      *
  2201.      */
  2202.     function install() {
  2203.  
  2204.         //$pwal = new PayWithaLike();
  2205.         //$pwal->init();
  2206.  
  2207.         //update_option( 'pwal_options', $pwal->options );
  2208.     }
  2209.  
  2210.     function uninstall() {
  2211.         // does nothing
  2212.     }
  2213.  
  2214.     function checkbox_value($name) {
  2215.         return (isset($_POST['pwal'][$name]) ? "true" : "");
  2216.     }
  2217.  
  2218.  
  2219.     function admin_menu() {
  2220.         $this->_pagehooks['pwal'] =  add_menu_page(
  2221.                 __('Pay With a Like','pwal'),
  2222.                 __('Pay With a Like','pwal'),
  2223.                 'manage_options',
  2224.                 'pay-with-a-like',
  2225.                 array(&$this,'settings')
  2226.             );
  2227.  
  2228.         $this->_pagehooks['pwal_settings'] = add_submenu_page(
  2229.             'pay-with-a-like',
  2230.             __('Settings', 'pwal'),
  2231.             __('Settings', 'pwal'),
  2232.             'manage_options',
  2233.             'pay-with-a-like',
  2234.             array(&$this,'settings')
  2235.         );
  2236.  
  2237. /*
  2238.         $this->_pagehooks['pwal_accessibility'] = add_submenu_page(
  2239.                 'pay-with-a-like',
  2240.                 __('Accessibility', 'pwal'),
  2241.                 __('Accessibility', 'pwal'),
  2242.                 'manage_options',
  2243.                 'pay-with-a-like-accessibility',
  2244.                 array(&$this,'settings')
  2245.             );
  2246. */
  2247.         $this->_pagehooks['pwal_buttons'] = add_submenu_page(
  2248.                 'pay-with-a-like',
  2249.                 __('Social Buttons', 'pwal'),
  2250.                 __('Social Buttons', 'pwal'),
  2251.                 'manage_options',
  2252.                 'pay-with-a-like-buttons',
  2253.                 array(&$this,'settings')
  2254.             );
  2255.  
  2256.         $this->_pagehooks['pwal_statistics'] = add_submenu_page(
  2257.                 'pay-with-a-like',
  2258.                 __('Statistics', 'pwal'),
  2259.                 __('Statistics', 'pwal'),
  2260.                 'manage_options',
  2261.                 'pay-with-a-like-statistics',
  2262.                 array(&$this,'settings')
  2263.             );
  2264.  
  2265.         $this->_pagehooks['pwal_customization'] = add_submenu_page(
  2266.                 'pay-with-a-like',
  2267.                 __('Customization', 'pwal'),
  2268.                 __('Customization', 'pwal'),
  2269.                 'manage_options',
  2270.                 'pay-with-a-like-customization',
  2271.                 array(&$this,'settings')
  2272.             );
  2273.  
  2274.  
  2275. /*
  2276.         $this->_pagehooks['pwal_settings'] = add_options_page(
  2277.             __('Pay With a Like Settings','pwal'),
  2278.             __('Pay With a Like','pwal'), 'manage_options',
  2279.             'pay-with-a-like',
  2280.             array(&$this,'settings') );
  2281. */
  2282.         add_action( 'load-'. $this->_pagehooks['pwal_settings'],        array(&$this, 'on_load_panels') );
  2283. //      add_action( 'load-'. $this->_pagehooks['pwal_accessibility'],   array(&$this, 'on_load_panels') );
  2284.         add_action( 'load-'. $this->_pagehooks['pwal_buttons'],         array(&$this, 'on_load_panels') );
  2285.         add_action( 'load-'. $this->_pagehooks['pwal_statistics'],      array(&$this, 'on_load_panels') );
  2286.         add_action( 'load-'. $this->_pagehooks['pwal_customization'],   array(&$this, 'on_load_panels') );
  2287.  
  2288.     }
  2289.  
  2290.     function on_load_panels() {
  2291.         $this->process_panel_actions();
  2292.  
  2293.         if ((isset($_GET['page'])) && ($_GET['page'] == 'pay-with-a-like-buttons')) {
  2294.             wp_enqueue_script('jquery-ui-sortable');
  2295.         }
  2296.  
  2297.         wp_enqueue_style( 'pay-with-a-like-admin-css', plugins_url('/css/pay-with-a-like-admin.css', __FILE__), array(), $this->version);
  2298.         wp_enqueue_script( 'pay-with-a-like-admin-js', plugins_url('/js/pay-with-a-like-admin.js', __FILE__), array(), $this->version);
  2299.  
  2300.         if ((isset($_GET['page'])) && ($_GET['page'] == 'pay-with-a-like-statistics')) {
  2301.             //wp_enqueue_style( 'pay-with-a-like-admin-css', plugins_url('/css/pay-with-a-like-admin.css', __FILE__), array('jquery'), $this->version);
  2302.             wp_enqueue_script( 'pay-with-a-like-admin-statistics-js', plugins_url('/js/pay-with-a-like-admin-statistics.js', __FILE__), array('jquery'), $this->version);
  2303.             wp_enqueue_script('flot_js', plugins_url('/js/jquery.flot.min.js', __FILE__), array('jquery'));
  2304.             wp_enqueue_script('flot_js', plugins_url('/js/jquery.flot.pie.min.js', __FILE__), array('flot_js'));
  2305.  
  2306.             add_action('admin_head', array(&$this, 'add_iehead') );
  2307.         }
  2308.     }
  2309.  
  2310.     function add_iehead() {
  2311.         echo '<!--[if lte IE 8]><script language="javascript" type="text/javascript" src="' . plugins_url('/js/excanvas.min.js', __FILE__) . '"></script><![endif]-->';
  2312.     }
  2313.  
  2314.     function ajax__aff_getstats() {
  2315.  
  2316.         if (isset($_GET['number'])) {
  2317.             $number = intval($_GET['number']);
  2318.         } else {
  2319.             $number = 12; // show last 12 months
  2320.         }
  2321.  
  2322.         if (isset($_GET['post_id'])) {
  2323.             $post_id = intval($_GET['post_id']);
  2324.         } else {
  2325.             $post_id = 0;
  2326.         }
  2327.  
  2328.         // We make a copy of the buttons because we run an unset on the ones used.
  2329.         $social_buttons_tmp = $this->options['social_buttons'];
  2330.  
  2331.         if (!empty($this->options['social_button_sort'])) {
  2332.             foreach($this->options['social_button_sort'] as $button_key) {
  2333.  
  2334.                 if (isset($social_buttons_tmp[$button_key])) {
  2335.                     $pwal_buttons_show[$button_key] = $social_buttons_tmp[$button_key];
  2336.                     unset($social_buttons_tmp[$button_key]);
  2337.                 }
  2338.             }
  2339.         }
  2340.  
  2341.         // Append any missing ones to the end of the show array.
  2342.         if (!empty($social_buttons_tmp)) {
  2343.             foreach($social_buttons_tmp as $button_key => $button_label) {
  2344.                 $pwal_buttons_show[$button_key] = $social_buttons_tmp[$button_key];
  2345.             }
  2346.         }
  2347.  
  2348.         foreach($pwal_buttons_show as $button_key => $button_label) {
  2349.             if ( !$this->options["use_".$button_key])
  2350.                 unset($pwal_buttons_show[$button_key]);
  2351.         }
  2352.  
  2353.         $stats = get_option( "pwal_statistics" );
  2354.         if (empty($stats)) $stats = array(); //return array();;
  2355.         //echo "stats<pre>"; print_r($stats); echo "</pre>";
  2356.  
  2357.  
  2358.         /*
  2359.         'b'     => $blog_id,
  2360.         'p'     => $_POST["post_id"],
  2361.         'c'     => $_POST["content_id"],
  2362.         's'     => $_POST["service"],
  2363.         'i'     => $_SERVER["REMOTE_ADDR"],
  2364.         't'     => current_time('timestamp')
  2365.         */
  2366.  
  2367.         $stat_items = array();
  2368.         foreach($stats as $stat) {
  2369.  
  2370.             $period = date('Ym', $stat['t']);
  2371.             if (!isset($stat_items[$period]))
  2372.                 $stat_items[$period] = array();
  2373.  
  2374.             if (!isset($stat_items[$period]['facebook']))
  2375.                 $stat_items[$period]['facebook'] = intval(0);
  2376.             if (!isset($stat_items[$period]['linkedin']))
  2377.                 $stat_items[$period]['linkedin'] = intval(0);
  2378.             if (!isset($stat_items[$period]['twitter']))
  2379.                 $stat_items[$period]['twitter'] = intval(0);
  2380.             if (!isset($stat_items[$period]['google']))
  2381.                 $stat_items[$period]['google'] = intval(0);
  2382.  
  2383.             if ( ($post_id != 0) && ($stat['p'] != $post_id) ) {
  2384.                 continue;
  2385.             }
  2386.  
  2387.             if (isset($stat['s'])) {
  2388.                 $stat['s'] = strtolower(trim($stat['s']));
  2389.                 //echo "service[". $stat['s'] ."] period[". $period ."]\r\n";
  2390.  
  2391.                 if ($stat['s'] === 'facebook') {
  2392.                     $stat_items[$period]['facebook'] += 1;
  2393.                 } else if ($stat['s'] === 'twitter') {
  2394.                     $stat_items[$period]['twitter'] += 1;
  2395.                 } else if ($stat['s'] === 'linkedin') {
  2396.                     $stat_items[$period]['linkedin'] += 1;
  2397.                 } else if ($stat['s'] === 'google') {
  2398.                     $stat_items[$period]['google'] += 1;
  2399.                 }
  2400.  
  2401.             }
  2402.         }
  2403.  
  2404.         //echo "stat_items<pre>"; print_r($stat_items); echo "</pre>";
  2405.  
  2406.         $output             = array();
  2407.         foreach($pwal_buttons_show as $button_key => $button_label) {
  2408.             $output[$button_key] = array();
  2409.         }
  2410.  
  2411.         $ticks              = array();
  2412.  
  2413.         $startat = strtotime(date("Y-m-15"));
  2414.         for($n = 0; $n < $number; $n++) {
  2415.  
  2416.             $place = $number - $n;
  2417.             $rdate = strtotime("-$n month", $startat);
  2418.             $period = date('Ym', $rdate);
  2419.             $ticks[] = array((int) $place, date('M', $rdate) . '<br/>' . date('Y', $rdate) );
  2420.  
  2421.             if (!isset($stat_items[$period])) {
  2422.                 // A zero blank row
  2423.                 foreach($pwal_buttons_show as $button_key => $button_label) {
  2424.                     $output[$button_key][] = array( (int) $place, (int) 0);
  2425.                 }
  2426.  
  2427.             } else {
  2428.                 foreach($pwal_buttons_show as $button_key => $button_label) {
  2429.                     $output[$button_key][] = array( (int) $place, (int) $stat_items[$period][$button_key]);
  2430.                 }
  2431.             }
  2432.         }
  2433.  
  2434.         $return = array();
  2435.         $return['ticks'] = $ticks;
  2436.  
  2437.         foreach($pwal_buttons_show as $button_key => $button_label) {
  2438.             $return['chart'][] = array("label" => $button_label, "data" => $output[$button_key]);
  2439.         }
  2440.  
  2441.         echo json_encode($return);
  2442.  
  2443.         die();
  2444.     }
  2445.  
  2446.     function process_panel_actions() {
  2447.  
  2448.         if ( isset( $_POST['pwal_action'] )) {
  2449.  
  2450.             //echo "_POST<pre>"; print_r($_POST); echo "</pre>";
  2451.             //die();
  2452.  
  2453.             if ( isset($_POST["pwal_action"]) && !wp_verify_nonce($_POST['pwal_nonce'], 'pwal_update_settings') ) {
  2454.                 add_action( 'admin_notices', array( &$this, 'warning' ) );
  2455.                 return;
  2456.             }
  2457.  
  2458.             switch ( $_GET['page'] ) {
  2459. /*
  2460.                 case 'pay-with-a-like-accessibility':
  2461.                     if (isset($_POST['pwal']['home']))
  2462.                         $this->options['home']                          = $_POST['pwal']['home'];
  2463.  
  2464.                     if (isset($_POST['pwal']['multi']))
  2465.                         $this->options['multi']                         = $_POST['pwal']['multi'];
  2466.  
  2467.                     if (isset($_POST['pwal']['admin']))
  2468.                         $this->options['admin']                         = $_POST['pwal']['admin'];
  2469.  
  2470.                     if (isset($_POST['pwal']['authorized']))
  2471.                         $this->options['authorized']                    = $_POST['pwal']['authorized'];
  2472.  
  2473.                     if (isset($_POST['pwal']['level']))
  2474.                         $this->options['level']                         = $_POST['pwal']['level'];
  2475.  
  2476.                     if (isset($_POST['pwal']['bot']))
  2477.                         $this->options['bot']                           = $_POST['pwal']['bot'];
  2478.  
  2479.                     if (isset($_POST['pwal']['cookie']))
  2480.                         $this->options['cookie']                        = intval($_POST['pwal']['cookie']);
  2481.  
  2482.                     break;
  2483. */
  2484.                 case 'pay-with-a-like-buttons':
  2485.                     //echo "_POST<pre>"; print_r($_POST); echo "</pre>";
  2486.                     //die();
  2487.  
  2488.                     if (isset($_POST['tab'])) {
  2489.                         switch($_POST['tab']) {
  2490.                             case 'buttons':
  2491.                                 if (isset($_POST['pwal']['social_button_sort']))
  2492.                                     $this->options['social_button_sort']                        = explode(',', $_POST['pwal']['social_button_sort']);
  2493.                                 else
  2494.                                     $this->options['social_button_sort']                        = '';
  2495.  
  2496.                                 //echo "social_button_sort<pre>"; print_r($this->options['social_button_sort']); echo "</pre>";
  2497.                                 //die();
  2498.  
  2499.                                 if (isset($_POST['pwal']['use_facebook']))
  2500.                                     $this->options['use_facebook']                      = $_POST['pwal']['use_facebook'];
  2501.                                 else
  2502.                                     $this->options['use_facebook']                      = '';
  2503.  
  2504.                                 // Ensure we don't load the social library IF we are not using it.
  2505.                                 if ($this->options['use_facebook'] == '')
  2506.                                     $this->options['load_facebook'] = '';
  2507.                                 else {
  2508.                                     if (isset($_POST['pwal']['load_facebook']))
  2509.                                         $this->options['load_facebook']                     = $_POST['pwal']['load_facebook'];
  2510.                                     else
  2511.                                         $this->options['load_facebook']                     = '';
  2512.                                 }
  2513.  
  2514.                                 if (isset($_POST['pwal']['use_linkedin']))
  2515.                                     $this->options['use_linkedin']                      = $_POST['pwal']['use_linkedin'];
  2516.                                 else
  2517.                                     $this->options['use_linkedin']                      = '';
  2518.  
  2519.                                 // Ensure we don't load the social library IF we are not using it.
  2520.                                 if ($this->options['use_linkedin'] == '')
  2521.                                     $this->options['load_linkedin'] = '';
  2522.                                 else {
  2523.                                     if (isset($_POST['pwal']['load_linkedin']))
  2524.                                         $this->options['load_linkedin']                     = $_POST['pwal']['load_linkedin'];
  2525.                                     else
  2526.                                         $this->options['load_linkedin']                     = '';
  2527.                                 }
  2528.  
  2529.                                 if (isset($_POST['pwal']['use_twitter']))
  2530.                                     $this->options['use_twitter']                       = $_POST['pwal']['use_twitter'];
  2531.                                 else
  2532.                                     $this->options['use_twitter']                       = '';
  2533.  
  2534.                                 // Ensure we don't load the social library IF we are not using it.
  2535.                                 if ($this->options['use_twitter'] == '')
  2536.                                     $this->options['load_twitter'] = '';
  2537.                                 else {
  2538.                                     if (isset($_POST['pwal']['load_twitter']))
  2539.                                         $this->options['load_twitter']                      = $_POST['pwal']['load_twitter'];
  2540.                                     else
  2541.                                         $this->options['load_twitter']                      = '';
  2542.                                 }
  2543.  
  2544.  
  2545.                                 if (isset($_POST['pwal']['use_google']))
  2546.                                     $this->options['use_google']                        = $_POST['pwal']['use_google'];
  2547.                                 else
  2548.                                     $this->options['use_google']                        = '';
  2549.  
  2550.                                 // Ensure we don't load the social library IF we are not using it.
  2551.                                 if ($this->options['use_google'] == '')
  2552.                                     $this->options['load_google'] = '';
  2553.                                 else {
  2554.                                     if (isset($_POST['pwal']['load_google']))
  2555.                                         $this->options['load_google']                       = $_POST['pwal']['load_google'];
  2556.                                     else
  2557.                                         $this->options['load_google']                       = '';
  2558.                                 }
  2559.  
  2560.                                 break;
  2561.  
  2562.                             case 'facebook':
  2563.                                 //echo "_POST<pre>"; print_r($_POST); echo "</pre>";
  2564.                                 //die();
  2565.  
  2566.                                 if (isset($_POST['pwal']['facebook_api_key']))
  2567.                                     $this->options['facebook_api_key']                  = esc_attr($_POST['pwal']['facebook_api_key']);
  2568.                                 else
  2569.                                     $this->options['facebook_api_key']                  = '';
  2570.  
  2571.                                 if (isset($_POST['pwal']['facebook_api_secret']))
  2572.                                     $this->options['facebook_api_secret']               = esc_attr($_POST['pwal']['facebook_api_secret']);
  2573.                                 else
  2574.                                     $this->options['facebook_api_secret']               = '';
  2575.  
  2576.                                 if (isset($_POST['pwal']['facebook_layout_style']))
  2577.                                     $this->options['facebook_layout_style']             = esc_attr($_POST['pwal']['facebook_layout_style']);
  2578.                                 else
  2579.                                     $this->options['facebook_layout_style']             = '';
  2580.  
  2581.                                 //if (isset($_POST['pwal']['facebook_color_scheme']))
  2582.                                 //  $this->options['facebook_color_scheme']             = esc_attr($_POST['pwal']['facebook_color_scheme']);
  2583.                                 //else
  2584.                                 //  $this->options['facebook_color_scheme']             = '';
  2585.  
  2586.                                 if (isset($_POST['pwal']['facebook_verb']))
  2587.                                     $this->options['facebook_verb']                     = esc_attr($_POST['pwal']['facebook_verb']);
  2588.                                 else
  2589.                                     $this->options['facebook_verb']                     = '';
  2590.  
  2591.                                 if (isset($_POST['pwal']['facebook_include_share']))
  2592.                                     $this->options['facebook_include_share']            = esc_attr($_POST['pwal']['facebook_include_share']);
  2593.                                 else
  2594.                                     $this->options['facebook_include_share']            = '';
  2595.  
  2596.                                 if (isset($_POST['pwal']['facebook_include_faces']))
  2597.                                     $this->options['facebook_include_faces']            = esc_attr($_POST['pwal']['facebook_include_faces']);
  2598.                                 else
  2599.                                     $this->options['facebook_include_faces']            = '';
  2600.  
  2601.                                 if (isset($_POST['pwal']['show_facebook_comment_popup']))
  2602.                                     $this->options['show_facebook_comment_popup']       = esc_attr($_POST['pwal']['show_facebook_comment_popup']);
  2603.                                 else
  2604.                                     $this->options['show_facebook_comment_popup']       = '';
  2605.  
  2606.  
  2607.                                 if (isset($_POST['pwal']['facebook_button_lang']))
  2608.                                     $this->options['facebook_button_lang']              = esc_attr($_POST['pwal']['facebook_button_lang']);
  2609.                                 else
  2610.                                     $this->options['facebook_button_lang']              = '';
  2611.  
  2612.                                 if (isset($_POST['pwal']['facebook_auth_polling']))
  2613.                                     $this->options['facebook_auth_polling']             = esc_attr($_POST['pwal']['facebook_auth_polling']);
  2614.                                 else
  2615.                                     $this->options['facebook_auth_polling']             = '';
  2616.  
  2617.                                 if (isset($_POST['pwal']['facebook_auth_polling_interval']))
  2618.                                     $this->options['facebook_auth_polling_interval']    = esc_attr($_POST['pwal']['facebook_auth_polling_interval']);
  2619.                                 else
  2620.                                     $this->options['facebook_auth_polling_interval']    = '';
  2621.  
  2622.  
  2623.  
  2624.  
  2625.  
  2626.  
  2627.                                 // Here we take the current form items and compare to the options by key. If the form item was deleted then
  2628.                                 // it will not be posted into here. So we remove the item from the samed options. Then we process the new URLs
  2629.                                 //echo "facebook_fan_pages<pre>"; print_r($this->options['facebook_fan_pages']); echo "</pre>";
  2630.                                 //echo "facebook_fan_page_urls_current<pre>"; print_r($_POST['pwal']['facebook_fan_page_urls_current']); echo "</pre>";
  2631.                                 //die();
  2632.  
  2633.                                 if (empty($_POST['pwal']['facebook_fan_page_urls_current'])) {
  2634.                                     unset($this->options['facebook_fan_pages']);
  2635.                                     $this->options['facebook_fan_pages'] = array();
  2636.                                 } else {
  2637.                                      foreach($this->options['facebook_fan_pages'] as $fan_page_idx => $fan_page_info) {
  2638.                                          if (in_array($fan_page_idx, $_POST['pwal']['facebook_fan_page_urls_current']) === false) {
  2639.                                             unset($this->options['facebook_fan_pages'][$fan_page_idx]);
  2640.                                         }
  2641.                                     }
  2642.                                 }
  2643.                                 //echo "facebook_fan_pages<pre>"; print_r($this->options['facebook_fan_pages']); echo "</pre>";
  2644.                                 //die();
  2645.  
  2646.                                 if ((!empty($this->options['facebook_api_key'])) && (!empty($this->options['facebook_api_secret']))
  2647.                                  && (isset($_POST['pwal']['facebook_fan_page_urls_new'])) && (!empty($_POST['pwal']['facebook_fan_page_urls_new']))) {
  2648.  
  2649.                                     //unset($this->options['facebook_fan_pages']);
  2650.                                     if (!isset($this->options['facebook_fan_pages']))
  2651.                                         $this->options['facebook_fan_pages'] = array();
  2652.  
  2653.                                     //echo "facebook_fan_page_urls<pre>"; print_r( $_POST['pwal']['facebookfan_page_urls'] ); echo "</pre>";
  2654.                                     //include_once( dirname(__FILE__) .'/lib/facebook-php-sdk/facebook.php');
  2655.  
  2656.                                     if (!class_exists('PWALBaseFacebook')) {
  2657.                                         //error_log('loading PWALBaseFacebook');
  2658.                                         include_once( dirname(__FILE__) .'/lib/facebook-php-sdk/base_facebook.php');
  2659.                                     }
  2660.  
  2661.                                     if (!class_exists('PWALFacebook')) {
  2662.                                         //error_log('loading PWALFacebook');
  2663.                                         include_once( dirname(__FILE__) .'/lib/facebook-php-sdk/facebook.php');
  2664.                                     }
  2665.  
  2666.                                     $facebook = new PWALFacebook(array(
  2667.                                         'appId'  => $this->options['facebook_api_key'],
  2668.                                         'secret' => $this->options['facebook_api_secret'],
  2669.                                         'cookie' => true,
  2670.                                     ));
  2671.  
  2672.                                     foreach($_POST['pwal']['facebook_fan_page_urls_new'] as $url) {
  2673.                                         $url = esc_url(trim($url));
  2674.                                         //echo "url[". $url ."]<br />";
  2675.                                         if (empty($url)) continue;
  2676.  
  2677.                                         $url_path = parse_url($url, PHP_URL_PATH);
  2678.                                         if (!empty($url_path)) {
  2679.                                             $url_path = basename($url_path);
  2680.  
  2681.                                             //echo "url_path[". $url_path ."]<br />";
  2682.                                             $url_path_int = intval($url_path);
  2683.                                             //echo "url_path_int[". $url_path_int ."]<br />";
  2684.                                             //echo "empty[". empty($url_path_int )."]<br />";
  2685.                                             if (empty($url_path_int)) {
  2686.                                                 $query_str = "SELECT name,page_id, page_url,pic_square FROM page WHERE username = '". $url_path ."'";
  2687.                                             }
  2688.                                             else {
  2689.                                                 $query_str = "SELECT name,page_id, page_url,pic_square FROM page WHERE page_id = '". $url_path ."'";
  2690.  
  2691.                                             }
  2692.  
  2693.                                             try {
  2694.  
  2695.                                                 //echo "query_str[". $query_str ."]<br />";
  2696.                                                 $params = array(
  2697.                                                     'method'    =>  'fql.query',
  2698.                                                     'query'     =>  $query_str
  2699.                                                 );
  2700.  
  2701.                                                 //echo "params<pre>"; print_r($params); echo "</pre>";
  2702.  
  2703.                                                 //Run Query
  2704.                                                 $results = $facebook->api($params);
  2705.                                                 //echo "results<pre>"; print_r($results); echo "</pre>";
  2706.                                                 if ((!empty($results)) && (is_array($results))) {
  2707.                                                     foreach($results as $result) {
  2708.                                                         if ((!empty($result)) && (isset($result['page_id'])) && (!empty($result['page_id']))) {
  2709.                                                             $this->options['facebook_fan_pages'][$result['page_id']] = $result;
  2710.                                                         }
  2711.                                                     }
  2712.                                                 }
  2713.  
  2714.                                              } catch (PWALFacebookApiException $e) {
  2715.                                                  //echo "Exception<pre>"; print_r($e); echo "</pre>";
  2716.                                              }
  2717.                                          }
  2718.                                     }
  2719.                                     //echo "facebook_fan_pages<pre>"; print_r($this->options['facebook_fan_pages']); echo "</pre>";
  2720.                                     //echo "end<br />";
  2721.                                     //die();
  2722.                                 }
  2723.                                 //echo "_POST<pre>"; print_r($_POST); echo "</pre>";
  2724.                                 //echo "options<pre>"; print_r($this->options); echo "</pre>";
  2725.                                 //die();
  2726.                                 break;
  2727.  
  2728.                             case 'linkedin':
  2729.                                 if (isset($_POST['pwal']['linkedin_layout_style']))
  2730.                                     $this->options['linkedin_layout_style']             = esc_attr($_POST['pwal']['linkedin_layout_style']);
  2731.                                 else
  2732.                                     $this->options['linkedin_layout_style']             = '';
  2733.  
  2734.                                 if (isset($_POST['pwal']['linkedin_button_lang']))
  2735.                                     $this->options['linkedin_button_lang']              = esc_attr($_POST['pwal']['linkedin_button_lang']);
  2736.                                 else
  2737.                                     $this->options['linkedin_button_lang']              = '';
  2738.  
  2739.                                 break;
  2740.  
  2741.                             case 'twitter':
  2742.                                 if (isset($_POST['pwal']['twitter_layout_style']))
  2743.                                     $this->options['twitter_layout_style']              = esc_attr($_POST['pwal']['twitter_layout_style']);
  2744.                                 else
  2745.                                     $this->options['twitter_layout_style']              = '';
  2746.  
  2747.                                 //if (isset($_POST['pwal']['twitter_button_size']))
  2748.                                 //  $this->options['twitter_button_size']               = esc_attr($_POST['pwal']['twitter_button_size']);
  2749.                                 //else
  2750.                                 //  $this->options['twitter_button_size']               = '';
  2751.  
  2752.                                 if (isset($_POST['pwal']['twitter_message']))
  2753.                                     $this->options['twitter_message']                   = esc_attr($_POST['pwal']['twitter_message']);
  2754.                                 else
  2755.                                     $this->options['twitter_message']                   = '';
  2756.  
  2757.                                 if (isset($_POST['pwal']['twitter_button_lang']))
  2758.                                     $this->options['twitter_button_lang']               = esc_attr($_POST['pwal']['twitter_button_lang']);
  2759.                                 else
  2760.                                     $this->options['twitter_button_lang']               = '';
  2761.  
  2762.                                 break;
  2763.  
  2764.                             case 'google':
  2765.                                 //echo "_POST<pre>"; print_r($_POST); echo "</pre>";
  2766.                                 //die();
  2767.  
  2768.                                 if (isset($_POST['pwal']['google_layout_style']))
  2769.                                     $this->options['google_layout_style']               = esc_attr($_POST['pwal']['google_layout_style']);
  2770.                                 else
  2771.                                     $this->options['google_layout_style']               = '';
  2772.  
  2773.                                 if (isset($_POST['pwal']['google_button_lang']))
  2774.                                     $this->options['google_button_lang']                = esc_attr($_POST['pwal']['google_button_lang']);
  2775.                                 else
  2776.                                     $this->options['google_button_lang']                = '';
  2777.  
  2778.  
  2779.                                 break;
  2780.                         }
  2781.                     }
  2782.                     break;
  2783.  
  2784.                 case 'pay-with-a-like-statistics':
  2785.                     break;
  2786.  
  2787.                 case 'pay-with-a-like-customization':
  2788.                     break;
  2789.  
  2790.                 case 'pay-with-a-like':
  2791.  
  2792.                     //echo "_POST<pre>"; print_r($_POST); echo "</pre>";
  2793.                     //die();
  2794.  
  2795.                     if (isset($_POST['pwal']['post_types']))
  2796.                         $this->options['post_types']                        = $_POST['pwal']['post_types'];
  2797.                     else
  2798.                         $this->options['post_types']                        = array();
  2799.  
  2800.  
  2801.                     if (isset($_POST['pwal']['show_metabox']))
  2802.                         $this->options['show_metabox']                      = $_POST['pwal']['show_metabox'];
  2803.                     else
  2804.                         $this->options['show_metabox']                      = array();
  2805.  
  2806.  
  2807.                     if (isset($_POST['pwal']['method']))
  2808.                         $this->options['method']                            = $_POST['pwal']['method'];
  2809.                     else
  2810.                         $this->options['method']                            = '';
  2811.  
  2812.                     if (isset($_POST['pwal']['excerpt']))
  2813.                         $this->options['excerpt']                           = esc_attr($_POST['pwal']['excerpt']);
  2814.                     else
  2815.                         $this->options['excerpt']                           = '';
  2816.  
  2817.  
  2818.                     if (isset($_POST['pwal']['description']))
  2819.                         $this->options['description']                       = esc_attr($_POST['pwal']['description']);
  2820.                     else
  2821.                         $this->options['description']                       = '';
  2822.  
  2823.  
  2824.                     if (isset($_POST['pwal']['container_width']))
  2825.                         $this->options['container_width']                   = esc_attr($_POST['pwal']['container_width']);
  2826.                     else
  2827.                         $this->options['container_width']                   = '';
  2828.  
  2829.  
  2830. //                  if (isset($_POST['pwal']['container_height']))
  2831. //                      $this->options['container_height']                  = esc_attr($_POST['pwal']['container_height']);
  2832. //                  else
  2833. //                      $this->options['container_height']                  = '';
  2834.  
  2835.  
  2836. //                  if (isset($_POST['pwal']['container_border_width']))
  2837. //                      $this->options['container_border_width']            = esc_attr($_POST['pwal']['container_border_width']);
  2838. //                  else
  2839. //                      $this->options['container_border_width']            = '';
  2840.  
  2841.  
  2842. //                  if (isset($_POST['pwal']['container_border_style']))
  2843. //                      $this->options['container_border_style']            = esc_attr($_POST['pwal']['container_border_style']);
  2844. //                  else
  2845. //                      $this->options['container_border_style']            = '';
  2846.  
  2847.  
  2848. //                  if (isset($_POST['pwal']['container_border_color']))
  2849. //                      $this->options['container_border_color']            = esc_attr($_POST['pwal']['container_border_color']);
  2850. //                  else
  2851. //                      $this->options['container_border_color']            = '';
  2852.  
  2853.  
  2854.                     if (isset($_POST['pwal']['content_reload']))
  2855.                         $this->options['content_reload']                    = $_POST['pwal']['content_reload'];
  2856.                     else
  2857.                         $this->options['content_reload']                    = 'refresh';
  2858.  
  2859.  
  2860.                     if (isset($_POST['pwal']['home']))
  2861.                         $this->options['home']                              = $_POST['pwal']['home'];
  2862.  
  2863.  
  2864.                     if (isset($_POST['pwal']['multi']))
  2865.                         $this->options['multi']                             = $_POST['pwal']['multi'];
  2866.  
  2867.  
  2868.                     if (isset($_POST['pwal']['admin']))
  2869.                         $this->options['admin']                             = $_POST['pwal']['admin'];
  2870.  
  2871.  
  2872.                     if (isset($_POST['pwal']['authorized']))
  2873.                         $this->options['authorized']                        = $_POST['pwal']['authorized'];
  2874.  
  2875.  
  2876.                     if (isset($_POST['pwal']['level']))
  2877.                         $this->options['level']                             = $_POST['pwal']['level'];
  2878.  
  2879.  
  2880.                     if (isset($_POST['pwal']['bot']))
  2881.                         $this->options['bot']                               = $_POST['pwal']['bot'];
  2882.  
  2883.  
  2884.                     if (isset($_POST['pwal']['cookie']))
  2885.                         $this->options['cookie']                            = intval($_POST['pwal']['cookie']);
  2886.  
  2887.  
  2888.                     if (isset($_POST['pwal']['sitewide']))
  2889.                         $this->options['sitewide']                          = $_POST['pwal']['sitewide'];
  2890.                     else
  2891.                         $this->options['sitewide']                          = '';
  2892.  
  2893.                     if (isset($_POST['pwal']['url_to_like']))
  2894.                         $this->options['url_to_like']                       = esc_url($_POST['pwal']['url_to_like']);
  2895.                     else
  2896.                         $this->options['url_to_like']                       = '';
  2897.  
  2898.                     if (isset($_POST['pwal']['random']))
  2899.                         $this->options['random']                            = $_POST['pwal']['random'];
  2900.                     else
  2901.                         $this->options['random']                            = '';
  2902.  
  2903.  
  2904.                     break;
  2905.  
  2906.                 default:
  2907.                     break;
  2908.             }
  2909.             //echo "options<pre>"; print_r($this->options); echo "</pre>";
  2910.  
  2911.             $this->options = apply_filters("pwal_before_save_options", $this->options);
  2912.             //echo "_POST<pre>"; print_r($_POST); echo "</pre>";
  2913.             //echo "options<pre>"; print_r($this->options); echo "</pre>";
  2914.             //die();
  2915.             update_option( 'pwal_options', $this->options );
  2916.             add_action( 'admin_notices', array ( &$this, 'saved' ) );
  2917.         } else if ((isset($_GET['page'])) && ($_GET['page'] == 'pay-with-a-like-statistics')) {
  2918.             if ((isset($_GET['action'])) && ($_GET['action'] == 'pwal_delete_stats')) {
  2919.                 if ((isset($_GET['pwal_nonce'])) || (wp_verify_nonce($_GET['pwal_nonce'], 'pwal_delete_stats'))) {
  2920.                     //echo "_GET<pre>"; print_r($_GET); echo "</pre>";
  2921.                     delete_option( 'pwal_statistics' );
  2922.                     add_action( 'admin_notices', array ( &$this, 'saved' ) );
  2923.  
  2924.                 } else {
  2925.                     echo "pwal_nonce failed<br />";
  2926.                 }
  2927.             }
  2928.         }
  2929.     }
  2930.  
  2931.     /**
  2932.      *  Prints "saved" message on top of Admin page
  2933.      */
  2934.     function saved( ) {
  2935.         echo '<div class="updated fade"><p>'.__("<b>[Pay With a Like]</b> Settings saved","pwal").'</p></div>';
  2936.     }
  2937.  
  2938.     /**
  2939.      *  Prints warning message on top of Admin page
  2940.      */
  2941.     function warning( ) {
  2942.         echo '<div class="error"><p>'.__("<b>[Pay With a Like]</b> You are not authorised to do this.","pwal").'</b></p></div>';
  2943.     }
  2944.  
  2945.     /**
  2946.      * Warn admin if no button is activated
  2947.      */
  2948.     function no_button () {
  2949.         if ( $this->button_count() == 0 ) {
  2950.             echo '<div class="updated fade"><p>' .
  2951.                 __("<b>[Pay With a Like]</b> You didn't select any buttons. Plugin will not function as expected.", 'pwal') .
  2952.             '</p></div>';
  2953.         }
  2954.     }
  2955.  
  2956.     /**
  2957.      * Warn admin to make some settings
  2958.      */
  2959.     function notice_settings () {
  2960.         global $pwal_options_page;
  2961.         $screen = get_current_screen();
  2962.  
  2963. //      if ( !$this->options["no_visit"] && $screen->id != $pwal_options_page ) {
  2964. //          /* translators: %s means settings here */
  2965. //          echo '<div class="updated fade"><p>' .
  2966. //              sprintf(__("<b>[Pay With a Like]</b> It looks like you have just installed the plugin. You may want to adjust some %s. If you are using a cache plugin, please clear your cache.", 'pwal'),"<a href='".admin_url('options-general.php?page=pay-with-a-like')."'>".__("settings","pwal")."</a>") .
  2967. //              '</p></div>';
  2968. //      }
  2969.         // If admin visits setting page, remove this annoying message :P
  2970.         if ( $screen->id == $pwal_options_page && !$this->options["no_visit"] ) {
  2971.             $this->options["no_visit"] = "true";
  2972.             update_option( "pwal_options", $this->options );
  2973.         }
  2974.         // If this is localhost, warn admin
  2975.         if ( stripos( home_url(), 'localhost' ) !== false )
  2976.             echo '<div class="error"><p>'.__("<b>[Pay With a Like]</b> As Social Networking scripts cannot access your local pages, plugin will not function properly in localhost.","pwal").'</b></p></div>';
  2977.  
  2978.         // Warn wrong language setting
  2979.         $locale = preg_replace('/-/', '_', get_locale());
  2980.         if ( 'en' == strtolower( $locale ) && $this->options["use_facebook"] && $this->options["load_facebook"] )
  2981.             echo '<div class="error"><p>'.__("<b>[Pay With a Like]</b> Your WPLANG setting in wp-config.php is wrong. Facebook button will not work.","pwal").'</b></p></div>';
  2982.  
  2983.     }
  2984.  
  2985.     /**
  2986.      *  Get saved postbox states
  2987.      */
  2988.     function postbox_classes( $css_id ) {
  2989.         if ( function_exists( 'postbox_classes' ) )
  2990.             return postbox_classes( $css_id, $this->page );
  2991.         else
  2992.             return "";
  2993.  
  2994.     }
  2995.  
  2996.     function is_content_enabled($post_id = 0) {
  2997.         global $post;
  2998.  
  2999.         if ((!$post_id) || (empty($post_id))) {
  3000.             if (!isset($post->ID))
  3001.                 return;
  3002.             $post_id = $post->ID;
  3003.         }
  3004.         $pwal_enable =  get_post_meta( $post_id, 'pwal_enable', true );
  3005.         //echo "pwal_enable[". $pwal_enable ."]<br />";
  3006.         if (empty($pwal_enable)) {
  3007.             if (isset($this->options['post_types'][$post->post_type]))
  3008.                 $pwal_enable = 'enable';
  3009.             else
  3010.                 $pwal_enable = 'disable';
  3011.         }
  3012.         return $pwal_enable;
  3013.     }
  3014.  
  3015.     /**
  3016.      *  Admin settings HTML code
  3017.      */
  3018.     function settings() {
  3019.  
  3020.         if (!current_user_can('manage_options')) {
  3021.             wp_die( __('You do not have sufficient permissions to access this page.', 'pwal') );
  3022.         }
  3023.  
  3024.         $pwal_page = esc_attr($_GET['page']);
  3025.         if (empty($pwal_page)) return;
  3026.  
  3027.         require_once dirname(__FILE__) . '/lib/pwal_admin_panels.php';
  3028.         ?>
  3029.         <div class="wrap pwal_wrap">
  3030.             <?php
  3031.                 switch($pwal_page) {
  3032.                     case 'pay-with-a-like':
  3033.  
  3034.                         ?>
  3035.                         <form id="pwal-settings-form-<?php echo $pwal_page; ?>" class="pwal-settings-form" method="post" action="<?php admin_url( '/options-general.php?page='. $pwal_page ); ?>">
  3036.                             <?php wp_nonce_field( 'pwal_update_settings', 'pwal_nonce' ); ?>
  3037.                             <input type="hidden" name="pwal_action" value="update_pwal" />
  3038.                             <input type="hidden" name="page" value="<?php echo $pwal_page; ?>" />
  3039.  
  3040.                             <h2><?php _e('Pay With a Like Settings', 'pwal') ?></h2>
  3041.                             <div id="poststuff" class="pwal-settings">
  3042.                                 <div id="post-body" class="metabox-holder columns-1">
  3043.                                     <div id="post-body-content">
  3044.                                         <?php pwal_admin_panels_global(); ?>
  3045.                                         <?php pwal_admin_panels_defaults(); ?>
  3046.                                         <?php pwal_admin_panels_container(); ?>
  3047.                                         <?php pwal_admin_panels_visibility(); ?>
  3048.                                     </div>
  3049.                                 </div>
  3050.                             </div>
  3051.                             <p class="submit"><input type="submit" class="button-primary" value="<?php _e('Save Changes', 'pwal') ?>" /></p>
  3052.                         </form>
  3053.                         <?php
  3054.                         break;
  3055. /*
  3056.                     case 'pay-with-a-like-accessibility':
  3057.                         ?>
  3058.                         <form id="pwal-settings-form-<?php echo $pwal_page; ?>" class="pwal-settings-form" method="post" action="<?php admin_url( '/options-general.php?page='. $pwal_page ); ?>">
  3059.                             <?php wp_nonce_field( 'pwal_update_settings', 'pwal_nonce' ); ?>
  3060.                             <input type="hidden" name="pwal_action" value="update_pwal" />
  3061.                             <input type="hidden" name="page" value="<?php echo $pwal_page; ?>" />
  3062.                             <h2><?php _e('Pay With a Like Accessibility Settings','pwal') ?></h2>
  3063.                             <div id="poststuff" class="pwal-settings">
  3064.                                 <div id="post-body" class="metabox-holder columns-1">
  3065.                                     <div id="post-body-content">
  3066.                                         <?php pwal_admin_panels_visibility(); ?>
  3067.                                     </div>
  3068.                                 </div>
  3069.                             </div>
  3070.                             <p class="submit"><input type="submit" class="button-primary" value="<?php _e('Save Changes', 'pwal') ?>" /></p>
  3071.                         </form>
  3072.                         <?php
  3073.                         break;
  3074. */
  3075.                     case 'pay-with-a-like-buttons':
  3076.                         $tabs = array(
  3077.                             'buttons'       => __('Social Buttons', 'pwal'),
  3078.                             'facebook'      => __('Facebook', 'pwal'),
  3079.                             'linkedin'      => __('LinkedIn', 'pwal'),
  3080.                             'twitter'       => __('Twitter', 'pwal'),
  3081.                             'google'        => __('Google', 'pwal'),
  3082.                         );
  3083.                         ?>
  3084.                         <form id="pwal-settings-form-<?php echo $pwal_page; ?>" class="pwal-settings-form" method="post" action="<?php admin_url( '/options-general.php?page='. $pwal_page ); ?>">
  3085.                             <?php wp_nonce_field( 'pwal_update_settings', 'pwal_nonce' ); ?>
  3086.                             <input type="hidden" name="pwal_action" value="update_pwal" />
  3087.                             <input type="hidden" name="page" value="<?php echo $pwal_page; ?>" />
  3088.                             <h2><?php _e('Pay With a Like Social Buttons','pwal') ?></h2>
  3089.  
  3090.                             <h2 class="nav-tab-wrapper"><?php
  3091.                                 if ( isset( $_GET['tab'] )) {
  3092.                                     $current_tab = $_GET['tab'];
  3093.                                     if (!isset($tabs[$current_tab])) {
  3094.                                         $current_tab = 'buttons';
  3095.                                     }
  3096.                                 } else {
  3097.                                     $current_tab = 'buttons';
  3098.                                 }
  3099.                                 ?><input type="hidden" name="tab" value="<?php echo $current_tab; ?>" /><?php
  3100.                                 foreach( $tabs as $tab => $name ) {
  3101.                                     if (($tab == "facebook") && ($this->options['use_facebook'] != "true")) continue;
  3102.                                     if (($tab == "linkedin") && ($this->options['use_linkedin'] != "true")) continue;
  3103.                                     if (($tab == "twitter") && ($this->options['use_twitter'] != "true")) continue;
  3104.                                     if (($tab == "google") && ($this->options['use_google'] != "true")) continue;
  3105.  
  3106.                                     $class = ( $tab == $current_tab ) ? ' nav-tab-active' : '';
  3107.                                     echo "<a class='nav-tab$class' href='?page=$pwal_page&tab=$tab'>$name</a>";
  3108.                                 }
  3109.                                 ?></h2>
  3110.                                 <div id="poststuff" class="pwal-settings">
  3111.                                     <div id="post-body" class="metabox-holder columns-1">
  3112.                                         <div id="post-body-content">
  3113.                                         <?php
  3114.                                             switch($current_tab) {
  3115.                                                 case 'facebook':
  3116.                                                     pwal_admin_panels_facebook();
  3117.                                                     break;
  3118.  
  3119.                                                 case 'linkedin':
  3120.                                                     pwal_admin_panels_linkedin();
  3121.                                                     break;
  3122.  
  3123.                                                 case 'twitter':
  3124.                                                     pwal_admin_panels_twitter();
  3125.                                                     break;
  3126.  
  3127.                                                 case 'google':
  3128.                                                     pwal_admin_panels_google();
  3129.                                                     break;
  3130.  
  3131.                                                 case 'buttons':
  3132.                                                     pwal_admin_panels_social();
  3133.                                                     break;
  3134.                                             }
  3135.                                         ?>
  3136.                                         </div>
  3137.                                     </div>
  3138.                                 </div>
  3139.                                 <p class="submit"><input type="submit" class="button-primary" value="<?php _e('Save Changes', 'pwal') ?>" /></p>
  3140.  
  3141.                             </form>
  3142.                         <?php
  3143.                         break;
  3144.  
  3145.                     case 'pay-with-a-like-statistics':
  3146.                         //echo "_GET<pre>"; print_r($_GET); echo "</pre>";
  3147.  
  3148.                         ?>
  3149.                         <h2><?php _e('Pay With a Like Statistics', 'pwal') ?><?php
  3150.                         if (isset($_GET['post'])) {
  3151.                             ?> - <a href="<?php echo get_edit_post_link( intval($_GET['post']) ); ?>"><?php echo get_the_title(intval($_GET['post'])) ?> </a><?php
  3152.                         }
  3153.                         ?></h2>
  3154.                         <div id="poststuff" class="pwal-settings">
  3155.                             <div id="post-body" class="metabox-holder columns-2">
  3156.                                 <div id="post-body-content">
  3157.                                     <?php pwal_admin_panels_statistic_chart(); ?>
  3158.                                     <?php //pwal_admin_panels_statistics(); ?>
  3159.                                 </div>
  3160.                                 <div id="postbox-container-1" class="postbox-container">
  3161.                                     <div id="side-sortables" class="meta-box-sortables ui-sortable">
  3162.                                         <?php pwal_admin_panels_statistic_actions(); ?>
  3163.                                         <?php pwal_admin_panels_statistic_summary(); ?>
  3164.                                         <?php pwal_admin_panels_statistic_top_pages(); ?>
  3165.                                         <?php pwal_admin_panels_statistic_top_ipaddress(); ?>
  3166.  
  3167.                                     </div>
  3168.                                 </div>
  3169.                             </div>
  3170.                         </div>
  3171.                         <?php
  3172.                         break;
  3173.  
  3174.                     case 'pay-with-a-like-customization':
  3175.                         ?>
  3176.                         <h2><?php _e('Pay With a Like Customization', 'pwal') ?></h2>
  3177.                         <div id="poststuff" class="pwal-settings">
  3178.                             <div id="post-body" class="metabox-holder columns-1">
  3179.                                 <div id="post-body-content">
  3180.                                     <?php pwal_admin_panels_customization(); ?>
  3181.                                 </div>
  3182.                             </div>
  3183.                         </div>
  3184.                         <?php
  3185.                         break;
  3186.  
  3187.                     default:
  3188.                     break;
  3189.  
  3190.                 }
  3191.             ?>
  3192.         </div>
  3193.         <?php
  3194.     }
  3195.  
  3196.     function delete_stats(){
  3197.         check_ajax_referer( $this->options["salt"], 'nonce' );
  3198.         if ( !delete_option( 'pwal_statistics' ) )
  3199.             die( json_encode( array('error' => __('Statistics could not be deleted','pwal'))));
  3200.     }
  3201.  
  3202.     function export_stats(){
  3203.         //check_ajax_referer( $this->options["salt"], 'nonce' );
  3204.  
  3205.         $stats = get_option( "pwal_statistics" );
  3206.         if ( !is_array( $stats ) )
  3207.             die(__('Nothing to download!','pwal'));
  3208.  
  3209.         $file = fopen('php://temp/maxmemory:'. (12*1024*1024), 'r+');
  3210.         fputcsv( $file, array("Blog ID","Post ID","Content ID","Social Button","User IP","Date","Time") );
  3211.  
  3212.         $date_format = get_option('date_format');
  3213.         $time_format = get_option('time_format');
  3214.  
  3215.         foreach ( $stats as $stat_key => $stat_value ) {
  3216.             if ( is_array( $stat_value ) ) {
  3217.                 foreach ( $stat_value as $key => $value ) {
  3218.                     if ( "t" == $key ) {
  3219.                         $stats[$stat_key]["t"] = date_i18n( $date_format, $value );
  3220.                         $stats[$stat_key]["time"] = date_i18n( $time_format, $value );
  3221.                     }
  3222.                 }
  3223.             }
  3224.             else
  3225.                 die(__('Nothing to download!','pwal'));
  3226.         }
  3227.  
  3228.         foreach ( $stats as $stat ) {
  3229.             fputcsv( $file, $stat );
  3230.         }
  3231.  
  3232.         $filename = "stats_".date('F')."_".date('d')."_".date('Y').".csv";
  3233.  
  3234.         //serve the file
  3235.         rewind($file);
  3236.         ob_end_clean(); //kills any buffers set by other plugins
  3237.         header('Content-Description: File Transfer');
  3238.         header('Content-Type: text/csv');
  3239.         header('Content-Disposition: attachment; filename="'.$filename.'"');
  3240.         header('Content-Transfer-Encoding: binary');
  3241.         header('Expires: 0');
  3242.         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  3243.         header('Pragma: public');
  3244.         $output = stream_get_contents($file);
  3245.         //$output = $output . "\xEF\xBB\xBF"; // UTF-8 BOM
  3246.         header('Content-Length: ' . strlen($output));
  3247.         fclose($file);
  3248.         die($output);
  3249.     }
  3250.  
  3251.     /**
  3252.      *  Adds tinyMCE editor to the post editor
  3253.      *  Modified from Password Protect Selected Content by Aaron Edwards
  3254.      */
  3255.     function load_tinymce() {
  3256.  
  3257.         if (isset($_GET['post_type'])) {
  3258.             $post_type = esc_attr($_GET['post_type']);
  3259.         } else {
  3260.             if (isset($_GET['post'])) {
  3261.                 $post_id = intval($_GET['post']);
  3262.                 $post = get_post($post_id);
  3263.  
  3264.                 if (($post) && ($post->post_type)) {
  3265.                     $post_type = $post->post_type;
  3266.                 }
  3267.             } else {
  3268.                 $post_type = 'post';
  3269.             }
  3270.         }
  3271.         if (isset($this->options['show_metabox'][$post_type])) {
  3272.             if ( current_user_can('edit_posts') && (get_user_option('rich_editing') == 'true') ) {
  3273.                 add_filter( 'mce_external_plugins', array(&$this, 'tinymce_add_plugin') );
  3274.                 add_filter( 'mce_buttons', array(&$this,'tinymce_register_button') );
  3275.                 add_filter( 'mce_external_languages', array(&$this,'tinymce_load_langs') );
  3276.             }
  3277.         }
  3278.     }
  3279.  
  3280.     /**
  3281.      * TinyMCE dialog content
  3282.      */
  3283.     function tinymce_options() {
  3284.         ?>
  3285.         <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  3286.         <html>
  3287.             <head>
  3288.                 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  3289.                 <script type="text/javascript" src="../wp-includes/js/tinymce/tiny_mce_popup.js?ver=327-1235"></script>
  3290.                 <script type="text/javascript" src="../wp-includes/js/tinymce/utils/form_utils.js?ver=327-1235"></script>
  3291.                 <script type="text/javascript" src="../wp-includes/js/tinymce/utils/editable_selects.js?ver=327-1235"></script>
  3292.  
  3293.                 <script type="text/javascript" src="../wp-includes/js/jquery/jquery.js"></script>
  3294.  
  3295.                 <script type="text/javascript">
  3296.  
  3297.                     tinyMCEPopup.storeSelection();
  3298.  
  3299.                     var insertPayWithaLike = function (ed) {
  3300.                         var description = jQuery.trim(jQuery('#pwal-description').val());
  3301.                         var id = Math.round((new Date()).getTime() / 1000) -1330955000;
  3302.                         tinyMCEPopup.restoreSelection();
  3303.                         output = '[pwal id="'+id+'" description="'+description+'"]'+tinyMCEPopup.editor.selection.getContent()+'[/pwal]';
  3304.  
  3305.                         tinyMCEPopup.execCommand('mceInsertContent', 0, output);
  3306.                         tinyMCEPopup.editor.execCommand('mceRepaint');
  3307.                         tinyMCEPopup.editor.focus();
  3308.                         // Return
  3309.                         tinyMCEPopup.close();
  3310.                     };
  3311.                 </script>
  3312.                 <style type="text/css">
  3313.                 td.info {
  3314.                     vertical-align: top;
  3315.                     color: #777;
  3316.                 }
  3317.                 </style>
  3318.  
  3319.                 <title><?php _e("Pay With a Like", 'pwal'); ?></title>
  3320.             </head>
  3321.             <body style="display: none">
  3322.  
  3323.                 <form onsubmit="insertPayWithaLike();return false;" action="#">
  3324.  
  3325.                     <div id="general_panel" class="panel current">
  3326.                         <div id="pwal-error" style="display: none;color:#C00;padding: 2px 0;"><?php _e("Please enter a value!", 'pwal'); ?></div>
  3327.                             <fieldset>
  3328.                           <table border="0" cellpadding="4" cellspacing="0">
  3329.                                 <tr>
  3330.                                     <td><label for="chat_width"><?php _e("Description", 'pwal'); ?></label></td>
  3331.                                     <td>
  3332.                                         <input type="text" id="pwal-description" name="pwal-description" value="" class="size" size="30" />
  3333.                                     </td>
  3334.                                     <td class="info"><?php _e("Description for this selection.", 'pwal'); ?></td>
  3335.                                 </tr>
  3336.                             </table>
  3337.                         </fieldset>
  3338.                     </div>
  3339.  
  3340.                     <div class="mceActionPanel">
  3341.                         <div style="float: left">
  3342.                             <input type="button" id="cancel" name="cancel" value="<?php _e("Cancel", 'pwal'); ?>" onclick="tinyMCEPopup.close();" />
  3343.                         </div>
  3344.  
  3345.                         <div style="float: right">
  3346.                             <input type="submit" id="insert" name="insert" value="<?php _e("Insert", 'pwal'); ?>" />
  3347.                         </div>
  3348.                     </div>
  3349.                 </form>
  3350.             </body>
  3351.         </html>
  3352.         <?php
  3353.         exit(0);
  3354.     }
  3355.  
  3356.     /**
  3357.      * @see     http://codex.wordpress.org/TinyMCE_Custom_Buttons
  3358.      */
  3359.     function tinymce_register_button($buttons) {
  3360.         array_push($buttons, "separator", "paywithalike");
  3361.         return $buttons;
  3362.     }
  3363.  
  3364.     /**
  3365.      * @see     http://codex.wordpress.org/TinyMCE_Custom_Buttons
  3366.      */
  3367.     function tinymce_load_langs($langs) {
  3368.         $langs["paywithalike"] =  plugins_url('pay-with-a-like/tinymce/langs/langs.php');
  3369.         return $langs;
  3370.     }
  3371.  
  3372.     /**
  3373.      * @see     http://codex.wordpress.org/TinyMCE_Custom_Buttons
  3374.      */
  3375.     function tinymce_add_plugin($plugin_array) {
  3376.         $plugin_array['paywithalike'] = plugins_url('pay-with-a-like/tinymce/editor_plugin.js');
  3377.         return $plugin_array;
  3378.     }
  3379.  
  3380.     /**
  3381.      *  check if visitor is a bot
  3382.      *
  3383.      */
  3384.     function is_bot(){
  3385.         $botlist = array("Teoma", "alexa", "froogle", "Gigabot", "inktomi",
  3386.         "looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory",
  3387.         "Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot",
  3388.         "crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp",
  3389.         "msnbot", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz",
  3390.         "Baiduspider", "Feedfetcher-Google", "TechnoratiSnoop", "Rankivabot",
  3391.         "Mediapartners-Google", "Sogou web spider", "WebAlta Crawler","TweetmemeBot",
  3392.         "Butterfly","Twitturls","Me.dium","Twiceler");
  3393.  
  3394.         foreach($botlist as $bot){
  3395.             if( strpos($_SERVER['HTTP_USER_AGENT'],$bot)!== false )
  3396.             return true;    // Is a bot
  3397.         }
  3398.  
  3399.         return false;   // Not a bot
  3400.     }
  3401.  
  3402.     function check_size_qualifier($size_str = '', $size_qualifiers = array('px', 'pt', 'em', '%')) {
  3403.         if (empty($size_str)) $size_str = "0"; //return $size_str;
  3404.  
  3405.         if (count($size_qualifiers)) {
  3406.             foreach($size_qualifiers as $size_qualifier) {
  3407.                 if (empty($size_qualifier)) continue;
  3408.  
  3409.                 if ( substr($size_str, strlen($size_qualifier) * -1, strlen($size_qualifier)) === $size_qualifier)
  3410.                     return $size_str;
  3411.             }
  3412.             return intval($size_str) ."px";
  3413.         }
  3414.     }
  3415.  
  3416. /*
  3417.     function closetags ( $html )
  3418.     {
  3419.         #put all opened tags into an array
  3420.         preg_match_all ( "#<([a-z]+)( .*)?(?!/)>#iU", $html, $result );
  3421.         $openedtags = $result[1];
  3422.  
  3423.         #put all closed tags into an array
  3424.         preg_match_all ( "#</([a-z]+)>#iU", $html, $result );
  3425.         $closedtags = $result[1];
  3426.         $len_opened = count ( $openedtags );
  3427.         # all tags are closed
  3428.         if( count ( $closedtags ) == $len_opened )
  3429.         {
  3430.             return $html;
  3431.         }
  3432.         $openedtags = array_reverse ( $openedtags );
  3433.         # close tags
  3434.         for( $i = 0; $i < $len_opened; $i++ )
  3435.         {
  3436.             if ( !in_array ( $openedtags[$i], $closedtags ) )
  3437.             {
  3438.                 $html .= "</" . $openedtags[$i] . ">";
  3439.             }
  3440.             else
  3441.             {
  3442.                 unset ( $closedtags[array_search ( $openedtags[$i], $closedtags)] );
  3443.             }
  3444.         }
  3445.         return $html;
  3446.     }
  3447. */
  3448. }
  3449. }
  3450.  
  3451. $pwal = new PayWithaLike();
  3452. global $pwal;
  3453.  
  3454. if ( !function_exists( 'wpmudev_pwal' ) ) {
  3455.     function wpmudev_pwal( $content='', $force=false ) {
  3456.         global $pwal, $post;
  3457.         if ( $content )
  3458.             return $pwal->content( $content, $force );
  3459.         else
  3460.             return $pwal->content( $post->post_content, $force );
  3461.     }
  3462. }
  3463.  
  3464. if ( !function_exists( 'wpmudev_pwal_html' ) ) {
  3465.     // since 1.1.2
  3466.     function wpmudev_pwal_html( $html='', $id=1, $description='' ) {
  3467.         global $pwal, $post;
  3468.  
  3469.         if ( !empty($html) )
  3470.             $content = $html;
  3471.         else if ( is_object( $post ) )
  3472.             $content = $post->content;
  3473.         else
  3474.             return 'No html code or post content found';
  3475.  
  3476.         //return $pwal->content( '[pwal id="'.$id.'" description="'.$description.'"]'. $content . '[/pwal]', true, 'tool' );
  3477.         return do_shortcode( '[pwal id="'. $id .'" content_reload="refresh" description="'. $description .'"]'. $content . '[/pwal]');
  3478.     }
  3479. }
  3480.  
  3481. if ( !function_exists( 'wpmudev_pwal_check_liked' ) ) {
  3482.     // since 1.1.5.1
  3483.     // Checks if a certain ID has been liked.
  3484.     // $pwal_id is the post ID to check
  3485.     // Returns true if liked else false
  3486.     function wpmudev_pwal_check_liked($pwal_id, $pwal_id_type='post_id') {
  3487.         global $pwal;
  3488.  
  3489.         // Check that $pwal is available
  3490.         if (!$pwal) return false;
  3491.  
  3492.         $pwal->load_cookie_likes();
  3493.         if (empty($pwal->cookie_likes['data'])) return false;
  3494.  
  3495.         // "sitewide like" is selected
  3496.         if ( $pwal->options["sitewide"] )
  3497.             $post_id = $this->sitewide_id;
  3498.         else
  3499.             $post_id = $pwal_id;
  3500.  
  3501.         // Check if this post is liked or sitewide like is selected
  3502.         foreach ( $pwal->cookie_likes['data'] as $like ) {
  3503.             // Cookie is already encrypted, so we are looking if post_id matches to the encryption
  3504.             if ($pwal_id_type == "post_id") {
  3505.                 if ( $like["post_id"] == md5( $post_id . $pwal->options["salt"] ) ) {
  3506.                     return true;
  3507.                 }
  3508.             } else if ($pwal_id_type == "content_id") {
  3509.                 if ($like['content_id'] == $post_id)
  3510.                     return true;
  3511.             }
  3512.         }
  3513.         if ( $pwal->options["admin"] == 'true' && current_user_can('administrator') )
  3514.             return true;
  3515.     }
  3516. }
  3517.  
  3518. //force_balance_tags
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement