main_file = str_replace('-class', '', __FILE__); // Get plugin url $this->plugin_url = WP_PLUGIN_URL . '/' . basename(dirname($this->main_file)); if (strpos($this->plugin_url, 'http') === 0 && is_ssl()) $this->plugin_url = str_replace('http://', 'https://', $this->plugin_url); // Log $this->debug = get_option(c_al2fb_option_debug); // Get site & blog id if (is_multisite()) { $current_site = get_current_site(); $this->site_id = $current_site->id; global $blog_id; $this->blog_id = $blog_id; } // register activation actions register_activation_hook($this->main_file, array(&$this, 'Activate')); register_deactivation_hook($this->main_file, array(&$this, 'Deactivate')); // Register actions add_action('init', array(&$this, 'Init'), 0); if (is_admin()) { add_action('admin_menu', array(&$this, 'Admin_menu')); add_filter('plugin_action_links', array(&$this, 'Plugin_action_links'), 10, 2); add_action('admin_notices', array(&$this, 'Admin_notices')); add_action('post_submitbox_start', array(&$this, 'Post_submitbox')); add_filter('manage_posts_columns', array(&$this, 'Manage_posts_columns')); add_action('manage_posts_custom_column', array(&$this, 'Manage_posts_custom_column'), 10, 2); add_filter('manage_pages_columns', array(&$this, 'Manage_posts_columns')); add_action('manage_pages_custom_column', array(&$this, 'Manage_posts_custom_column'), 10, 2); add_action('add_meta_boxes', array(&$this, 'Add_meta_boxes')); add_action('save_post', array(&$this, 'Save_post')); } add_action('transition_post_status', array(&$this, 'Transition_post_status'), 10, 3); add_action('xmlrpc_publish_post', array(&$this, 'Remote_publish')); add_action('app_publish_post', array(&$this, 'Remote_publish')); add_action('future_to_publish', array(&$this, 'Future_to_publish')); add_action('al2fb_publish', array(&$this, 'Remote_publish')); if (get_option(c_al2fb_option_use_pp)) add_action('publish_post', array(&$this, 'Remote_publish')); // Content add_action('wp_head', array(&$this, 'WP_head')); add_filter('the_content', array(&$this, 'The_content'), 999); add_filter('comments_array', array(&$this, 'Comments_array'), 10, 2); add_filter('get_comments_number', array(&$this, 'Get_comments_number'), 10, 2); add_filter('comment_class', array(&$this, 'Comment_class')); add_filter('get_avatar', array(&$this, 'Get_avatar'), 10, 5); // Shortcodes add_shortcode('al2fb_likers', array(&$this, 'Shortcode_likers')); add_shortcode('al2fb_like_button', array(&$this, 'Shortcode_like_button')); add_filter('al2fb_excerpt', array(&$this, 'Filter_excerpt'), 10, 2); add_filter('al2fb_content', array(&$this, 'Filter_content'), 10, 2); } // Handle plugin activation function Activate() { global $wpdb; $version = get_option(c_al2fb_option_version); if ($version <= 1) { delete_option(c_al2fb_meta_client_id); delete_option(c_al2fb_meta_app_secret); delete_option(c_al2fb_meta_access_token); delete_option(c_al2fb_meta_picture_type); delete_option(c_al2fb_meta_picture); delete_option(c_al2fb_meta_page); delete_option(c_al2fb_meta_clean); delete_option(c_al2fb_meta_donated); } if ($version <= 2) { $rows = $wpdb->get_results("SELECT user_id, meta_value FROM " . $wpdb->usermeta . " WHERE meta_key='al2fb_integrate'"); foreach ($rows as $row) { update_user_meta($row->user_id, c_al2fb_meta_fb_comments, $row->meta_value); update_user_meta($row->user_id, c_al2fb_meta_fb_likes, $row->meta_value); delete_user_meta($row->user_id, 'al2fb_integrate'); } } if ($version <= 3) { global $wpdb; $rows = $wpdb->get_results("SELECT ID FROM " . $wpdb->users); foreach ($rows as $row) update_user_meta($row->ID, c_al2fb_meta_like_faces, true); } if ($version <= 4) { $rows = $wpdb->get_results("SELECT user_id, meta_value FROM " . $wpdb->usermeta . " WHERE meta_key='" . c_al2fb_meta_trailer . "'"); foreach ($rows as $row) { $value = get_user_meta($row->user_id, c_al2fb_meta_trailer, true); update_user_meta($row->user_id, c_al2fb_meta_trailer, ' ' . $value); } } update_option(c_al2fb_option_version, 5); } // Handle plugin deactivation function Deactivate() { global $user_ID; get_currentuserinfo(); // Cleanup if requested if (get_user_meta($user_ID, c_al2fb_meta_clean, true)) { delete_user_meta($user_ID, c_al2fb_meta_shared); delete_user_meta($user_ID, c_al2fb_meta_client_id); delete_user_meta($user_ID, c_al2fb_meta_app_secret); delete_user_meta($user_ID, c_al2fb_meta_access_token); delete_user_meta($user_ID, c_al2fb_meta_picture_type); delete_user_meta($user_ID, c_al2fb_meta_picture); delete_user_meta($user_ID, c_al2fb_meta_picture_default); delete_user_meta($user_ID, c_al2fb_meta_page); delete_user_meta($user_ID, c_al2fb_meta_page_owner); delete_user_meta($user_ID, c_al2fb_meta_use_groups); delete_user_meta($user_ID, c_al2fb_meta_group); delete_user_meta($user_ID, c_al2fb_meta_caption); delete_user_meta($user_ID, c_al2fb_meta_msg); delete_user_meta($user_ID, c_al2fb_meta_shortlink); delete_user_meta($user_ID, c_al2fb_meta_add_new_page); delete_user_meta($user_ID, c_al2fb_meta_trailer); delete_user_meta($user_ID, c_al2fb_meta_hyperlink); delete_user_meta($user_ID, c_al2fb_meta_share_link); delete_user_meta($user_ID, c_al2fb_meta_fb_comments); delete_user_meta($user_ID, c_al2fb_meta_fb_likes); delete_user_meta($user_ID, c_al2fb_meta_post_likers); delete_user_meta($user_ID, c_al2fb_meta_post_like_button); delete_user_meta($user_ID, c_al2fb_meta_like_nohome); delete_user_meta($user_ID, c_al2fb_meta_like_noposts); delete_user_meta($user_ID, c_al2fb_meta_like_nopages); delete_user_meta($user_ID, c_al2fb_meta_like_noarchives); delete_user_meta($user_ID, c_al2fb_meta_like_nocategories); delete_user_meta($user_ID, c_al2fb_meta_like_layout); delete_user_meta($user_ID, c_al2fb_meta_like_faces); delete_user_meta($user_ID, c_al2fb_meta_like_width); delete_user_meta($user_ID, c_al2fb_meta_like_action); delete_user_meta($user_ID, c_al2fb_meta_like_font); delete_user_meta($user_ID, c_al2fb_meta_like_colorscheme); delete_user_meta($user_ID, c_al2fb_meta_like_link); delete_user_meta($user_ID, c_al2fb_meta_like_top); delete_user_meta($user_ID, c_al2fb_meta_like_iframe); delete_user_meta($user_ID, c_al2fb_meta_open_graph); delete_user_meta($user_ID, c_al2fb_meta_open_graph_type); delete_user_meta($user_ID, c_al2fb_meta_exclude_default); delete_user_meta($user_ID, c_al2fb_meta_not_post_list); delete_user_meta($user_ID, c_al2fb_meta_fb_encoding); delete_user_meta($user_ID, c_al2fb_meta_clean); delete_user_meta($user_ID, c_al2fb_meta_donated); delete_user_meta($user_ID, c_al2fb_meta_rated); //delete_option(c_al2fb_option_version); //delete_option(c_al2fb_option_timeout); //delete_option(c_al2fb_option_nonotice); //delete_option(c_al2fb_option_min_cap); //delete_option(c_al2fb_option_msg_refresh); //delete_option(c_al2fb_option_max_descr); //delete_option(c_al2fb_option_exclude_type); //delete_option(c_al2fb_option_exclude_cat); //delete_option(c_al2fb_option_noverifypeer); //delete_option(c_al2fb_option_siteurl); //delete_option(c_al2fb_option_nocurl); //delete_option(c_al2fb_option_use_pp); //delete_option(c_al2fb_option_debug); } } // Initialization function Init() { // Secret request if (isset($_REQUEST['al2fb_check'])) { if ($_REQUEST['al2fb_check'] == self::Authorize_secret()) echo 'OK'; exit(); } // Image request if (isset($_GET['al2fb_image'])) { $img = dirname(__FILE__) . '/wp-blue-s.png'; header('Content-type: image/png'); readfile($img); exit(); } // Set default capability if (!get_option(c_al2fb_option_min_cap)) update_option(c_al2fb_option_min_cap, 'edit_posts'); // I18n load_plugin_textdomain(c_al2fb_text_domain, false, dirname(plugin_basename(__FILE__)) . '/language/'); // Enqueue style sheet $css_name = $this->Change_extension(basename($this->main_file), '.css'); if (file_exists(WP_CONTENT_DIR . '/uploads/' . $css_name)) $css_url = WP_CONTENT_URL . '/uploads/' . $css_name; else if (file_exists(TEMPLATEPATH . '/' . $css_name)) $css_url = get_bloginfo('template_directory') . '/' . $css_name; else $css_url = $this->plugin_url . '/' . $css_name; wp_register_style('al2fb_style', $css_url); wp_enqueue_style('al2fb_style'); // Check user capability if (current_user_can(get_option(c_al2fb_option_min_cap))) { if (is_admin()) { // Enqueue script wp_enqueue_script('jquery'); // Initiate Facebook authorization if (isset($_REQUEST['al2fb_action']) && $_REQUEST['al2fb_action'] == 'init') { // Debug info update_option(c_al2fb_log_redir_init, date('c')); // Get current user global $user_ID; get_currentuserinfo(); // Redirect $auth_url = self::Authorize_url($user_ID); try { // Check if (ini_get('safe_mode') || ini_get('open_basedir')) update_option(c_al2fb_log_redir_check, 'No'); else { $response = self::Request($auth_url, '', 'GET'); update_option(c_al2fb_log_redir_check, date('c')); } // Redirect wp_redirect($auth_url); exit(); } catch (Exception $e) { // Register error update_option(c_al2fb_log_redir_check, $e->getMessage()); update_option(c_al2fb_last_error, $e->getMessage()); update_option(c_al2fb_last_error_time, date('c')); // Redirect $error_url = admin_url('tools.php?page=' . plugin_basename($this->main_file)); $error_url .= '&al2fb_action=error'; $error_url .= '&error=' . urlencode($e->getMessage()); wp_redirect($error_url); exit(); } } } // Handle Facebook authorization self::Authorize(); } } // Display admin messages function Admin_notices() { // Check user capability if (current_user_can(get_option(c_al2fb_option_min_cap))) { // Get current user global $user_ID; get_currentuserinfo(); // Disable shared application if (!self::Client_side_flow_available() && get_user_meta($user_ID, c_al2fb_meta_shared, true)) { update_user_meta($user_ID, c_al2fb_meta_shared, false); delete_user_meta($user_ID, c_al2fb_meta_access_token); } // Check actions if (isset($_REQUEST['al2fb_action'])) { // Configuration if ($_REQUEST['al2fb_action'] == 'config') self::Action_config(); // Authorization else if ($_REQUEST['al2fb_action'] == 'authorize') self::Action_authorize(); // Mail debug info else if ($_REQUEST['al2fb_action'] == 'mail') self::Action_mail(); } self::Check_config(); } } // Save settings function Action_config() { // Security check check_admin_referer(c_al2fb_nonce_form); // Get current user global $user_ID; get_currentuserinfo(); // Default values if (empty($_POST[c_al2fb_meta_shared])) $_POST[c_al2fb_meta_shared] = null; if (empty($_POST[c_al2fb_meta_picture_type])) $_POST[c_al2fb_meta_picture_type] = 'wordpress'; if (empty($_POST[c_al2fb_meta_page])) $_POST[c_al2fb_meta_page] = null; if (empty($_POST[c_al2fb_meta_page_owner])) $_POST[c_al2fb_meta_page_owner] = null; if (empty($_POST[c_al2fb_meta_use_groups])) $_POST[c_al2fb_meta_use_groups] = null; if (empty($_POST[c_al2fb_meta_group])) $_POST[c_al2fb_meta_group] = null; if (empty($_POST[c_al2fb_meta_caption])) $_POST[c_al2fb_meta_caption] = null; if (empty($_POST[c_al2fb_meta_msg])) $_POST[c_al2fb_meta_msg] = null; if (empty($_POST[c_al2fb_meta_shortlink])) $_POST[c_al2fb_meta_shortlink] = null; if (empty($_POST[c_al2fb_meta_add_new_page])) $_POST[c_al2fb_meta_add_new_page] = null; if (empty($_POST[c_al2fb_meta_trailer])) $_POST[c_al2fb_meta_trailer] = null; if (empty($_POST[c_al2fb_meta_hyperlink])) $_POST[c_al2fb_meta_hyperlink] = null; if (empty($_POST[c_al2fb_meta_share_link])) $_POST[c_al2fb_meta_share_link] = null; if (empty($_POST[c_al2fb_meta_fb_comments])) $_POST[c_al2fb_meta_fb_comments] = null; if (empty($_POST[c_al2fb_meta_fb_likes])) $_POST[c_al2fb_meta_fb_likes] = null; if (empty($_POST[c_al2fb_meta_post_likers])) $_POST[c_al2fb_meta_post_likers] = null; if (empty($_POST[c_al2fb_meta_post_like_button])) $_POST[c_al2fb_meta_post_like_button] = null; if (empty($_POST[c_al2fb_meta_like_nohome])) $_POST[c_al2fb_meta_like_nohome] = null; if (empty($_POST[c_al2fb_meta_like_noposts])) $_POST[c_al2fb_meta_like_noposts] = null; if (empty($_POST[c_al2fb_meta_like_nopages])) $_POST[c_al2fb_meta_like_nopages] = null; if (empty($_POST[c_al2fb_meta_like_noarchives])) $_POST[c_al2fb_meta_like_noarchives] = null; if (empty($_POST[c_al2fb_meta_like_nocategories])) $_POST[c_al2fb_meta_like_nocategories] = null; if (empty($_POST[c_al2fb_meta_like_layout])) $_POST[c_al2fb_meta_like_layout] = null; if (empty($_POST[c_al2fb_meta_like_faces])) $_POST[c_al2fb_meta_like_faces] = null; if (empty($_POST[c_al2fb_meta_like_action])) $_POST[c_al2fb_meta_like_action] = null; if (empty($_POST[c_al2fb_meta_like_font])) $_POST[c_al2fb_meta_like_font] = null; if (empty($_POST[c_al2fb_meta_like_colorscheme])) $_POST[c_al2fb_meta_like_colorscheme] = null; if (empty($_POST[c_al2fb_meta_like_top])) $_POST[c_al2fb_meta_like_top] = null; if (empty($_POST[c_al2fb_meta_like_iframe])) $_POST[c_al2fb_meta_like_iframe] = null; if (empty($_POST[c_al2fb_meta_open_graph])) $_POST[c_al2fb_meta_open_graph] = null; if (empty($_POST[c_al2fb_meta_exclude_default])) $_POST[c_al2fb_meta_exclude_default] = null; if (empty($_POST[c_al2fb_meta_not_post_list])) $_POST[c_al2fb_meta_not_post_list] = null; if (empty($_POST[c_al2fb_meta_clean])) $_POST[c_al2fb_meta_clean] = null; if (empty($_POST[c_al2fb_meta_donated])) $_POST[c_al2fb_meta_donated] = null; if (empty($_POST[c_al2fb_meta_rated])) $_POST[c_al2fb_meta_rated] = null; $_POST[c_al2fb_meta_client_id] = trim($_POST[c_al2fb_meta_client_id]); $_POST[c_al2fb_meta_app_secret] = trim($_POST[c_al2fb_meta_app_secret]); $_POST[c_al2fb_meta_picture] = trim(stripslashes($_POST[c_al2fb_meta_picture])); $_POST[c_al2fb_meta_picture_default] = trim(stripslashes($_POST[c_al2fb_meta_picture_default])); $_POST[c_al2fb_meta_trailer] = rtrim($_POST[c_al2fb_meta_trailer]); $_POST[c_al2fb_meta_like_width] = trim($_POST[c_al2fb_meta_like_width]); $_POST[c_al2fb_meta_like_link] = trim($_POST[c_al2fb_meta_like_link]); $_POST[c_al2fb_meta_open_graph_type] = trim($_POST[c_al2fb_meta_open_graph_type]); $_POST[c_al2fb_meta_fb_encoding] = trim($_POST[c_al2fb_meta_fb_encoding]); // Prevent losing selected page if (!self::Is_authorized($user_ID) || (get_user_meta($user_ID, c_al2fb_meta_use_groups, true) && get_user_meta($user_ID, c_al2fb_meta_group, true))) $_POST[c_al2fb_meta_page] = get_user_meta($user_ID, c_al2fb_meta_page, true); // Prevent losing selected group if (!self::Is_authorized($user_ID) || !get_user_meta($user_ID, c_al2fb_meta_use_groups, true)) $_POST[c_al2fb_meta_group] = get_user_meta($user_ID, c_al2fb_meta_group, true); // Shared changed if ($_POST[c_al2fb_meta_shared] != get_user_meta($user_ID, c_al2fb_meta_shared, true)) delete_user_meta($user_ID, c_al2fb_meta_access_token); // App ID or secret changed if (!$_POST[c_al2fb_meta_shared]) if (get_user_meta($user_ID, c_al2fb_meta_client_id, true) != $_POST[c_al2fb_meta_client_id] || get_user_meta($user_ID, c_al2fb_meta_app_secret, true) != $_POST[c_al2fb_meta_app_secret]) delete_user_meta($user_ID, c_al2fb_meta_access_token); // Page owner changed if ($_POST[c_al2fb_meta_page_owner] && !get_user_meta($user_ID, c_al2fb_meta_page_owner, true)) delete_user_meta($user_ID, c_al2fb_meta_access_token); // Use groups changed if ($_POST[c_al2fb_meta_use_groups] && !get_user_meta($user_ID, c_al2fb_meta_use_groups, true)) if (!get_user_meta($user_ID, c_al2fb_meta_group, true)) delete_user_meta($user_ID, c_al2fb_meta_access_token); // Update user options update_user_meta($user_ID, c_al2fb_meta_shared, $_POST[c_al2fb_meta_shared]); update_user_meta($user_ID, c_al2fb_meta_client_id, $_POST[c_al2fb_meta_client_id]); update_user_meta($user_ID, c_al2fb_meta_app_secret, $_POST[c_al2fb_meta_app_secret]); update_user_meta($user_ID, c_al2fb_meta_picture_type, $_POST[c_al2fb_meta_picture_type]); update_user_meta($user_ID, c_al2fb_meta_picture, $_POST[c_al2fb_meta_picture]); update_user_meta($user_ID, c_al2fb_meta_picture_default, $_POST[c_al2fb_meta_picture_default]); update_user_meta($user_ID, c_al2fb_meta_page, $_POST[c_al2fb_meta_page]); update_user_meta($user_ID, c_al2fb_meta_page_owner, $_POST[c_al2fb_meta_page_owner]); update_user_meta($user_ID, c_al2fb_meta_use_groups, $_POST[c_al2fb_meta_use_groups]); update_user_meta($user_ID, c_al2fb_meta_group, $_POST[c_al2fb_meta_group]); update_user_meta($user_ID, c_al2fb_meta_caption, $_POST[c_al2fb_meta_caption]); update_user_meta($user_ID, c_al2fb_meta_msg, $_POST[c_al2fb_meta_msg]); update_user_meta($user_ID, c_al2fb_meta_shortlink, $_POST[c_al2fb_meta_shortlink]); update_user_meta($user_ID, c_al2fb_meta_add_new_page, $_POST[c_al2fb_meta_add_new_page]); update_user_meta($user_ID, c_al2fb_meta_trailer, $_POST[c_al2fb_meta_trailer]); update_user_meta($user_ID, c_al2fb_meta_hyperlink, $_POST[c_al2fb_meta_hyperlink]); update_user_meta($user_ID, c_al2fb_meta_share_link, $_POST[c_al2fb_meta_share_link]); update_user_meta($user_ID, c_al2fb_meta_fb_comments, $_POST[c_al2fb_meta_fb_comments]); update_user_meta($user_ID, c_al2fb_meta_fb_likes, $_POST[c_al2fb_meta_fb_likes]); update_user_meta($user_ID, c_al2fb_meta_post_likers, $_POST[c_al2fb_meta_post_likers]); update_user_meta($user_ID, c_al2fb_meta_post_like_button, $_POST[c_al2fb_meta_post_like_button]); update_user_meta($user_ID, c_al2fb_meta_like_nohome, $_POST[c_al2fb_meta_like_nohome]); update_user_meta($user_ID, c_al2fb_meta_like_noposts, $_POST[c_al2fb_meta_like_noposts]); update_user_meta($user_ID, c_al2fb_meta_like_nopages, $_POST[c_al2fb_meta_like_nopages]); update_user_meta($user_ID, c_al2fb_meta_like_noarchives, $_POST[c_al2fb_meta_like_noarchives]); update_user_meta($user_ID, c_al2fb_meta_like_nocategories, $_POST[c_al2fb_meta_like_nocategories]); update_user_meta($user_ID, c_al2fb_meta_like_layout, $_POST[c_al2fb_meta_like_layout]); update_user_meta($user_ID, c_al2fb_meta_like_faces, $_POST[c_al2fb_meta_like_faces]); update_user_meta($user_ID, c_al2fb_meta_like_width, $_POST[c_al2fb_meta_like_width]); update_user_meta($user_ID, c_al2fb_meta_like_action, $_POST[c_al2fb_meta_like_action]); update_user_meta($user_ID, c_al2fb_meta_like_font, $_POST[c_al2fb_meta_like_font]); update_user_meta($user_ID, c_al2fb_meta_like_colorscheme, $_POST[c_al2fb_meta_like_colorscheme]); update_user_meta($user_ID, c_al2fb_meta_like_link, $_POST[c_al2fb_meta_like_link]); update_user_meta($user_ID, c_al2fb_meta_like_top, $_POST[c_al2fb_meta_like_top]); update_user_meta($user_ID, c_al2fb_meta_like_iframe, $_POST[c_al2fb_meta_like_iframe]); update_user_meta($user_ID, c_al2fb_meta_open_graph, $_POST[c_al2fb_meta_open_graph]); update_user_meta($user_ID, c_al2fb_meta_open_graph_type, $_POST[c_al2fb_meta_open_graph_type]); update_user_meta($user_ID, c_al2fb_meta_exclude_default, $_POST[c_al2fb_meta_exclude_default]); update_user_meta($user_ID, c_al2fb_meta_not_post_list, $_POST[c_al2fb_meta_not_post_list]); update_user_meta($user_ID, c_al2fb_meta_fb_encoding, $_POST[c_al2fb_meta_fb_encoding]); update_user_meta($user_ID, c_al2fb_meta_clean, $_POST[c_al2fb_meta_clean]); update_user_meta($user_ID, c_al2fb_meta_donated, $_POST[c_al2fb_meta_donated]); update_user_meta($user_ID, c_al2fb_meta_rated, $_POST[c_al2fb_meta_rated]); if (isset($_REQUEST['debug'])) { if (empty($_POST[c_al2fb_meta_access_token])) $_POST[c_al2fb_meta_access_token] = null; $_POST[c_al2fb_meta_access_token] = trim($_POST[c_al2fb_meta_access_token]); update_user_meta($user_ID, c_al2fb_meta_access_token, $_POST[c_al2fb_meta_access_token]); } // Update admin options if (current_user_can('manage_options')) { if (empty($_POST[c_al2fb_option_app_share])) $_POST[c_al2fb_option_app_share] = null; else $_POST[c_al2fb_option_app_share] = $user_ID; if (is_multisite()) update_site_option(c_al2fb_option_app_share, $_POST[c_al2fb_option_app_share]); else update_option(c_al2fb_option_app_share, $_POST[c_al2fb_option_app_share]); if (empty($_POST[c_al2fb_option_nonotice])) $_POST[c_al2fb_option_nonotice] = null; if (empty($_POST[c_al2fb_option_min_cap])) $_POST[c_al2fb_option_min_cap] = null; if (empty($_POST[c_al2fb_option_noverifypeer])) $_POST[c_al2fb_option_noverifypeer] = null; $_POST[c_al2fb_option_msg_refresh] = trim($_POST[c_al2fb_option_msg_refresh]); $_POST[c_al2fb_option_max_descr] = trim($_POST[c_al2fb_option_max_descr]); $_POST[c_al2fb_option_exclude_type] = trim($_POST[c_al2fb_option_exclude_type]); $_POST[c_al2fb_option_exclude_cat] = trim($_POST[c_al2fb_option_exclude_cat]); update_option(c_al2fb_option_nonotice, $_POST[c_al2fb_option_nonotice]); update_option(c_al2fb_option_min_cap, $_POST[c_al2fb_option_min_cap]); update_option(c_al2fb_option_msg_refresh, $_POST[c_al2fb_option_msg_refresh]); update_option(c_al2fb_option_max_descr, $_POST[c_al2fb_option_max_descr]); update_option(c_al2fb_option_exclude_type, $_POST[c_al2fb_option_exclude_type]); update_option(c_al2fb_option_exclude_cat, $_POST[c_al2fb_option_exclude_cat]); update_option(c_al2fb_option_noverifypeer, $_POST[c_al2fb_option_noverifypeer]); if (isset($_REQUEST['debug'])) { if (empty($_POST[c_al2fb_option_siteurl])) $_POST[c_al2fb_option_siteurl] = null; if (empty($_POST[c_al2fb_option_nocurl])) $_POST[c_al2fb_option_nocurl] = null; if (empty($_POST[c_al2fb_option_use_pp])) $_POST[c_al2fb_option_use_pp] = null; if (empty($_POST[c_al2fb_option_debug])) $_POST[c_al2fb_option_debug] = null; update_option(c_al2fb_option_siteurl, $_POST[c_al2fb_option_siteurl]); update_option(c_al2fb_option_nocurl, $_POST[c_al2fb_option_nocurl]); update_option(c_al2fb_option_use_pp, $_POST[c_al2fb_option_use_pp]); update_option(c_al2fb_option_debug, $_POST[c_al2fb_option_debug]); } } // Show result echo '

