main_file = $bt[0]['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); // 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_action('admin_notices', array(&$this, 'Admin_notices')); add_action('save_post', array(&$this, 'Save_post'), 10, 2); add_action('publish_post', array(&$this, 'Publish_post')); add_action('post_submitbox_start', array(&$this, 'Post_submitbox')); } add_action('xmlrpc_publish_post', array(&$this, 'Publish_post')); //add_action('app_publish_post', array(&$this, 'Publish_post')); } // Handle plugin activation function Activate() { if (!get_option(c_al2fb_option_version)) { // Set version update_option(c_al2fb_option_version, 1); } } // Handle plugin deactivation function Deactivate() { // Cleanup if requested if (get_option(c_al2fb_option_clean)) { // Delete options delete_option(c_al2fb_option_version); delete_option(c_al2fb_option_timeout); delete_option(c_al2fb_option_donated); delete_option(c_al2fb_option_clean); // Delete user meta values global $wpdb; $rows = $wpdb->get_results("SELECT user_id, meta_key FROM " . $wpdb->usermeta . " WHERE meta_key LIKE 'al2fb_%'"); foreach ($rows as $row) delete_usermeta($row->user_id, $row->meta_key); } } function Init() { if (is_admin()) { // I18n load_plugin_textdomain(c_al2fb_text_domain, false, dirname(plugin_basename(__FILE__))); // 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'); } // Handle Facebook authorization $uri = $_SERVER['REQUEST_URI']; if (strpos($_SERVER['HTTP_REFERER'], 'facebook.com/connect') && strpos($uri, basename(dirname($this->main_file))) === false && (strpos($uri, 'code=') || strpos($uri, 'error='))) { // http://www.facebook.com/connect/uiserver.php?... $url = admin_url('tools.php?page=' . plugin_basename($this->main_file)); header('HTTP/1.1 302 Found'); header('Location: ' . $url . '&action=authorize&' . substr($uri, strpos($uri, '?') + 1)); exit(); } } // Display admin messages function Admin_notices() { // Get current user global $user_ID; get_currentuserinfo(); // Check if action if (isset($_REQUEST['action'])) { // Configuration if ($_REQUEST['action'] == 'config') { // Check security check_admin_referer(c_al2fb_nonce_form); // Invalidate access token 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); if (empty($_POST[c_al2fb_meta_picture_type])) $_POST[c_al2fb_meta_picture_type] = null; if (empty($_POST[c_al2fb_option_clean])) $_POST[c_al2fb_option_clean] = null; if (empty($_POST[c_al2fb_option_donated])) $_POST[c_al2fb_option_donated] = null; // Update options 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]); if (current_user_can('manage_options')) { update_option(c_al2fb_option_clean, $_POST[c_al2fb_option_clean]); update_option(c_al2fb_option_donated, $_POST[c_al2fb_option_donated]); } // Show result echo '

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

'; } // Authorization else if ($_REQUEST['action'] == 'authorize') { if (isset($_REQUEST['code'])) { try { $access_token = self::Get_token(); update_user_meta($user_ID, c_al2fb_meta_access_token, $access_token); echo '

' . __('Authorized', c_al2fb_text_domain) . '

'; } catch (Exception $e) { delete_user_meta($user_ID, c_al2fb_meta_access_token); echo '

' . $e->getMessage() . '

'; } } else if (isset($_REQUEST['error'])) { // error_reason, error, error_description echo '

' . $_REQUEST['error_description'] . '

'; } } } // Check config/authorization if (!get_user_meta($user_ID, c_al2fb_meta_client_id, true) || !get_user_meta($user_ID, c_al2fb_meta_app_secret, true)) $msg = __('needs configuration', c_al2fb_text_domain); else if (!get_user_meta($user_ID, c_al2fb_meta_access_token, true)) $msg = __('needs authorization', c_al2fb_text_domain); if (!empty($msg)) { $url = admin_url('tools.php?page=' . plugin_basename($this->main_file)); echo '

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

'; } } // Register options page function Admin_menu() { 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), 'edit_posts', $this->main_file, array(&$this, 'Administration')); } // Handle option page function Administration() { // Security check if (!current_user_can('edit_posts')) die('Unauthorized'); // Get current user global $user_ID; get_currentuserinfo(); self::Sponsorship(); $pic_type = get_user_meta($user_ID, c_al2fb_meta_picture_type, true); $pic_wordpress = ($pic_type == 'wordpress' ? ' checked' : ''); $pic_featured = ($pic_type == 'featured' ? ' checked' : ''); $pic_facebook = ($pic_type == 'facebook' ? ' checked' : ''); $pic_custom = ($pic_type == 'custom' ? ' checked' : ''); ?>


Site URL:', c_al2fb_text_domain); ?>
>
>
>
>

/>
/>

