Instantly share code, notes, and snippets.
Created
June 5, 2014 11:36
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save imvision/c5c7b89d6aaaf71fed9b to your computer and use it in GitHub Desktop.
Url preview and sharing plugin for wordpress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: URL Preview | |
Plugin URI: | |
Description: This plugin will create facebook like preview of urls. | |
Version: 1.1.1 | |
Author: Ali Roshan | |
Author Email: [email protected] | |
License: | |
Copyright 2011 Ali Roshan ([email protected]) | |
This program is free software; you can redistribute it and/or modify | |
it under the terms of the GNU General Public License, version 2, as | |
published by the Free Software Foundation. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
*/ | |
class URLPreview { | |
/*--------------------------------------------* | |
* Constants | |
*--------------------------------------------*/ | |
const name = 'URL Preview'; | |
const slug = 'url_preview'; | |
const api_ep = 'http://api.embed.ly/1/oembed?url='; | |
/** | |
* Constructor | |
*/ | |
function __construct() { | |
//Hook up to the init action | |
add_action( 'init', array( &$this, 'init_url_preview' ) ); | |
} | |
/** | |
* Runs when the plugin is activated | |
*/ | |
function install_url_preview() { | |
// do not generate any output here | |
} | |
/** | |
* Runs when the plugin is initialized | |
*/ | |
function init_url_preview() { | |
// Load JavaScript and stylesheets | |
$this->register_scripts_and_styles(); | |
// Register the shortcode [url_view] | |
add_shortcode( 'url_view', array( &$this, 'render_shortcode' ) ); | |
if ( is_admin() ) { | |
//this will run when in the WordPress admin | |
} else { | |
//this will run when on the frontend | |
} | |
add_action( 'wp_ajax_loadview', array( &$this, 'createPreview' ) ); | |
add_action( 'wp_ajax_saveurl', array( &$this, 'saveUrl' ) ); | |
} | |
function render_shortcode($atts) { | |
// Extract the attributes | |
extract(shortcode_atts(array( | |
'url' => '' //foo is a default value | |
), $atts)); | |
// you can now access the attribute values using $attr1 and $attr2 | |
ob_start(); | |
?> | |
<div class="share_form row"> | |
<form method="post" action="<?php echo admin_url() . 'admin-ajax.php' ;?>"> | |
<label>URL</label> | |
<input type="text" name="url" id="share_url" placeholder="http://"> | |
<div class="row"> | |
<button id="load_view" class="btn btn-primary">Preview</button> | |
<img src="<?php echo plugins_url( 'img/ajax-loader.gif' , __FILE__ );?>" id="url_aloader" style="display: none;"> | |
</div> | |
</form> | |
<div class="url-preview row"></div> | |
</div> | |
<script type="text/javascript"> | |
(function($) { | |
$(document).ready(function() { | |
$("#load_view").click(function() { | |
if($('#share_url').val()=="") { | |
return false; | |
} | |
var ajaxurl = $(this).closest('form').attr('action'); | |
var sharurl = encodeURIComponent($('#share_url').val()); | |
$.ajax({ | |
type: "post",url: ajaxurl,data: { action: 'loadview', url: sharurl}, | |
beforeSend: function() { | |
$('.url-preview').html(""); | |
$('#load_view').hide(); | |
$('#url_aloader').show(); | |
}, | |
success: function(html) { | |
$(".url-preview").html(html); | |
$('#load_view').show(); | |
$('#url_aloader').hide(); | |
} | |
}); //close $.ajax( | |
return false; | |
}); | |
var lang = "en"; | |
$('.share_form').on('click', '#submit_url', function() { | |
if(document.location.href.indexOf("lang=it")!==-1) { | |
var lang = "it"; | |
} | |
var link_data = { | |
link_title : $('#link_title').val(), | |
link_text : $('#link_text').val(), | |
link_image : $('#link_image').val(), | |
link_url : $('#link_url').val(), | |
link_tags : $('#link_tags').val(), | |
} | |
$.ajax({ | |
type: "post", url: ajaxurl, data: { action: 'saveurl', link_data: link_data, lang: lang}, | |
beforeSend: function() { | |
$('#submit_url').hide(); | |
$('#url_save').show(); | |
}, | |
success: function(html) { | |
$(".url-preview").html(html); | |
$('#share_url').val(''); | |
} | |
}); //close $.ajax( | |
return false; | |
}); | |
$('.share_form').on("click", ".tags-view span a", function() { | |
var current_tags = $.trim($("#link_tags").val()); | |
if(current_tags.slice(-1)!=",") { | |
if(current_tags!="") { | |
current_tags += ","; | |
} | |
} | |
current_tags += " " + $(this).text(); | |
$('#link_tags').val(current_tags); | |
$(this).hide(); | |
}); | |
}); | |
})(jQuery); | |
</script> | |
<?php | |
$html_data = ob_get_clean(); | |
return $html_data; | |
} | |
function createPreview() { | |
$url = str_replace(" ", "%20", $_POST['url']); | |
$final_url = self::api_ep . $url; | |
$url_data = $this->fetch($final_url); | |
$this->view_share($url_data); | |
die(); | |
} | |
function saveUrl() { | |
$current_user = wp_get_current_user(); | |
$user_ID = $current_user->ID; | |
$link_data = $_POST['link_data']; | |
$postData = array(); | |
$postData['post_title'] = $link_data['link_title']; | |
$postData['post_content'] = $link_data['link_text']; | |
$postData['post_status'] = 'pending'; | |
$postData['post_author'] = $user_ID; | |
$postData['post_type'] = "resource"; | |
$postData['post_excerpt'] = $link_data['link_text']; | |
$roles = $current_user->roles; | |
$allow_publish = array("author", "contributor", "editor", "administrator"); | |
foreach($roles as $role) { | |
if(in_array($role, $allow_publish)) { | |
$postData['post_status'] = 'publish'; | |
} | |
} | |
$newPost = wp_insert_post($postData); | |
update_post_meta($newPost, "res-img-url", $link_data['link_image']); | |
update_post_meta($newPost, "wpcf-url", $link_data['link_url']); | |
wp_set_post_tags($newPost, $link_data['link_tags']); | |
if(isset($_REQUEST['lang']) && $_REQUEST['lang']=="it") { | |
$category = array(); | |
$cat_id = get_cat_ID( "italian" ); | |
array_push($category, $cat_id); | |
wp_set_post_categories($newPost, $category); | |
} | |
// TODO: Save as "resource" post_type | |
echo "<p class=\"alert alert-success\">Your link has been submitted. This will appear in Links section under Learning & Resources once it is reviewed by the admin.</p>"; | |
die(); | |
} | |
function fetch($url="") { | |
$url_data_json = file_get_contents($url); | |
$url_data = json_decode($url_data_json); | |
return $url_data; | |
} | |
function view_share($url_data) { | |
$url = $url_data->url; | |
?> | |
<form method="post" action="<?php echo admin_url() . 'admin-ajax.php' ;?>"> | |
<div class="row"> | |
<div class="img-preview"> | |
<img width="200px" height="" src="<?php echo $url_data->thumbnail_url;?>"> | |
</div> | |
<div class="text-preview"> | |
<input type="text" name="link_title" id="link_title" value="<?php echo $url_data->title;?>"> | |
<textarea name="link_text" id="link_text" rows="8"><?php echo $url_data->description;?></textarea> | |
<p><a href="<?php echo $url;?>"><?php echo $url;?></a></p> | |
</div> | |
</div> | |
<div class="clearfix"></div> | |
<input type="hidden" name="link_image" id="link_image" value="<?php echo $url_data->thumbnail_url;?>"> | |
<input type="hidden" name="link_url" id="link_url" value="<?php echo $url;?>"> | |
<div class="row span6 tags-view"> | |
<div class=""> | |
<strong>Tags:</strong> | |
<input type="text" name="link_tags" id="link_tags"> | |
</div> | |
<div class="clearfix"></div> | |
<div class="tags-input"> | |
<strong>Most used:</strong> | |
<?php echo $this->topTags();?> | |
</div> | |
</div> | |
<div class="row pull-right btn-share"> | |
<button id="submit_url" class="btn btn-primary">Share</button> | |
<img src="<?php echo plugins_url( 'img/ajax-loader.gif' , __FILE__ );?>" id="url_save" style="display: none;"> | |
</div> | |
</form> | |
<?php | |
} | |
function topTags() { | |
$tags = get_tags(); | |
if (empty($tags)) | |
return; | |
$counts = $tag_links = array(); | |
foreach ( (array) $tags as $tag ) { | |
$counts[$tag->name] = $tag->count; | |
$tag_links[$tag->name] = get_tag_link( $tag->term_id ); | |
} | |
asort($counts); | |
$counts = array_reverse( $counts, true ); | |
$i = 0; | |
foreach ( $counts as $tag => $count ) { | |
$i++; | |
$tag_link = clean_url($tag_links[$tag]); | |
$tag = str_replace(' ', ' ', wp_specialchars( $tag )); | |
if($i < 5){ | |
print "<span><a href=\"javascript:void(0);\">$tag</a></span>"; | |
} | |
} | |
} | |
function get_domain($url) { | |
$pieces = parse_url($url); | |
$domain = isset($pieces['host']) ? $pieces['host'] : ''; | |
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) { | |
return $regs['domain']; | |
} | |
return false; | |
} | |
/** | |
* Registers and enqueues stylesheets for the administration panel and the | |
* public facing site. | |
*/ | |
private function register_scripts_and_styles() { | |
if ( is_admin() ) { | |
// $this->load_file( self::slug . '-admin-script', '/js/admin.js', true ); | |
// $this->load_file( self::slug . '-admin-style', '/css/admin.css' ); | |
} else { | |
// $this->load_file( self::slug . '-script', '/js/widget.js', true ); | |
$this->load_file( self::slug . '-style', '/css/default.css' ); | |
} // end if/else | |
} // end register_scripts_and_styles | |
/** | |
* Helper function for registering and enqueueing scripts and styles. | |
* | |
* @name The ID to register with WordPress | |
* @file_path The path to the actual file | |
* @is_script Optional argument for if the incoming file_path is a JavaScript source file. | |
*/ | |
private function load_file( $name, $file_path, $is_script = false ) { | |
$url = plugins_url($file_path, __FILE__); | |
$file = plugin_dir_path(__FILE__) . $file_path; | |
if( file_exists( $file ) ) { | |
if( $is_script ) { | |
wp_register_script( $name, $url, array('jquery') ); //depends on jquery | |
wp_enqueue_script( $name ); | |
} else { | |
wp_register_style( $name, $url ); | |
wp_enqueue_style( $name ); | |
} // end if | |
} // end if | |
} // end load_file | |
} // end class | |
new URLPreview(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment