. */ //authority_linker register_activation_hook( __FILE__,'wp_authority_linker_register' ); add_action('admin_menu','wp_authority_linker_menu'); add_filter('the_content','wp_authority_linker_inject_links'); add_action('add_meta_boxes','wp_authority_linker_register_meta_box'); add_action('save_post','wp_authority_linker_meta_data',10,2); function wp_authority_linker_register_meta_box() { add_meta_box('wp_authority_linker_tick_meta_box','Authority Linker Options','wp_authority_linker_tick_meta_box','post','side'); add_meta_box('wp_authority_linker_tick_meta_box','Authority Linker Options','wp_authority_linker_tick_meta_box','page','side'); } function wp_authority_linker_tick_meta_box($post) { // Retrieve is allow to add link status $post_authority_linker_status = esc_html(get_post_meta($post->ID,'post_authority_linker_status',true)); ?>
Allow to add Links />
post_type == 'post' || $post->post_type == 'page') { if(isset($_POST['post_authority_linker_status'])) { $post_authority_linker_status = $_POST['post_authority_linker_status']; } else { $post_authority_linker_status = 0; } // Store data in post meta table if present in post data update_post_meta($post_id,'post_authority_linker_status',$post_authority_linker_status); } } function wp_authority_linker_register() { //create table to store URL and its keywords $sql = 'CREATE TABLE IF NOT EXISTS `wp_authority_linker` ( `link_id` bigint(10) NOT NULL AUTO_INCREMENT, `link` text NOT NULL, `keywords` text NOT NULL, PRIMARY KEY (`link_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;'; //$wpdb->query($sql); require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); //add mix link repacement count defualt value 4 if(get_option('wp_authority_linker_max_links') === false) { add_option('wp_authority_linker_max_links', "4"); }//failure url } function wp_authority_linker_menu() { add_menu_page( 'Authority Linker', 'Authority Linker', 'manage_options', 'wp-authority-linker-settings-menu', 'wp_authority_linker_config_page', '', 55 ); } function do_addlinks_actions($page,$post,$type) { global $wpdb; $_POST = $post; /*echo '
';
	print_r($_POST);
    echo '
';*/ // Save the settings // if(isset($_POST['Save'])) { $err_flg = 0; // Get settings and do some validation $table_name = "wp_authority_linker"; if(trim($type) == 'add') { if($post['linkinfo']['al_link_url'] == '') { queue_message( __('Please enter Link', 'wp_authority_linker' ), 'error' ); $err_flg = 1; } if($post['linkinfo']['al_keywords'] == '') { queue_message( __('Please enter Keywords', 'wp_authority_linker' ), 'error' ); $err_flg = 1; } ## URL check if($post['linkinfo']['al_link_url'] != '') { if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $post['linkinfo']['al_link_url'])) { queue_message( __( 'Invalid URL', 'wp_authority_linker' ), 'error' ); $err_flg = 1; } } ## ## Check keyword is exists or not /*if($post['linkinfo']['al_keywords'] != '') { $keywords_exists = ''; $arr_keywords = explode(",",$post['linkinfo']['al_keywords']); //echo '
';
				//print_r($arr_keywords);
    			//echo '
'; die; foreach ($arr_keywords as $key => $value) { $sql = "SELECT * FROM wp_authority_linker WHERE keywords LIKE '%".$value."%'"; $results = $wpdb->get_results($sql); if($results) { $keywords_exists .= $value.'
'; $err_flg = 1; } } if($keywords_exists != '') { queue_message( __('Following Keywords are already exists.
'.$keywords_exists, 'wp_authority_linker' ), 'error' ); } } */ ## if($err_flg == 0) { //die; // Update database option and get response $wpdb->insert($table_name, array( 'link'=>$post['linkinfo']['al_link_url'], 'keywords'=>$post['linkinfo']['al_keywords'] )); $getLinkID = $wpdb->insert_id; if($getLinkID > 0) { // Show update message queue_message( __( 'New Link has been saved successfully.', 'wp_authority_linker' ), 'updated' ); $_POST['linkinfo']['al_link_url'] = ''; $_POST['linkinfo']['al_keywords'] = ''; } else { queue_message( __( 'Error!Please Try Again', 'wp_authority_linker' ), 'error' ); } } } elseif(trim($type) == 'edit') { if($post['linkinfo']['al_link_url'] == '') { queue_message( __('Please enter Link', 'wp_authority_linker' ), 'error' ); $err_flg = 1; } if($post['linkinfo']['al_keywords'] == '') { queue_message( __('Please enter Keywords', 'wp_authority_linker' ), 'error' ); $err_flg = 1; } ## URL check if($post['linkinfo']['al_link_url'] != '') { if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $post['linkinfo']['al_link_url'])) { queue_message( __( 'Invalid URL', 'wp_authority_linker' ), 'error' ); $err_flg = 1; } } ## /* ## Check keyword is exists or not if($post['linkinfo']['al_keywords'] != '') { $keywords_exists = ''; $arr_keywords = explode(",",$post['linkinfo']['al_keywords']); //echo '
';
				//print_r($arr_keywords);
    			//echo '
'; die; foreach ($arr_keywords as $key => $value) { $sql = "SELECT * FROM wp_authority_linker WHERE link_id != ".$_POST['link_id']." AND keywords LIKE '%".$value."%'"; $results = $wpdb->get_results($sql); if($results) { $keywords_exists .= $value.'
'; $err_flg = 1; } } if($keywords_exists != '') { queue_message( __('Following Keywords are already exists.
'.$keywords_exists, 'wp_authority_linker' ), 'error' ); } } */ ## if($err_flg == 0) { // Update database option and get response $wpdb->update( $table_name, array( 'link'=>$post['linkinfo']['al_link_url'], 'keywords'=>$post['linkinfo']['al_keywords'] ), array( 'link_id' => intval(trim($_POST['link_id'])) ) ); } // Show update message return queue_message( __( 'Link has been updated successfully.', 'wp_authority_linker' ), 'updated' ); } } } function wp_authority_linker_inject_links($content) { $post_id = get_the_ID(); $post_authority_linker_status = get_post_meta($post_id,'post_authority_linker_status',true); if($post_authority_linker_status == 1) { $wp_authority_linker_max_links = get_option('wp_authority_linker_max_links'); if($wp_authority_linker_max_links < 1 || $wp_authority_linker_max_links == '') { return $content; } ## fetch all keywords - Start global $wpdb; $sql = "SELECT * FROM wp_authority_linker order by rand()"; $results = $wpdb->get_results($sql); $arr_link = array(); $patterns_scram = array(); $replacements_scram = array(); foreach ($results as $result) { $keywords = $result->keywords; $link = $result->link; if(!in_array($link,$arr_link)) { $arr_keywords = explode(",",$keywords); foreach ($arr_keywords as $key => $value) { $keyword = ltrim($value); $keyword = rtrim($keyword); $regex = '/\b'.$keyword.'\b/i'; $srch_cnt = preg_match_all($regex, $content,$match); if($srch_cnt > 0) { //Scramble replace $replacement = base64_encode("$keyword"); //$content = preg_replace_callback($regex, $replacement, $content ,1); $callback = new wp_authority_linker_callback($replacement,$srch_cnt); $content = preg_replace_callback($regex, array($callback, 'callback'), $content); $patterns_scram[] = $replacement; $replacements_scram[] = "$keyword"; $arr_link[] = $link; break; }//if kyword found }//foreach keyword if(count($arr_link) >= $wp_authority_linker_max_links) { break; }//if link count reaches to maximum then stop replacement }//if link is already added }//foreach each links if(count($patterns_scram) > 0) { /*echo $content; echo '
';
			print_r($patterns_scram);
			print_r($replacements_scram);
			echo '
'; */ $content = str_replace($patterns_scram, $replacements_scram, $content); }//if scrambled replacement is poresent then bring to origianl format }//if link addition applicable for post/page return $content; } function do_getLink_info( $id ) { global $wpdb; $table_name = "wp_authority_linker"; $sql = "SELECT * FROM $table_name WHERE `link_id`='".$id."'"; return $results = $wpdb->get_row($sql); } function do_dumpLink_actions() { /*echo '
';
	print_r($_FILES);
	echo '
';*/ global $wpdb; if ($_FILES[bulk_links_file][size] > 0) { //get the csv file $file = $_FILES[bulk_links_file][tmp_name]; $handle = fopen($file,"r"); $table_name = "wp_authority_linker"; //loop through the csv file and insert into database $import_count = 0; $total_count = 0; do { if ($data[0]) { $err_flg = 0; $post['linkinfo']['al_link_url'] = $data[0]; $post['linkinfo']['al_keywords'] = $data[1]; // Get settings and do some validation if($post['linkinfo']['al_link_url'] == '') { //queue_message( __('Please enter Link', 'wp_authority_linker' ), 'error' ); $err_flg = 1; } if($post['linkinfo']['al_keywords'] == '') { //queue_message( __('Please enter Keywords', 'wp_authority_linker' ), 'error' ); $err_flg = 1; } ## URL check if($post['linkinfo']['al_link_url'] != '') { if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $post['linkinfo']['al_link_url'])) { //queue_message( __( 'Invalid URL', 'wp_authority_linker' ), 'error' ); $err_flg = 1; } } ## if($err_flg == 0) { //die; // Update database option and get response $wpdb->insert($table_name, array( 'link'=>$post['linkinfo']['al_link_url'], 'keywords'=>$post['linkinfo']['al_keywords'] )); $getLinkID = $wpdb->insert_id; if($getLinkID > 0) { $import_count++; } else { queue_message( __( 'Error!Please Try Again', 'wp_authority_linker' ), 'error' ); } } $total_count++; } } while ($data = fgetcsv($handle,1000,",",'"')); if($total_count > 0) { return queue_message( __( "$import_count out of $total_count links has been added successfully. ", 'wp_authority_linker' ), 'updated' ); } } } function do_updMaxLink_actions() { if($_POST['wp_authority_linker_max_links'] != '') { if(preg_match('/^\d+$/',$_POST['wp_authority_linker_max_links'])) { $wp_authority_linker_max_links = sanitize_text_field($_POST[wp_authority_linker_max_links]); update_option('wp_authority_linker_max_links', $wp_authority_linker_max_links); return queue_message( __( "Max link count has been updated successfully. ", 'wp_authority_linker' ), 'updated' ); } else { return queue_message( __( "Max link count must be positive integer ", 'wp_authority_linker' ), 'error' ); } } else { return queue_message( __( "Max link count can not be empty. ", 'wp_authority_linker' ), 'error' ); } } function wp_authority_linker_config_page() { //links_tab ?>

do_addvendorSupplier_actions( $_GET['page'],$_POST,'add' ); $currentTab = 'addnewlinks_tab'; } }//save link if ( isset( $_POST['Save'] ) ) { if($_POST['link-edit'] == 'Y' && trim($_GET['tab']) == 'editlinks_tab') { do_addlinks_actions($_GET['page'],$_POST,'edit'); $currentTab = 'editlinks_tab'; } }//update link if ( isset( $_POST['Save'] ) ) { if($_POST['upload-link-submit'] == 'Y' && trim($_GET['tab']) == 'importlinks_tab') { do_dumpLink_actions(); $currentTab = 'importlinks_tab'; } }//upload links via csv if ( isset( $_POST['Save'] ) ) { if($_POST['max-link-update'] == 'Y' && trim($_GET['tab']) == 'links_setting') { do_updMaxLink_actions(); $currentTab = 'links_setting'; } }//upload links via csv // if ( isset( $_REQUEST['action'] ) ) { if($_REQUEST['action'] == 'edit' && trim($_REQUEST['tab']) == 'editlinks_tab') { $currentTab = 'editlinks_tab'; $LinkInfo = do_getLink_info(intval(trim($_REQUEST['link_id']))); } if($_POST['action'] == 'csvExport' && $currentTab == 'links_tab') { //die("csvExport"); generate_csv( $_GET['page'],$_POST); } } $tabNames = array( 'links_tab' => 'Links', 'addnewlinks_tab' => 'Add New Link' ,'importlinks_tab' => 'Import Links','links_setting' => 'Setting','editlinks_tab' => 'Edit Link',); $tabs = wp_authority_linker_admin_tabs( $currentTab , $tabNames); echo $tabs; $pagenow = $_GET['page'] ? trim($_GET['page']) : ''; $action = ''; ?>
"; displayLinkList();; if(trim($_REQUEST['action']) == 'delete') do_action( 'woocommerce_mercadolibre_admin_messages' ); ?> '; ?>   e.g. http://www.example.com  Use Comma after each keyword (ABC,PQR,etc).