' . __('Settings updated', c_al2fb_text_domain) . '

'; } // Get token function Action_authorize() { // Get current user global $user_ID; get_currentuserinfo(); // Client-side flow authorization if (get_user_meta($user_ID, c_al2fb_meta_shared, true) && isset($_REQUEST['access_token'])) { update_option(c_al2fb_log_auth_time, date('c')); update_user_meta($user_ID, c_al2fb_meta_access_token, $_REQUEST['access_token']); delete_option(c_al2fb_last_error); delete_option(c_al2fb_last_error_time); echo '

' . __('Authorized, go posting!', c_al2fb_text_domain) . '

'; } // Server-side flow authorization if (isset($_REQUEST['code'])) { try { // Get & store token $access_token = self::Get_token($user_ID); update_option(c_al2fb_log_auth_time, date('c')); update_user_meta($user_ID, c_al2fb_meta_access_token, $access_token); delete_option(c_al2fb_last_error); delete_option(c_al2fb_last_error_time); echo '

' . __('Authorized, go posting!', c_al2fb_text_domain) . '

'; } catch (Exception $e) { delete_user_meta($user_ID, c_al2fb_meta_access_token); update_option(c_al2fb_last_error, $e->getMessage()); update_option(c_al2fb_last_error_time, date('c')); echo '