link . '" target="_blank">' . $me->name . ''; } catch (Exception $e) { echo '

' . $e->getMessage() . '

'; } ?>

get_user_meta($user_ID, c_al2fb_meta_client_id, true), 'redirect_uri' => get_site_url(null, '/', 'http'), '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 profile data function Get_me() { // Get current user global $user_ID; get_currentuserinfo(); $url = 'https://graph.facebook.com/me'; $query = http_build_query(array( 'access_token' => get_user_meta($user_ID, c_al2fb_meta_access_token, true) )); $response = self::Request($url, $query, 'GET'); $me = json_decode($response); return $me; } // Add exclude checkbox function Post_submitbox() { global $post; $exclude = get_post_meta($post->ID, c_al2fb_meta_exclude, true); $chk_exclude = $exclude ? 'checked' : ''; ?>
/>
post_status == 'publish' && empty($post->post_password)) { // Get link picture $picture = ''; $picture_type = get_user_meta($user_ID, c_al2fb_meta_picture_type, true); if (!$picture_type || $picture_type == 'wordpress') $picture = 'http://s.wordpress.org/about/images/logo-blue/blue-s.png'; else if ($picture_type == 'featured') { $picture_id = get_post_thumbnail_id($post_ID); if ($picture_id) { $picture = wp_get_attachment_image_src($picture_id, 'large'); $picture = $picture[0]; } } else if ($picture_type == 'custom') $picture = get_user_meta($user_ID, c_al2fb_meta_picture, true); // Add link try { // http://developers.facebook.com/docs/reference/api $url = 'https://graph.facebook.com/me/feed'; $excerpt = do_shortcode($post->post_excerpt ? $post->post_excerpt : $post->post_content); $excerpt = preg_replace('/<[^>]*>/', '', $excerpt); $query = http_build_query(array( 'access_token' => get_user_meta($user_ID, c_al2fb_meta_access_token, true), 'link' => get_permalink($post_ID), 'name' => $post->post_title, //'caption' => $picture, 'description' => $excerpt, //'icon' => 'Add Link to Facebook', 'picture' => $picture //'message' => 'Add Link to Facebook' )); $response = self::Request($url, $query, 'POST'); $link = json_decode($response); // Register link add_post_meta($post_ID, c_al2fb_meta_link_id, $link->id); } catch (Exception $e) { // Do not disturb WordPress add_post_meta($post_ID, c_al2fb_meta_error, $e->getMessage()); } } } } // Generic http request function Request($url, $query, $type) { // Get timeout $timeout = get_option(c_al2fb_option_timeout); if (!$timeout) $timeout = 60; // Use cURL if available if (function_exists('curl_init')) return self::curl_file_get_contents($url, $query, $type, $timeout); if (version_compare(PHP_VERSION, '5.2.1') < 0) ini_set('default_socket_timeout', $timeout); set_error_handler(array(&$this, 'PHP_error_handler')); if ($type == 'GET') { $context = stream_context_create(array( 'http' => array( 'method' => 'GET', 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'timeout' => $timeout ) )); $content = file_get_contents($url . '?' . $query, false, $context); } else { $context = stream_context_create(array( 'http' => array( 'method' => 'POST', 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'timeout' => $timeout, 'content' => $query ) )); $content = file_get_contents($url, false, $context); } restore_error_handler(); // Check for errors $status = explode(' ', $http_response_header[0]); $status = intval($status[1]); if ($status == 200) return $content; else throw new Exception('Error ' . $status . ': ' . $this->php_error); } // Persist PHP errors function PHP_error_handler($errno, $errstr) { $this->php_error = $errstr; } // cURL http request function curl_file_get_contents($url, $query, $type, $timeout) { $c = curl_init(); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); curl_setopt($c, CURLOPT_TIMEOUT, $timeout); if ($type == 'GET') curl_setopt($c, CURLOPT_URL, $url . '?' . $query); else { curl_setopt($c, CURLOPT_URL, $url); curl_setopt($c, CURLOPT_POST, true); curl_setopt($c, CURLOPT_POSTFIELDS, $query); } $content = curl_exec($c); $status = curl_getinfo($c, CURLINFO_HTTP_CODE); curl_close($c); if ($status == 200) return $content; else { $error = json_decode($content); $error = empty($error->error->message) ? $content : $error->error->message; throw new Exception('Error ' . $status . ': ' . $error); } } // Check environment function Check_prerequisites() { // Check PHP version if (version_compare(PHP_VERSION, '5.0.0', '<')) die('Add Link to Facebook requires at least PHP 5.0.0'); // Check WordPress version global $wp_version; if (version_compare($wp_version, '3.0') < 0) die('Add Link to Facebook requires at least WordPress 3.0'); // Check basic prerequisities self::Check_function('add_action'); self::Check_function('add_filter'); self::Check_function('wp_register_style'); self::Check_function('wp_enqueue_style'); self::Check_function('file_get_contents'); self::Check_function('json_decode'); } function Check_function($name) { if (!function_exists($name)) die('Required WordPress function "' . $name . '" does not exist'); } // Change file extension function Change_extension($filename, $new_extension) { return preg_replace('/\..+$/', $new_extension, $filename); } } } ?>