'; ?> "http://www.facebook.com","Facebook, Social Network, Mark Zuckerberg"
"http://www.yahoo.com","yahoo, email service,free mail"

'; ?>

'; ?>   e.g. http://www.example.com  Use Comma after each keyword (ABC,PQR,etc).


'; $output .= ''; return $output; } function displayLinkList() { global $wpdb; //echo "
"; print_r($_REQUEST); echo "
";die;
	  $table_name  = $wpdb->prefix . "authority_linker";
	  if(isset($_REQUEST['action'])) {
		 if($_REQUEST['action'] != '')
		 {
		   if(trim($_REQUEST['action']) == 'delete')
		   {
			  if(!is_array($_REQUEST['link_id'])) {
				  if(intval(trim($_REQUEST['link_id'])) > 0)
				  {
					//die("is first");
					$delsql  = "DELETE FROM $table_name WHERE $table_name.`link_id` = '".intval(trim($_REQUEST['link_id']))."'";
					$wpdb->query($delsql);
				  }	
			  }
			  else {
				  if(count($_REQUEST['link_id']) > 0)
				  {
						//die("is 2ND");
					$delsql  = "DELETE FROM $table_name WHERE $table_name.`link_id` IN (".implode(',',$_REQUEST['link_id']).")";
					$wpdb->query($delsql);
				  }
			  }
			  // Show update message 
			  queue_message( __( 'Link has been deleted successfully.', 'wp_authority_linker' ), 'updated' );
		   }
		 }
	  }
	  
	  $sql 		   = "SELECT * FROM $table_name ";
	  if(isset($_REQUEST['s'])) {
		 if(trim($_REQUEST['s']) != '') {
			$sql  .= "WHERE `link` LIKE '%".trim($_REQUEST['s'])."%' OR `keywords` LIKE '%".trim($_REQUEST['s'])."%' ";
		 }
	  }
	  $sql 		  .= "ORDER BY `link_id` DESC";
	  $results     = $wpdb->get_results($sql);
	  $recordsArr  = array();
	  if($wpdb->num_rows)
	  {
		foreach ($results as $result)
		{
			$recordsArr[] = array('link_id' => $result->link_id,'link' => $result->link, 'keywords' => $result->keywords);
		}
	  }    
	  $filename = dirname( __FILE__ ) . '/link_list_table.php';
	  require_once( $filename );
	  global $linkListTable;
	  $option = 'per_page';
	  $args = array(
			 'label' => 'Links',
			 'default' => 10,
			 'option' => 'links_per_page'
			 );
	  add_screen_option( $option, $args );
	  $linkListTable = new Link_List_Table();
	  $linkListTable->link_data = $recordsArr;
	  $linkListTable->prepare_items();
	  $linkListTable->search_box( 'search', 'search_id' );
	  $linkListTable->display();
}
/**
* Script to produce the CSV
* 
*/	
function generate_csv( $page,$post ) 
{ 
				 $sitename = sanitize_key( get_bloginfo( 'name' ) );
				 if ( ! empty( $sitename ) )
					$sitename .= '.';
				 $filename = $sitename . 'links.' . date( 'Y-m-d-H-i-s' ) . '.csv';
                 ob_end_clean();
				 header( 'Content-Description: File Transfer' );
				 header( 'Content-Disposition: attachment; filename=' . $filename );
				 header( 'Content-Type: text/csv; charset=' . get_option( 'blog_charset' ), true );
				 
				 $exclude_data = apply_filters( 'pp_eu_exclude_data', array() );
				 
				 global $wpdb;
				 
				 $fields       = array('ID', 'Links', 'keywords');
				 $dbfields     = array('link_id', 'link', 'keywords');
				 $headers = array();
				 
				 foreach ( $fields as $key => $field ) {
					if ( in_array( $field, $exclude_data ) )
						unset( $fields[$key] );
					else
						$headers[] = '"' . $field . '"';
				 }
			
				 echo implode( ',', $headers ) . "\n";
        	     $table_name     = $wpdb->prefix . "authority_linker";
			     $sql 		     = "SELECT * FROM $table_name ";
				 if(is_array($_REQUEST['link_id'])) {
				    if(count($_REQUEST['link_id']) > 0) {
					    $sql 	.= "WHERE $table_name.`link_id` IN (".implode(',',$_REQUEST['link_id']).") ";
						//$sql 	.= "WHERE $table_name.`link_id` IN (3,9) ";
					}
				 }
				 $sql .= "ORDER BY `link_id` ASC";
			     $results     = $wpdb->get_results($sql);
				 //echo "
"; print_r($results); echo "
"; die;
				 if($wpdb->num_rows)
				 {
				 	
					foreach ($results as $result)
					{
						
					  $data = array();
					  foreach ( $dbfields as $field ) {
							$value = isset( $result->{$field} ) ? $result->{$field} : '';
							$value = is_array( $value ) ? serialize( $value ) : $value;
							$data[] = '"' . str_replace( '"', '""', $value ) . '"';
					  }
					  echo implode( ',', $data ) . "\n"; 
					}
				 }
				 exit;
			 }			
			
function queue_message( $text, $type ) 
{
	$message = "

$text

"; //return $message; add_action( 'wp_authority_linker_admin_messages', create_function( '', 'echo "'. $message .'";' ) ); } class wp_authority_linker_callback { private $replacement; private $srch_cnt; private $match_cnt; private $rand_val; function __construct($replacement,$srch_cnt) { $this->replacement = $replacement; $this->srch_cnt = $srch_cnt; $this->match_cnt = 0; if($this->srch_cnt > 1) { $this->rand_val = rand(1, $this->srch_cnt); } else { $this->rand_val = 1; } } public function callback($matches) { $this->match_cnt++; if($this->match_cnt == $this->rand_val) { return $this->replacement; } else { return $matches[0]; } } } ?>