' . htmlspecialchars($e->getMessage(), ENT_QUOTES, get_bloginfo('charset')) . '

'; } } // Authorization error else if (isset($_REQUEST['error'])) { delete_user_meta($user_ID, c_al2fb_meta_access_token); $faq = 'http://wordpress.org/extend/plugins/add-link-to-facebook/faq/'; $msg = stripslashes($_REQUEST['error_description']); $msg .= ' error: ' . stripslashes($_REQUEST['error']); $msg .= ' reason: ' . stripslashes($_REQUEST['error_reason']); update_option(c_al2fb_last_error, $msg); update_option(c_al2fb_last_error_time, date('c')); $msg .= '

Most errors are described in the FAQ'; echo '

' . htmlspecialchars($msg, ENT_QUOTES, get_bloginfo('charset')) . '

'; } } // Send debug info function Action_mail() { // Check security check_admin_referer(c_al2fb_nonce_form); // Build headers $headers = 'From: ' . stripslashes($_POST[c_al2fb_mail_name]) . '<' . stripslashes($_POST[c_al2fb_mail_email]) . '>' . "\r\n"; $headers .= 'X-Mailer: AL2FB' . "\r\n"; $headers .= 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=' . get_bloginfo('charset') . "\r\n"; // Build message $message = 'Add Link to Facebook'; $message .= '

' . nl2br(htmlspecialchars(stripslashes($_POST[c_al2fb_mail_msg]), ENT_QUOTES, get_bloginfo('charset'))) . '

'; $message .= '
'; $message .= self::Debug_info(); $message .= ''; if (mail('marcel@bokhorst.biz', '[Add Link to Facebook] Debug information', $message, $headers)) echo '

' . __('Debug information sent', c_al2fb_text_domain) . '

'; else echo '

' . __('Sending debug information failed', c_al2fb_text_domain) . '

'; } // Display notices function Check_config() { // Get current user global $user_ID; get_currentuserinfo(); // Check config/authorization $uri = $_SERVER['REQUEST_URI']; $url = 'tools.php?page=' . plugin_basename($this->main_file); $nonotice = get_option(c_al2fb_option_nonotice); if (is_multisite()) $nonotice = $nonotice || get_site_option(c_al2fb_option_app_share); else $nonotice = $nonotice || get_option(c_al2fb_option_app_share); $donotice = ($nonotice ? strpos($uri, $url) !== false : true); if ($donotice) { if (!get_user_meta($user_ID, c_al2fb_meta_shared, true) && (!get_user_meta($user_ID, c_al2fb_meta_client_id, true) || !get_user_meta($user_ID, c_al2fb_meta_app_secret, true))) { $notice = __('needs configuration', c_al2fb_text_domain); $anchor = 'configure'; } else if (!self::Is_authorized($user_ID)) { $notice = __('needs authorization', c_al2fb_text_domain); $anchor = 'authorize'; } if (!empty($notice)) { echo '

'; _e('Add Link to Facebook', c_al2fb_text_domain); echo ' ' . $notice . '

'; } } // Check for error if (isset($_REQUEST['al2fb_action']) && $_REQUEST['al2fb_action'] == 'error') { $faq = 'http://wordpress.org/extend/plugins/add-link-to-facebook/faq/'; $msg = htmlspecialchars(stripslashes($_REQUEST['error']), ENT_QUOTES, get_bloginfo('charset')); $msg .= '

Most errors are described in the FAQ'; echo '

' . $msg . '

'; } // Check for post errors $posts = new WP_Query(array( 'author' => $user_ID, 'meta_key' => c_al2fb_meta_error, 'posts_per_page' => 5)); while ($posts->have_posts()) { $posts->next_post(); $error = get_post_meta($posts->post->ID, c_al2fb_meta_error, true); if (!empty($error)) { echo '

'; echo __('Add Link to Facebook', c_al2fb_text_domain) . ' - '; edit_post_link($posts->post->post_title, null, null, $posts->post->ID); echo ': ' . htmlspecialchars($error, ENT_QUOTES, get_bloginfo('charset')) . '

'; } } // Check for rating notice if ($donotice && !get_user_meta($user_ID, c_al2fb_meta_rated, true)) { echo '

'; $msg = __('If you like the Add Link to Facebook plugin, please rate it on wordpress.org.
If the average rating is low, it makes no sense to support this plugin any longer.
You can disable this notice by checking the option "I have rated this plugin" on the settings page.', c_al2fb_text_domain); $msg = str_replace('[wordpress]', 'http://wordpress.org/extend/plugins/add-link-to-facebook/', $msg); $msg = str_replace('[settings]', $url, $msg); echo $msg . '

