web/wp-content/plugins/network-publisher/networkpub.php
changeset 170 8e3a5a6fc63e
child 194 32102edaa81b
equal deleted inserted replaced
169:2d410dc8c1f3 170:8e3a5a6fc63e
       
     1 <?php
       
     2 /*
       
     3 Plugin Name: Network Publisher
       
     4 Plugin URI: http://www.linksalpha.com/
       
     5 Description: Automatically posts your Articles to Social Networks - Facebook, Twitter, and LinkedIn
       
     6 Version: 1.0.4
       
     7 Author: Vivek Puri
       
     8 Author URI: http://vivekpuri.com
       
     9 */
       
    10 
       
    11 define('WIDGET_NAME', 'Network Publisher');
       
    12 define('WIDGET_NAME_INTERNAL', 'networkpub');
       
    13 define('WIDGET_PREFIX', 'networkpub');
       
    14 define('NETWORKPUB', 'Automatically post your Articles to Social Networks - Facebook and Twitter');
       
    15 define('ERROR_INTERNAL', 'internal error');
       
    16 define('ERROR_INVALID_URL', 'invalid url');
       
    17 define('ERROR_INVALID_KEY', 'invalid key');
       
    18 if ( ! defined( 'WP_CONTENT_URL' ) )
       
    19       define( 'WP_CONTENT_URL', get_option( 'siteurl' ) . '/wp-content' );
       
    20 if ( ! defined( 'WP_CONTENT_DIR' ) )
       
    21       define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
       
    22 define('WP_PLUGIN_URL', WP_CONTENT_URL. '/plugins' );
       
    23 
       
    24 $networkpub_settings['api_key'] = array('label'=>'API Key:', 'type'=>'text', 'default'=>'');
       
    25 $networkpub_settings['id'] = array('label'=>'id', 'type'=>'text', 'default'=>'');
       
    26 $options = get_option(WIDGET_NAME_INTERNAL);
       
    27 
       
    28 function networkpub_init() {
       
    29 	wp_enqueue_script('jquery');
       
    30 	wp_register_script('networkpubjs', WP_PLUGIN_URL .'/network-publisher/networkpub.js');
       
    31 	wp_enqueue_script('networkpubjs');
       
    32 	wp_register_style('networkpubcss', WP_PLUGIN_URL . '/network-publisher/networkpub.css');
       
    33 	wp_enqueue_style('networkpubcss');
       
    34 	add_action('admin_menu', 'networkpub_pages');	
       
    35 	add_action('{$new_status}_{$post->post_type}', 'networkping');
       
    36 	add_action('publish_post', 'networkping');
       
    37 }
       
    38 
       
    39 add_action('init', 'networkpub_init');
       
    40 add_action('init', 'networkpub_remove');
       
    41 
       
    42 function networkping($id) {
       
    43 	if(!$id) {
       
    44 		return FALSE;
       
    45 	}
       
    46 	$options = get_option(WIDGET_NAME_INTERNAL);
       
    47 	$link = 'http://www.linksalpha.com/a/ping?id='.$options['id'];
       
    48 	require_once(ABSPATH.WPINC.'/class-snoopy.php');
       
    49 	$snoop = new Snoopy;
       
    50 	$snoop->agent = WIDGET_NAME.' - '.get_option('siteurl');
       
    51 	$response = '';
       
    52 	if($snoop->fetchtext($link)){
       
    53 		if (strpos($snoop->response_code, '200')) {
       
    54 			return TRUE;
       
    55 		} 
       
    56 	}
       
    57 	return FALSE;
       
    58 }
       
    59 
       
    60 function networkpub_pages() {
       
    61 	if ( function_exists('add_submenu_page') ) {
       
    62 		$page = add_submenu_page('plugins.php', WIDGET_NAME, WIDGET_NAME, 'manage_options', 'networkpub', 'networkpub_conf');
       
    63 	}
       
    64 }
       
    65 
       
    66 function networkpub_conf() {
       
    67 	global $networkpub_settings;
       
    68 	
       
    69 	if ( isset($_POST['submit']) ) {
       
    70 		if ( function_exists('current_user_can') && !current_user_can('manage_options') ) {
       
    71 			die(__('Cheatin&#8217; uh?'));
       
    72 		}
       
    73 		$field_name = sprintf('%s_%s', WIDGET_PREFIX, 'api_key');
       
    74 		$value = strip_tags(stripslashes($_POST[$field_name]));
       
    75 		if($value) {
       
    76 			$networkadd = networkpub_add($value);	
       
    77 		}
       
    78 	}
       
    79 	$options = get_option(WIDGET_NAME_INTERNAL);
       
    80 	$html  = '<div class="rts_header"><big><strong>'.WIDGET_NAME.'</strong></big></div>';
       
    81 	$html .= '<table style="width:800px;"><tr><td style="padding-bottom:40px;">';
       
    82 	$html .= '<div style="padding:0px 0px 10px 0px;">Network Publisher makes it easy and painless to Publish your Blog Posts to Social Networks. All you need to do is connect to your Social Networks at <a target="_blank" href="http://www.linksalpha.com/user/networks">LinksAlpha.com</a>, grab API Key for each enabled Network and enter it below. Once setup, your Blog posts content appears on the social networks as soon as you hit the Publish button.</div>';
       
    83 	$html .= '<div style="padding:0px 0px 20px 0px;"><a href="http://www.linksalpha.com/user/networks" style="font-weight:bold;" target="_blank">Click Here</a> to get API Keys for your Social Networks. You can <a href="http://help.linksalpha.com/wordpress-plugin-network-publisher" target="_blank">read more about this process at LinksAlpha.com.</a></div>';
       
    84 	$html .= '<div class="rts_header2"><big><strong>Setup</strong></big></div><div style="padding-left:10px;">';
       
    85 	$html .= '<div style="text-align:left;">';
       
    86 	$html .= '<form action="" method="post" id="networkpubadd" name="networkpubadd" style="width:90%;">';
       
    87 	$html .= '<fieldset class="rts_fieldset">';
       
    88 	$html .= '<legend>Additional API Key</legend>';
       
    89 	
       
    90 	$curr_field = 'api_key';
       
    91 	$field_name = sprintf('%s_%s', WIDGET_PREFIX, $curr_field);
       
    92 	$html .= '<div><label for="'.$field_name.'">'.$networkpub_settings[$curr_field]['label'].'&nbsp;</label></div>';
       
    93 	$html .= '<div style="padding-bottom:10px"><input style="width:400px;" class="widefat" id="'.$field_name.'" name="'.$field_name.'" type="text" /></div>';
       
    94 	
       
    95 	$html .= '</fieldset>';
       
    96 	$html .= '<div style="padding-top:20px;"><input type="submit" name="submit" value="Add API Key" /></div>';
       
    97 	$html .= '<input type="hidden" value="'.WP_PLUGIN_URL.'" id="networkpub_plugin_url" /></form></div></td></tr><tr><td style="padding:0px 0px 0px 0px;vertical-align:top;">';
       
    98 	$html .= '<div class="rts_header2"><big><strong>Currently Publishing</strong></big></div>';
       
    99 	$html .= '<div style="padding-left:10px;">'.networkpub_load().'</div>';
       
   100 	$html .= '</div></td></tr></table>';
       
   101 	echo $html;
       
   102 }
       
   103 
       
   104 function networkpub_add($api_key) {
       
   105 	if (!$api_key) {
       
   106 		return FALSE;
       
   107 	}
       
   108 	$url = get_option('siteurl');
       
   109 	$desc = get_bloginfo('description');
       
   110 	if (!$url) {
       
   111 		return FALSE;
       
   112 	}
       
   113 	$link = 'http://www.linksalpha.com/a/networkpubadd?url='.urlencode($url).'&key='.$api_key.'&desc='.urlencode($desc);
       
   114 	require_once(ABSPATH.WPINC.'/class-snoopy.php');
       
   115 	$snoop = new Snoopy;
       
   116 	$snoop->agent = WIDGET_NAME.' - '.get_option('siteurl');
       
   117 	$response = '';
       
   118 	if($snoop->fetchtext($link)){
       
   119 		if (strpos($snoop->response_code, '200')) {
       
   120 			$response_json = $snoop->results;
       
   121 			$response = networkpub_json_decode($response_json);
       
   122 		}
       
   123 	}
       
   124 	if (!$response) {
       
   125 		return FALSE;
       
   126 	}
       
   127 	if ($response->errorCode > 0) {
       
   128 		return FALSE;
       
   129 	}
       
   130 	$options = get_option(WIDGET_NAME_INTERNAL);
       
   131 	if(empty($options['api_key'])) {
       
   132 		$options['api_key'] = $api_key;	
       
   133 	} else {
       
   134 		$options_array = explode(',', $options['api_key']);
       
   135 		if(!in_array($api_key, $options_array)) {
       
   136 			$options['api_key'] = $options['api_key'].','.$api_key;	
       
   137 		}
       
   138 	}
       
   139 	$options['id'] = $response->results->id; 
       
   140 	update_option(WIDGET_NAME_INTERNAL, $options);
       
   141 	return TRUE;	
       
   142 }
       
   143 
       
   144 function networkpub_load() {
       
   145 	$options = get_option(WIDGET_NAME_INTERNAL);
       
   146 	if (empty($options['api_key'])) {
       
   147 		$html = '<div>You have not added any API Key</div>';
       
   148 		return $html;
       
   149 	}
       
   150 	$link = 'http://www.linksalpha.com/a/networkpubget?key='.$options['api_key'];
       
   151 	require_once(ABSPATH.WPINC.'/class-snoopy.php');
       
   152 	$snoop = new Snoopy;
       
   153 	$snoop->agent = WIDGET_NAME.' - '.get_option('siteurl');
       
   154 	$response = '';
       
   155 	if($snoop->fetchtext($link)){
       
   156 		if (strpos($snoop->response_code, '200')) {
       
   157 			$response_json = $snoop->results;
       
   158 			$response = networkpub_json_decode($response_json);
       
   159 		}
       
   160 	}
       
   161 	if (!$response) {
       
   162 		$html = '<div>Error occured while trying to load the API Keys. Please try again later.</div>';
       
   163 		return $html;
       
   164 	}
       
   165 	if($response->errorCode > 0) {
       
   166 		$html = '<div>Error occured while trying to load the API Keys. Please try again later.</div>';
       
   167 		return $html;
       
   168 	}
       
   169 	$html = '<div style="padding-bottom:10px;">You are currently Publishing your Blog to '.count($response->results).' Social Networks</div>';
       
   170 	$html .= '<table class="networkpub_added"><tr><th>API Key</th><th>Network</th><th>Option</th></tr>';
       
   171 	foreach($response->results as $row) {
       
   172 		$html .= '<tr id="r_key_'.$row->api_key.'">';
       
   173 		switch($row->type) {
       
   174 			case 'twitter':
       
   175 				$html  .= '<td>'.$row->api_key.'</td><td>Twitter Account - <a target="_blank" href="http://twitter.com/'.$row->short_name.'">'.$row->name.'</a></td>';
       
   176 				break;
       
   177 			case 'fb_page':
       
   178 				$html  .= '<td>'.$row->api_key.'</td><td>Facebook Page - <a target="_blank" href="'.$row->page_url.'">'.$row->name.'</a></td>';
       
   179 				break;
       
   180 			case 'fb_wall':
       
   181 				$html  .= '<td>'.$row->api_key.'</td><td><a target="_blank" href="http://www.facebook.com/profile.php?id='.$row->id.'">'.$row->name.'</a></td>';
       
   182 				break;	
       
   183 			case 'myspace':
       
   184 				$html  .= '<td>'.$row->api_key.'</td><td>MySpace Account - <a target="_blank" href="'.$row->page_url.'">'.$row->name.'</a></td>';
       
   185 				break;	
       
   186 			case 'linkedin':
       
   187 				$html  .= '<td>'.$row->api_key.'</td><td>LinkedIn Account - <a target="_blank" href="'.$row->page_url.'">'.$row->name.'</a></td>';
       
   188 				break;
       
   189 			case 'yammer':
       
   190 				$html  .= '<td>'.$row->api_key.'</td><td>Yammer Account - <a target="_blank" href="'.$row->page_url.'">'.$row->name.'</a></td>';
       
   191 				break;	
       
   192 		}
       
   193 		$html .= '<td><a href="#" id="key_'.$row->api_key.'" class="networkpubre">Remove</a></td></tr>';
       
   194 	}
       
   195 	$html .= '</table>';
       
   196 	return $html;
       
   197 }
       
   198 
       
   199 function networkpub_remove() {
       
   200 	$options = get_option(WIDGET_NAME_INTERNAL);
       
   201 	if (!empty($_POST['key'])) {
       
   202 		$key_full = $_POST['key'];
       
   203 		$key_only = substr($key_full, 4);
       
   204 		$api_key = $options['api_key'];
       
   205 		$api_key_array = explode(',', $api_key);
       
   206 		$loc = array_search($key_only, $api_key_array);
       
   207 		if($loc !== FALSE) {
       
   208 			unset($api_key_array[$loc]);
       
   209 		}
       
   210 		$api_key = implode(",", $api_key_array);
       
   211 		$options['api_key'] = $api_key;
       
   212 		update_option(WIDGET_NAME_INTERNAL, $options);
       
   213 		$link = 'http://www.linksalpha.com/a/networkpubremove?id='.$options['id'].'&key='.$key_only;
       
   214 		require_once(ABSPATH.WPINC.'/class-snoopy.php');
       
   215 		$snoop = new Snoopy;
       
   216 		$snoop->agent = WIDGET_NAME.' - '.get_option('siteurl');
       
   217 		$response = '';
       
   218 		$snoop->fetchtext($link);	
       
   219 		echo $key_full;
       
   220 		return;
       
   221 	}
       
   222 }
       
   223 
       
   224 function networkpub_json_decode($str) {
       
   225 	if (function_exists("json_decode")) {
       
   226 	    return json_decode($str);
       
   227 	} else {
       
   228 		if (!class_exists('Services_JSON')) {
       
   229 			require_once("JSON.php");
       
   230 		}
       
   231 	    $json = new Services_JSON();
       
   232 	    return $json->decode($str);
       
   233 	}
       
   234 }
       
   235 
       
   236 ?>