Skip to content

Instantly share code, notes, and snippets.

@imvision
Created January 22, 2014 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imvision/8558813 to your computer and use it in GitHub Desktop.
Save imvision/8558813 to your computer and use it in GitHub Desktop.
Wordpress plugin - Creates facebook like preview of given url
.url-preview {
border-top: 1px solid #EAEAEA;
font-family: calibri;
}
.share_form {
width: 625px;
}
.share_form input {
width: 500px;
}
.img-preview {
float: left;
width: 200px;
padding-top: 15px;
}
.text-preview {
float: left;
width: 400px;
margin-left: 15px;
}
.tags-view {
padding-top: 20px;
}
.tags-view input{
width: 70%;
margin-left: 10px;
}
.tags-view ul {
list-style: none;
margin-left: 0 !important;
}
.tags-view ul li {
display: inline;
/*margin-left: 0;*/
}
.tags-view span {
margin-left: 10px;
}
<?php
/*
Plugin Name: URL Preview
Plugin URI:
Description: This plugin will create facebook like preview of urls.
Version: 0.1
Author: Ali Roshan
Author Email: aliroshan@live.com
License:
Copyright 2011 Ali Roshan (aliroshan@live.com)
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;
}
$.ajax({
type: "post",url: ajaxurl,data: { action: 'loadview', url: $('#share_url').val()},
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;
});
$('.share_form').on('click', '#submit_url', function() {
$.ajax({
type: "post", url: ajaxurl, data: { action: 'saveurl', link_data: $(this).closest('form').serialize()},
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($("#tags").val());
if(current_tags.slice(-1)!=",") {
if(current_tags!="") {
current_tags += ",";
}
}
current_tags += " " + $(this).text();
$('#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() {
$user_ID = get_current_user_id();
// 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;
?>
<div class="row">
<div class="img-preview">
<img width="200px" height="" src="<?php echo $url_data->thumbnail_url;?>">
</div>
<div class="text-preview">
<h4>
<a href="<?php echo $url;?>"><?php echo $url_data->title;?></a>
</h4>
<p><?php echo $url_data->description;?></p>
<p><a href="<?php echo $url ;?>"><?php echo $this->get_domain($url);?></a></p>
</div>
</div>
<div class="clearfix"></div>
<form method="post" action="<?php echo admin_url() . 'admin-ajax.php' ;?>">
<input type="hidden" name="link_title" value="<?php echo $url_data->title;?>">
<input type="hidden" name="link_text" value="<?php echo $url_data->description;?>">
<input type="hidden" name="link_image" value="<?php echo $url_data->thumbnail_url;?>">
<div class="row span6 tags-view">
<div class="">
<strong>Tags:</strong>
<input type="text" name="tags" id="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">
<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(' ', '&nbsp;', 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