'; } } // Register options page function Admin_menu() { // Get current user global $user_ID; get_currentuserinfo(); // Check for app share if (is_multisite()) $shared_user_ID = get_site_option(c_al2fb_option_app_share); else $shared_user_ID = get_option(c_al2fb_option_app_share); if ($shared_user_ID && $shared_user_ID != $user_ID) return; if (function_exists('add_management_page')) add_management_page( __('Add Link to Facebook', c_al2fb_text_domain) . ' ' . __('Administration', c_al2fb_text_domain), __('Add Link to Facebook', c_al2fb_text_domain), get_option(c_al2fb_option_min_cap), $this->main_file, array(&$this, 'Administration')); } function Plugin_action_links($links, $file) { if ($file == plugin_basename($this->main_file)) { if (current_user_can(get_option(c_al2fb_option_min_cap))) { // Get current user global $user_ID; get_currentuserinfo(); // Check for shared app if (is_multisite()) $shared_user_ID = get_site_option(c_al2fb_option_app_share); else $shared_user_ID = get_option(c_al2fb_option_app_share); if (!$shared_user_ID || $shared_user_ID == $user_ID) { // Add settings link $config_url = admin_url('tools.php?page=' . plugin_basename($this->main_file)); $links[] = '' . __('Settings', c_al2fb_text_domain) . ''; } } } return $links; } // Handle option page function Administration() { // Security check if (!current_user_can(get_option(c_al2fb_option_min_cap))) die('Unauthorized'); // Get current user global $user_ID; get_currentuserinfo(); $charset = get_bloginfo('charset'); $config_url = admin_url('tools.php?page=' . plugin_basename($this->main_file)); if (isset($_REQUEST['debug'])) $config_url .= '&debug=1'; // Decode picture type $pic_type = get_user_meta($user_ID, c_al2fb_meta_picture_type, true); $pic_wordpress = ($pic_type == 'wordpress' ? ' checked' : ''); $pic_media = ($pic_type == 'media' ? ' checked' : ''); $pic_featured = ($pic_type == 'featured' ? ' checked' : ''); $pic_facebook = ($pic_type == 'facebook' ? ' checked' : ''); $pic_post = ($pic_type == 'post' ? ' checked' : ''); $pic_userphoto = ($pic_type == 'userphoto' ? ' checked' : ''); $pic_custom = ($pic_type == 'custom' ? ' checked' : ''); if (!current_theme_supports('post-thumbnails') || !function_exists('get_post_thumbnail_id') || !function_exists('wp_get_attachment_image_src')) $pic_featured .= ' disabled'; if (!in_array('user-photo/user-photo.php', get_option('active_plugins'))) $pic_userphoto .= ' disabled'; // Like button $like_layout = get_user_meta($user_ID, c_al2fb_meta_like_layout, true); $like_layout_standard = ($like_layout == 'standard' ? ' checked' : ''); $like_layout_button = ($like_layout == 'button_count' ? ' checked' : ''); $like_layout_box = ($like_layout == 'box_count' ? ' checked' : ''); $like_action = get_user_meta($user_ID, c_al2fb_meta_like_action, true); $like_action_like = ($like_action == 'like' ? ' checked' : ''); $like_action_recommend = ($like_action == 'recommend' ? ' checked' : ''); $like_font = get_user_meta($user_ID, c_al2fb_meta_like_font, true); $like_color = get_user_meta($user_ID, c_al2fb_meta_like_colorscheme, true); $like_color_light = ($like_color == 'light' ? ' checked' : ''); $like_color_dark = ($like_color == 'dark' ? ' checked' : ''); // Sustainable Plugins Sponsorship Network self::Render_sponsorship(); ?>

' . __('Your server may not allow external connections', c_al2fb_text_domain) . '

'; self::Render_debug_info(); self::Render_resources(); ?>

link . '" target="_blank">' . htmlspecialchars($me->name, ENT_QUOTES, $charset); if (!empty($me->category)) echo ' - ' . htmlspecialchars($me->category, ENT_QUOTES, $charset); echo ''; } catch (Exception $e) { echo '

' . htmlspecialchars($e->getMessage(), ENT_QUOTES, $charset) . '

'; } } ?>


/> Beta!


>
>
>
>
>
>
>


/>
/>

' . htmlspecialchars($e->getMessage(), ENT_QUOTES, $charset) . '

'; } ?>
/>
""
/>

/>
/> Experimental!
/>
/>
/>
/>
/>
/>
/>
/>
/>
/>
/>
>
>
>
/>
>
>
>
>

/>
/>
/>

/>
/>

/>
/>
/>

/>




/>

/>
/>
/>
/>


main_file)]['Version']; $available = self::Request(c_al2fb_app_url, 'available=' . c_al2fb_app_version . '&plugin=' . $plugin_version, 'GET'); set_transient(c_al2fb_transient_available, $available, 60 * 60); } catch (Exception $e) { $available = false; } return $available; } // Get Facebook authorize addess function Authorize_url($user_ID) { // http://developers.facebook.com/docs/authentication/permissions $url = 'https://graph.facebook.com/oauth/authorize'; $shared = get_user_meta($user_ID, c_al2fb_meta_shared, true); if ($shared) { $url .= '?client_id=' . c_al2fb_app_id; $url .= '&redirect_uri=' . urlencode(c_al2fb_app_url); } else { $url .= '?client_id=' . get_user_meta($user_ID, c_al2fb_meta_client_id, true); $url .= '&redirect_uri=' . urlencode(self::Redirect_uri()); } $url .= '&scope=publish_stream,offline_access'; if (get_user_meta($user_ID, c_al2fb_meta_page_owner, true)) $url .= ',manage_pages'; if (get_user_meta($user_ID, c_al2fb_meta_use_groups, true)) $url .= ',user_groups'; $url .= '&state=' . self::Authorize_secret(); if ($shared) { $url .= ',' . urlencode(self::Redirect_uri()); $url .= ',' . c_al2fb_app_version; $url .= '&response_type=token'; } return $url; } // Get Facebook return addess function Redirect_uri() { // WordPress Address -> get_site_url() -> WordPress folder // Blog Address -> get_home_url() -> Home page if (get_option(c_al2fb_option_siteurl)) return get_site_url(null, '/'); else return get_home_url(null, '/'); } // Generate authorization secret function Authorize_secret() { return 'al2fb_auth_' . substr(md5(AUTH_KEY ? AUTH_KEY : get_bloginfo('url')), 0, 10); } // Handle Facebook authorization function Authorize() { parse_str($_SERVER['QUERY_STRING'], $query); if (isset($query['state']) && strpos($query['state'], self::Authorize_secret()) !== false) { // Build new url $query['state'] = ''; $query['al2fb_action'] = 'authorize'; $url = admin_url('tools.php?page=' . plugin_basename($this->main_file)); $url .= '&' . http_build_query($query, '', '&'); // Debug info update_option(c_al2fb_log_redir_time, date('c')); update_option(c_al2fb_log_redir_ref, (empty($_SERVER['HTTP_REFERER']) ? null : $_SERVER['HTTP_REFERER'])); update_option(c_al2fb_log_redir_from, $_SERVER['REQUEST_URI']); update_option(c_al2fb_log_redir_to, $url); // Redirect wp_redirect($url); exit(); } } // Request token function Get_token($user_ID) { $url = 'https://graph.facebook.com/oauth/access_token'; $query = http_build_query(array( 'client_id' => get_user_meta($user_ID, c_al2fb_meta_client_id, true), 'redirect_uri' => self::Redirect_uri(), 'client_secret' => get_user_meta($user_ID, c_al2fb_meta_app_secret, true), 'code' => $_REQUEST['code'] ), '', '&'); $response = self::Request($url, $query, 'GET'); $key = 'access_token='; $access_token = substr($response, strpos($response, $key) + strlen($key)); $access_token = explode('&', $access_token); $access_token = $access_token[0]; return $access_token; } // Get application properties function Get_application($user_ID) { if (get_user_meta($user_ID, c_al2fb_meta_shared, true)) $app_id = c_al2fb_app_id; else $app_id = get_user_meta($user_ID, c_al2fb_meta_client_id, true); $url = 'https://graph.facebook.com/' . $app_id; $query = http_build_query(array( 'access_token' => get_user_meta($user_ID, c_al2fb_meta_access_token, true) ), '', '&'); $response = self::Request($url, $query, 'GET'); $app = json_decode($response); return $app; } // Get wall or page name function Get_me($user_ID, $self) { if (get_user_meta($user_ID, c_al2fb_meta_use_groups, true)) $page_id = get_user_meta($user_ID, c_al2fb_meta_group, true); if (empty($page_id)) $page_id = get_user_meta($user_ID, c_al2fb_meta_page, true); if ($self || empty($page_id)) $page_id = 'me'; $url = 'https://graph.facebook.com/' . $page_id; $query = http_build_query(array( 'access_token' => self::Get_access_token_by_page($user_ID, $page_id) ), '', '&'); $response = self::Request($url, $query, 'GET'); $me = json_decode($response); if ($me) { if (empty($me->link)) // Group $me->link = 'http://www.facebook.com/home.php?sk=group_' . $page_id; return $me; } else throw new Exception('Page ' . $page_id . ' not found'); } // Get page list function Get_pages($user_ID) { $url = 'https://graph.facebook.com/me/accounts'; $query = http_build_query(array( 'access_token' => get_user_meta($user_ID, c_al2fb_meta_access_token, true) ), '', '&'); $response = self::Request($url, $query, 'GET'); $accounts = json_decode($response); return $accounts; } // Get group list function Get_groups($user_ID) { $url = 'https://graph.facebook.com/me/groups'; $query = http_build_query(array( 'access_token' => get_user_meta($user_ID, c_al2fb_meta_access_token, true) ), '', '&'); $response = self::Request($url, $query, 'GET'); $groups = json_decode($response); return $groups; } // Get comments function Get_comments($user_ID, $id) { $url = 'https://graph.facebook.com/' . $id . '/comments'; $query = http_build_query(array( 'access_token' => get_user_meta($user_ID, c_al2fb_meta_access_token, true) ), '', '&'); $response = self::Request($url, $query, 'GET'); $comments = json_decode($response); return $comments; } // Get comments function Get_likes($user_ID, $id) { $url = 'https://graph.facebook.com/' . $id . '/likes'; $query = http_build_query(array( 'access_token' => get_user_meta($user_ID, c_al2fb_meta_access_token, true) ), '', '&'); $response = self::Request($url, $query, 'GET'); $likes = json_decode($response); return $likes; } // Get comments function Get_picture_url($id, $size) { if (function_exists('get_header')) { $headers = get_headers('https://graph.facebook.com/' . $id . '/picture?' . $size, true); if (isset($headers['Location'])) return $headers['Location']; else return false; } else return false; } // Add exclude checkbox function Post_submitbox() { global $post; $user_ID = self::Get_user_ID($post); // Check if links for pages enabled if ($post->post_type == 'page') if (!get_user_meta($user_ID, c_al2fb_meta_add_new_page, true)) return; // Get exclude indication $exclude = get_post_meta($post->ID, c_al2fb_meta_exclude, true); $link_id = get_post_meta($post->ID, c_al2fb_meta_link_id, true); if (!$link_id && get_user_meta($user_ID, c_al2fb_meta_exclude_default, true)) $exclude = true; $chk_exclude = ($exclude ? 'checked' : ''); // Get no like button indication $chk_nolike = (get_post_meta($post->ID, c_al2fb_meta_nolike, true) ? 'checked' : ''); ?>
/>
/>
' . ($link_id ? __('Yes', c_al2fb_text_domain) : __('No', c_al2fb_text_domain)) . ''; if ($link_id) { $post = get_post($post_ID); $user_ID = self::Get_user_ID($post); // Show number of comments if (get_user_meta($user_ID, c_al2fb_meta_fb_comments, true)) { $fb_comments = self::Get_fb_comments($post, false); if (!empty($fb_comments)) echo '
' . count($fb_comments->data) . ' ' . __('comments', c_al2fb_text_domain) . ''; } // Show number of likes if (get_user_meta($user_ID, c_al2fb_meta_fb_likes, true)) { $fb_likes = self::Get_fb_comments($post, true); if (!empty($fb_likes)) echo '
' . count($fb_comments->data) . ' ' . __('likes', c_al2fb_text_domain) . ''; } } } } // Add post meta box function Add_meta_boxes() { add_meta_box( 'al2fb_meta', __('Add Link to Facebook', c_al2fb_text_domain), array(&$this, 'Meta_box'), 'post'); add_meta_box( 'al2fb_meta', __('Add Link to Facebook', c_al2fb_text_domain), array(&$this, 'Meta_box'), 'page'); } // Display attached image selector function Meta_box() { // Security wp_nonce_field(plugin_basename(__FILE__), c_al2fb_nonce_form); if ($this->debug) { global $post; $texts = self::Get_texts($post); echo 'Original: ' . htmlspecialchars($post->post_content) . '
'; echo 'Processed: ' . htmlspecialchars($texts['content']) . '
'; } if (function_exists('wp_get_attachment_image_src')) { // Get attached images global $post; $images = &get_children('post_type=attachment&post_mime_type=image&order=ASC&post_parent=' . $post->ID); if (empty($images)) echo '' . __('No images in the media library for this post', c_al2fb_text_domain) . '
'; else { // Display image selector $disabled = get_post_meta($post->ID, c_al2fb_meta_link_id, true); $disabled = (empty($disabled) ? '' : ' disabled'); $image_id = get_post_meta($post->ID, c_al2fb_meta_image_id, true); // Header echo '

' . __('Select link image:', c_al2fb_text_domain) . '

'; echo '
'; // None echo '
'; echo ''; echo '
'; echo ''; echo '
'; // Images foreach ($images as $attachment_id => $attachment) { $picture = wp_get_attachment_image_src($attachment_id, 'thumbnail'); echo '
'; echo ''; echo '
'; echo ''; echo '
'; echo '' . $picture[1] . ' x ' . $picture[2] . ''; echo '
'; } echo '
'; } } else echo 'wp_get_attachment_image_src does not exist'; $excerpt = get_post_meta($post->ID, c_al2fb_meta_excerpt, true); echo '

' . __('Custom exerpt', c_al2fb_text_domain) . '

'; echo ''; } // Save selected attached image function Save_post($post_id) { // Security checks $nonce = (isset($_POST[c_al2fb_nonce_form]) ? $_POST[c_al2fb_nonce_form] : null); if (!wp_verify_nonce($nonce, plugin_basename(__FILE__))) return $post_id; if (!current_user_can('edit_post', $post_id)) return $post_id; // Skip auto save if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id; // Process exclude indication if (isset($_POST[c_al2fb_meta_exclude]) && $_POST[c_al2fb_meta_exclude]) update_post_meta($post_id, c_al2fb_meta_exclude, true); else delete_post_meta($post_id, c_al2fb_meta_exclude); // Process no like indication if (isset($_POST[c_al2fb_meta_nolike]) && $_POST[c_al2fb_meta_nolike]) update_post_meta($post_id, c_al2fb_meta_nolike, true); else delete_post_meta($post_id, c_al2fb_meta_nolike); // Persist data if (isset($_POST['al2fb_image_id'])) update_post_meta($post_id, c_al2fb_meta_image_id, $_POST['al2fb_image_id']); if (isset($_POST['al2fb_excerpt'])) update_post_meta($post_id, c_al2fb_meta_excerpt, $_POST['al2fb_excerpt']); } // Remote publish & custom action function Remote_publish($post_ID) { $post = get_post($post_ID); // Log if ($this->debug) add_post_meta($post->ID, c_al2fb_meta_log, 'Remote ' . $post->post_status . ' @' . date('c')); // Only if published if ($post->post_status == 'publish') self::Publish_post($post); } // Workaround function Future_to_publish($post_ID) { $post = get_post($post_ID); // Log if ($this->debug) add_post_meta($post->ID, c_al2fb_meta_log, 'Future_to_publish @' . date('c')); // Delegate self::Transition_post_status('publish', 'future', $post); } // Handle post status change function Transition_post_status($new_status, $old_status, $post) { $user_ID = self::Get_user_ID($post); $delete = (isset($_POST['al2fb_delete']) && $_POST['al2fb_delete']); // Log if ($this->debug) { global $al2fb_transition_count; if (isset($al2fb_transition_count)) $al2fb_transition_count++; else $al2fb_transition_count = 1; $msg = ($delete ? 'Delete' : 'Add') . ':'; $msg .= ' ' . $old_status . '->' . $new_status . ':' . $post->post_status; $msg .= ((self::user_can($user_ID, get_option(c_al2fb_option_min_cap)) ? '' : ' no') . ' can;'); $msg .= ((get_post_meta($post->ID, c_al2fb_meta_error, true) ? '' : ' no') . ' err;'); $msg .= ' @' . date('c'); $msg .= ' #' . $al2fb_transition_count; add_post_meta($post->ID, c_al2fb_meta_log, $msg); } // Security check if (self::user_can($user_ID, get_option(c_al2fb_option_min_cap))) { // Add or delete link if ($delete) { $link_id = get_post_meta($post->ID, c_al2fb_meta_link_id, true); if (!empty($link_id) && self::Is_authorized($user_ID)) self::Delete_link($post); } else { // Check post status if ($new_status == 'publish' && ($new_status != $old_status || get_post_meta($post->ID, c_al2fb_meta_error, true))) self::Publish_post($post); } } } // Handle publish post / XML-RPC publish post function Publish_post($post) { $user_ID = self::Get_user_ID($post); // Log if ($this->debug) { $msg = 'Publish ' . $post->post_type . ':'; $msg .= ((self::user_can($user_ID, get_option(c_al2fb_option_min_cap)) ? '' : ' no') . ' can;'); $msg .= ((self::Is_authorized($user_ID) ? '' : ' no') . ' auth;'); $msg .= ((get_post_meta($post->ID, c_al2fb_meta_link_id, true) ? '' : ' no') . ' lnk;'); $msg .= ((get_post_meta($post->ID, c_al2fb_meta_exclude, true) ? '' : ' no') . ' ex;'); $msg .= ((empty($post->post_password) ? ' no' : '') . ' pwd;'); $msg .= ' ' . strlen($post->post_excerpt) . ':' . strlen($post->post_content); $msg .= ' @' . date('c'); add_post_meta($post->ID, c_al2fb_meta_log, $msg); } // Checks if (self::user_can($user_ID, get_option(c_al2fb_option_min_cap))) { // Check if not added if (self::Is_authorized($user_ID) && !get_post_meta($post->ID, c_al2fb_meta_link_id, true) && !get_post_meta($post->ID, c_al2fb_meta_exclude, true)) { $add_new_page = get_user_meta($user_ID, c_al2fb_meta_add_new_page, true); $exclude_category = false; $categories = get_the_category($post->ID); $excluding_categories = explode(',', get_option(c_al2fb_option_exclude_cat)); foreach ($categories as $category) if (in_array($category->cat_ID, $excluding_categories)) $exclude_category = true; // Check if public post if (empty($post->post_password) && ($post->post_type != 'page' || $add_new_page) && !in_array($post->post_type, explode(',', get_option(c_al2fb_option_exclude_type))) && !$exclude_category) self::Add_link($post); } } } // Build texts for link/ogp function Get_texts($post) { $user_ID = self::Get_user_ID($post); // Filter texts $excerpt = get_post_meta($post->ID, c_al2fb_meta_excerpt, true); if (empty($excerpt)) $excerpt = $post->post_excerpt; $excerpt = apply_filters('al2fb_excerpt', $excerpt, $post); $content = apply_filters('al2fb_content', $post->post_content, $post); // Get body $description = ''; if (get_user_meta($user_ID, c_al2fb_meta_msg, true)) $description = $content; else $description = ($excerpt ? $excerpt : $content); // Trailer: limit body size $trailer = get_user_meta($user_ID, c_al2fb_meta_trailer, true); if ($trailer) { $trailer = preg_replace('/<[^>]*>/', '', $trailer); // Get maximum FB description size $maxlen = get_option(c_al2fb_option_max_descr); if (!$maxlen) $maxlen = 256; // Add maximum number of sentences $lines = explode('.', $description); if ($lines) { $count = 0; $description = ''; foreach ($lines as $sentence) { $line = $sentence; if ($count + 1 < count($lines)) $line .= '.'; if (strlen($description) + strlen($line) + strlen($trailer) < $maxlen) $description .= $line; else break; } if (empty($description) && count($lines) > 0) $description = substr($lines[0], 0, $maxlen - strlen($trailer)); $description .= $trailer; } } // Build result $texts = array( 'excerpt' => $excerpt, 'content' => $content, 'description' => $description ); return $texts; } // Get link picture function Get_link_picture($post, $user_ID) { // Get selected image $image_id = get_post_meta($post->ID, c_al2fb_meta_image_id, true); if (!empty($image_id) && function_exists('wp_get_attachment_thumb_url')) { $picture_type = 'meta'; $picture = wp_get_attachment_thumb_url($image_id); } if (empty($picture)) { // Default picture $picture = get_user_meta($user_ID, c_al2fb_meta_picture_default, true); if (empty($picture)) $picture = self::Redirect_uri() . '?al2fb_image=1'; // Check picture type $picture_type = get_user_meta($user_ID, c_al2fb_meta_picture_type, true); if ($picture_type == 'media') { $images = array_values(get_children('post_type=attachment&post_mime_type=image&order=ASC&post_parent=' . $post->ID)); if (!empty($images) && function_exists('wp_get_attachment_image_src')) { $picture = wp_get_attachment_image_src($images[0]->ID, 'thumbnail'); if ($picture && $picture[0]) $picture = $picture[0]; } } else if ($picture_type == 'featured') { if (current_theme_supports('post-thumbnails') && function_exists('get_post_thumbnail_id') && function_exists('wp_get_attachment_image_src')) { $picture_id = get_post_thumbnail_id($post->ID); if ($picture_id) { $picture = wp_get_attachment_image_src($picture_id, 'thumbnail'); if ($picture && $picture[0]) $picture = $picture[0]; } } } else if ($picture_type == 'facebook') $picture = ''; else if ($picture_type == 'post') { if (preg_match('/< *img[^>]*src *= *["\']([^"\']*)["\']/i', do_shortcode($post->post_content), $matches)) $picture = $matches[1]; } else if ($picture_type == 'userphoto') { $userdata = get_userdata($post->post_author); if ($userdata->userphoto_approvalstatus == USERPHOTO_APPROVED) { $image_file = $userdata->userphoto_image_file; $upload_dir = wp_upload_dir(); $picture = trailingslashit($upload_dir['baseurl']) . 'userphoto/' . $image_file; } } else if ($picture_type == 'custom') { $custom = get_user_meta($user_ID, c_al2fb_meta_picture, true); if ($custom) $picture = $custom; } } return array( 'picture' => $picture, 'picture_type' => $picture_type ); } function Filter_excerpt($excerpt, $post) { return self::Filter_standard($excerpt, $post); } function Filter_content($content, $post) { return self::Filter_standard($content, $post); } function Filter_standard($text, $post) { $user_ID = self::Get_user_ID($post); // Convert to UTF-8 if needed $text = self::Convert_encoding($user_ID, $text); // Execute shortcodes $text = do_shortcode($text); // Remove scripts $text = preg_replace('//im', '', $text); // Replace hyperlinks if (get_user_meta($user_ID, c_al2fb_meta_hyperlink, true)) $text = preg_replace('/< *a[^>]*href *= *["\']([^"\']*)["\'][^<]*/i', '$1', $text); // Remove image captions $text = preg_replace('/]*class="wp-caption-text"[^>]*>[^<]*<\/p>/i', '', $text); // Get plain texts $text = preg_replace('/<[^>]*>/', '', $text); return $text; } // Convert charset function Convert_encoding($user_ID, $text) { $blog_encoding = get_option('blog_charset'); $fb_encoding = get_user_meta($user_ID, c_al2fb_meta_fb_encoding, true); if (empty($fb_encoding)) $fb_encoding = 'UTF-8'; if ($blog_encoding != $fb_encoding && function_exists('mb_convert_encoding')) return mb_convert_encoding($text, $fb_encoding, $blog_encoding); else return $text; } // Add Link to Facebook function Add_link($post) { // Get url $user_ID = self::Get_user_ID($post); if (get_user_meta($user_ID, c_al2fb_meta_shortlink, true)) $link = wp_get_shortlink($post->ID); if (empty($link)) $link = get_permalink($post->ID); // Get processed texts $texts = self::Get_texts($post); $excerpt = $texts['excerpt']; $content = $texts['content']; $description = $texts['description']; // Get name $name = self::Convert_encoding($user_ID, $post->post_title); // Get caption $caption = ''; if (get_user_meta($user_ID, c_al2fb_meta_caption, true)) { $caption = html_entity_decode(get_bloginfo('title'), ENT_QUOTES, get_bloginfo('charset')); $caption = self::Convert_encoding($user_ID, $caption); } // Log if ($this->debug) { $picture_type = get_user_meta($user_ID, c_al2fb_meta_picture_type, true); $log = 'Picture type: ' . $picture_type . PHP_EOL; $image_id = get_post_meta($post->ID, c_al2fb_meta_image_id, true); $log .= '- meta: ' . $image_id . PHP_EOL; $images = array_values(get_children('post_type=attachment&post_mime_type=image&order=ASC&post_parent=' . $post->ID)); $log .= '- attached: ' . print_r($images, true); $picture_id = get_post_thumbnail_id($post->ID); $log .= '- featured: ' . $picture_id . PHP_EOL; if (preg_match('/< *img[^>]*src *= *["\']([^"\']*)["\']/i', do_shortcode($post->post_content), $matches)) $log .= '- post: ' . $matches[1] . PHP_EOL; else $log .= '- post: none' . PHP_EOL; $custom = get_user_meta($user_ID, c_al2fb_meta_picture, true); $log .= '- custom: ' . $custom . PHP_EOL; add_post_meta($post->ID, c_al2fb_meta_log, $log); } // Get link picture $picture_info = self::Get_link_picture($post, $user_ID); $picture = $picture_info['picture']; $picture_type = $picture_info['picture_type']; // Get user note $message = ''; if (get_user_meta($user_ID, c_al2fb_meta_msg, true)) $message = $excerpt; // Do not disturb WordPress try { // Build request if (get_user_meta($user_ID, c_al2fb_meta_use_groups, true)) $page_id = get_user_meta($user_ID, c_al2fb_meta_group, true); if (empty($page_id)) $page_id = get_user_meta($user_ID, c_al2fb_meta_page, true); if (empty($page_id)) $page_id = 'me'; $url = 'https://graph.facebook.com/' . $page_id . '/feed'; $query_array = array( 'access_token' => self::Get_access_token_by_post($post), 'link' => $link, 'name' => $name, 'caption' => $caption, 'description' => $description, 'picture' => $picture, 'message' => $message ); // Add share link if (get_user_meta($user_ID, c_al2fb_meta_share_link, true)) { // http://forum.developers.facebook.net/viewtopic.php?id=50049 // http://bugs.developers.facebook.net/show_bug.cgi?id=9075 $actions = array( 'name' => __('Share', c_al2fb_text_domain), 'link' => 'http://www.facebook.com/share.php?u=' . urlencode($link) . '&t=' . urlencode($post->post_title) ); $query_array['actions'] = json_encode($actions); } // http://developers.facebook.com/docs/reference/api/link/ $query = http_build_query($query_array, '', '&'); if ($this->debug) { add_post_meta($post->ID, c_al2fb_meta_log, print_r($query_array, true)); add_post_meta($post->ID, c_al2fb_meta_log, $query); } // Execute request $response = self::Request($url, $query, 'POST'); if ($this->debug) add_post_meta($post->ID, c_al2fb_meta_log, print_r($response, true)); $fb_link = json_decode($response); // Register link/date add_post_meta($post->ID, c_al2fb_meta_link_id, $fb_link->id); update_post_meta($post->ID, c_al2fb_meta_link_time, date('c')); update_post_meta($post->ID, c_al2fb_meta_link_picture, $picture_type . '=' . $picture); delete_post_meta($post->ID, c_al2fb_meta_error); } catch (Exception $e) { add_post_meta($post->ID, c_al2fb_meta_error, $e->getMessage()); update_post_meta($post->ID, c_al2fb_meta_link_time, date('c')); update_post_meta($post->ID, c_al2fb_meta_link_picture, $picture_type . '=' . $picture); } } // Add Link to Facebook function Delete_link($post) { // Do not disturb WordPress try { // Build request // http://developers.facebook.com/docs/reference/api/link/ $link_id = get_post_meta($post->ID, c_al2fb_meta_link_id, true); $url = 'https://graph.facebook.com/' . $link_id; $query = http_build_query(array( 'access_token' => self::Get_access_token_by_post($post), 'method' => 'delete' ), '', '&'); if ($this->debug) add_post_meta($post->ID, c_al2fb_meta_log, $query); // Execute request $response = self::Request($url, $query, 'POST'); if ($this->debug) add_post_meta($post->ID, c_al2fb_meta_log, print_r($response, true)); // Delete meta data delete_post_meta($post->ID, c_al2fb_meta_link_id); delete_post_meta($post->ID, c_al2fb_meta_link_time); delete_post_meta($post->ID, c_al2fb_meta_link_picture); delete_post_meta($post->ID, c_al2fb_meta_error); } catch (Exception $e) { add_post_meta($post->ID, c_al2fb_meta_error, $e->getMessage()); update_post_meta($post->ID, c_al2fb_meta_link_time, date('c')); } } function Is_authorized($user_ID) { return get_user_meta($user_ID, c_al2fb_meta_access_token, true); } // Get correct access for post function Get_access_token_by_post($post) { $user_ID = self::Get_user_ID($post); $page_id = get_user_meta($user_ID, c_al2fb_meta_page, true); return self::Get_access_token_by_page($user_ID, $page_id); } // Get access token for page function Get_access_token_by_page($user_ID, $page_id) { $access_token = get_user_meta($user_ID, c_al2fb_meta_access_token, true); if ($page_id && $page_id != 'me' && get_user_meta($user_ID, c_al2fb_meta_page_owner, true)) { $found = false; $pages = self::Get_pages($user_ID); foreach ($pages->data as $page) if ($page->id == $page_id) { $found = true; $access_token = $page->access_token; } } return $access_token; } // HTML header function WP_head() { if (is_single() || is_page()) { global $post; $user_ID = self::Get_user_ID($post); if (get_user_meta($user_ID, c_al2fb_meta_open_graph, true)) { $charset = get_bloginfo('charset'); $title = html_entity_decode(get_bloginfo('title'), ENT_QUOTES, get_bloginfo('charset')); // Get link picture $link_picture = get_post_meta($post->ID, c_al2fb_meta_link_picture, true); if (empty($link_picture)) { $picture_info = self::Get_link_picture($post, $user_ID); $picture = $picture_info['picture']; if (empty($picture)) $picture = self::Redirect_uri() . '?al2fb_image=1'; } else $picture = substr($link_picture, strpos($link_picture, '=') + 1); // Get type $ogp_type = get_user_meta($user_ID, c_al2fb_meta_open_graph_type, true); if (empty($ogp_type)) $ogp_type = 'article'; // Generate meta echo '' . PHP_EOL; echo '' . PHP_EOL; echo '' . PHP_EOL; echo '' . PHP_EOL; echo '' . PHP_EOL; if (is_single()) { $texts = self::Get_texts($post); echo '' . PHP_EOL; } $appid = get_user_meta($user_ID, c_al2fb_meta_client_id, true); if (!empty($appid)) echo '' . PHP_EOL; } } else if (is_home()) { $charset = get_bloginfo('charset'); $title = html_entity_decode(get_bloginfo('title'), ENT_QUOTES, get_bloginfo('charset')); echo '' . PHP_EOL; echo '' . PHP_EOL; echo '' . PHP_EOL; } } // Post content function The_content($content = '') { global $post; $user_ID = self::Get_user_ID($post); if (!(get_user_meta($user_ID, c_al2fb_meta_like_nohome, true) && is_home()) && !(get_user_meta($user_ID, c_al2fb_meta_like_noposts, true) && is_single()) && !(get_user_meta($user_ID, c_al2fb_meta_like_nopages, true) && is_page()) && !(get_user_meta($user_ID, c_al2fb_meta_like_noarchives, true) && is_archive()) && !(get_user_meta($user_ID, c_al2fb_meta_like_nocategories, true) && is_category())) { // Show likers if (get_user_meta($user_ID, c_al2fb_meta_post_likers, true)) { $likers = self::Get_likers($post); if (!empty($likers)) if (get_user_meta($user_ID, c_al2fb_meta_like_top, true)) $content = $likers . $content; else $content .= $likers; } // Show like button if (get_user_meta($user_ID, c_al2fb_meta_post_like_button, true) && !get_post_meta($post->ID, c_al2fb_meta_nolike, true)) { $like_button = self::Get_like_button($post); if (!empty($like_button)) if (get_user_meta($user_ID, c_al2fb_meta_like_top, true)) $content = $like_button . $content; else $content .= $like_button; } } return $content; } // Shortcode likers names function Shortcode_likers($atts) { extract(shortcode_atts(array('post_id' => null), $atts)); if (empty($post_id)) global $post; else $post = get_post($post_id); if (isset($post)) return self::Get_likers($post); } // Shortcode like button function Shortcode_like_button($atts) { extract(shortcode_atts(array('post_id' => null), $atts)); if (empty($post_id)) global $post; else $post = get_post($post_id); if (isset($post)) return self::Get_like_button($post); } // Get HTML for likers function Get_likers($post) { $likers = ''; $charset = get_bloginfo('charset'); $fb_likes = self::Get_fb_comments($post, true); if ($fb_likes) foreach ($fb_likes->data as $fb_like) { if (!empty($likers)) $likers .= ', '; $link = 'http://www.facebook.com/profile.php?id=' . $fb_like->id; $likers .= '' . htmlspecialchars($fb_like->name, ENT_QUOTES, $charset) . ''; } if (!empty($likers)) { $likers .= ' ' . _n('liked this post', 'liked this post', count($fb_likes->data), c_al2fb_text_domain) . ''; $likers = '
' . $likers . '
'; } return $likers; } // Get HTML for like button function Get_like_button($post) { $user_ID = self::Get_user_ID($post); // Get language $lang = WPLANG; if (empty($lang)) $lang = 'en_US'; // Get options $layout = get_user_meta($user_ID, c_al2fb_meta_like_layout, true); $faces = get_user_meta($user_ID, c_al2fb_meta_like_faces, true); $width = get_user_meta($user_ID, c_al2fb_meta_like_width, true); $action = get_user_meta($user_ID, c_al2fb_meta_like_action, true); $font = get_user_meta($user_ID, c_al2fb_meta_like_font, true); $colorscheme = get_user_meta($user_ID, c_al2fb_meta_like_colorscheme, true); $link = get_user_meta($user_ID, c_al2fb_meta_like_link, true); if (empty($link)) $link = get_permalink($post->ID); // Build content if (get_user_meta($user_ID, c_al2fb_meta_like_iframe, true)) { $height = '80'; if ($layout == 'standard') $height = '80'; else if ($layout == 'button_count') $height = '21'; else if ($layout == 'box_count') $height = '65'; $content = '