Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
Uploading version 1.0.9, which includes a new filter
Browse files Browse the repository at this point in the history
  • Loading branch information
nelio committed Nov 26, 2014
1 parent ba58019 commit 5c25289
Show file tree
Hide file tree
Showing 5 changed files with 516 additions and 0 deletions.
94 changes: 94 additions & 0 deletions includes/admin/edit-post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

// Creating box
add_action( 'add_meta_boxes', 'nelioefi_add_url_metabox' );
function nelioefi_add_url_metabox() {

$excluded_post_types = array(
'attachment', 'revision', 'nav_menu_item', 'wpcf7_contact_form',
);

foreach ( get_post_types( '', 'names' ) as $post_type ) {
if ( in_array( $post_type, $excluded_post_types ) )
continue;
add_meta_box(
'nelioefi_url_metabox',
'External Featured Image',
'nelioefi_url_metabox',
$post_type,
'side',
'default'
);
}

}

function nelioefi_url_metabox( $post ) {
$nelioefi_url = get_post_meta( $post->ID, _nelioefi_url(), true );
$has_img = strlen( $nelioefi_url ) > 0;

if ( $has_img ) { ?>
<div id="nelioefi_preview_block"><?php
} else { ?>
<div id="nelioefi_preview_block" style="display:none;"><?php
} ?>
<div id="nelioefi_image_wrapper" style="<?php
echo (
'width:100%;' .
'max-width:300px;' .
'height:200px;' .
'margin-top:10px;' .
'background:url(' . $nelioefi_url . ') no-repeat center center; ' .
'-webkit-background-size:cover;' .
'-moz-background-size:cover;' .
'-o-background-size:cover;' .
'background-size:cover;' );
?>">
</div>

<?php
if ( strlen( $nelioefi_url ) > 0 ) { ?>
<a id="nelioefi_remove_button" href="#" onClick="javascript:nelioefiRemoveFeaturedImage();">Remove featured image</a>
<script>
function nelioefiRemoveFeaturedImage() {
jQuery("#nelioefi_preview_block").hide();
jQuery("#nelioefi_image_wrapper").hide();
jQuery("#nelioefi_remove_button").hide();
jQuery("#nelioefi_url").val('');
jQuery("#nelioefi_controls").show();
}
</script>
<?php
} ?>
</div><?php

if ( $has_img ) { ?>
<div id="nelioefi_controls" style="display:none;"><?php
} else { ?>
<div id="nelioefi_controls"><?php
} ?>
<input type="text" placeholder="Image URL" style="width:100%;margin-top:10px;"
id="nelioefi_url" name="nelioefi_url"
value="<?php echo esc_attr( $nelioefi_url ); ?>" />
<div style="text-align:right;margin-top:10px;">
<a class="button" id="ext_feat_img-preview" onClick="javascript:nelioefiPreview();">Preview</a>
<script>
function nelioefiPreview() {
jQuery("#nelioefi_preview_block").show();
jQuery("#nelioefi_image_wrapper").css('background-image', "url('" + jQuery("#nelioefi_url").val() + "')" );
jQuery("#nelioefi_image_wrapper").show();
}
</script>
</div>
</div>
<?php
}

add_action( 'save_post', 'nelioefi_save_url' );
function nelioefi_save_url( $post_ID ) {
if ( isset( $_POST['nelioefi_url'] ) )
update_post_meta( $post_ID, _nelioefi_url(), strip_tags( $_POST['nelioefi_url'] ) );
}



77 changes: 77 additions & 0 deletions includes/admin/nelioab-campaign.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

if ( !function_exists( 'nelioab_campaign_notice' ) ) {

add_action( 'wp_ajax_nelioab_campaign_dismiss_notice',
'nelioab_campaign_dismiss_notice' );
function nelioab_campaign_dismiss_notice() {
update_option( 'nelioab_campaign_last_dismiss', time() );
die();
}

$INTERVAL = 2592000; // 1 MONTH in seconds
if ( get_option( 'nelioab_campaign_last_dismiss', 0 ) + $INTERVAL < time() )
add_action( 'admin_notices', 'nelioab_campaign_notice' );
function nelioab_campaign_notice() {
$nelioab_plugin = WP_PLUGIN_DIR . '/nelio-ab-testing/main.php';
if ( file_exists( $nelioab_plugin ) )
return;

$message = '';
$messages = array();
$mailing = 'nelioefi';

$messages[0] = '<strong>Nelio A/B Testing</strong> will help you ' .
'deliver to every reader a <a href="http://wp-abtesting.com/' .
'nelio-ab-testing-publishers/?mailing=%1$s">more engaging and ' .
'relevant reading experience</a>.';

$messages[1] = 'Do you want to get more money out of your ' .
'site? <strong>Nelio A/B Testing</strong> is a <a href="' .
'http://wp-abtesting.com/nelio-ab-testing-business/' .
'?mailing=%1$s">native conversion optimization service for ' .
'WordPress</a> that will help you improve your site.';

$messages[2] = 'Do you want to improve your headlines and get ' .
'more readers? <strong>Nelio A/B Testing</strong> will help you ' .
'decide which headlines are more appealing!';

$messages[3] = 'If you don\'t know which is the best featured image ' .
'for a post, use <strong>Nelio A/B Testing</strong>. It\'ll tell you ' .
'which one is more appealing to your users!';

$messages[4] = 'Have you ever wondered what your visitors do in your ' .
'site? Subscribe to <strong>Nelio A/B Testing</strong> for free and ' .
'get insightful <a href="http://wp-abtesting.com/heatmaps-teach-us/' .
'?mailing=%1$s">Heatmaps and Clickmaps</a>.';

$message = $messages[mt_rand( 0, count( $messages )-1 )];
if ( wp_count_posts( 'post' )->publish > 5 * wp_count_posts( 'page' )->publish )
if ( mt_rand( 0, 100 ) < 30 )
$message = $messages[0];

$message .= ' <strong>Check our <a href="http://wp-abtesting.com/' .
'subscription-plans/?mailing=%1$s">subscription plans</a> and ' .
'try it for free</strong>.';

$message = sprintf( $message, $mailing );
?>
<div class="updated">
<p style="float:right;font-size:10px;text-align:right;">
<a id="dismiss-nelioab-campaign" href="#"><?php _e( 'Dismiss' ); ?></a>
</p>
<p style="font-size:15px;"><?php echo $message; ?></p>
<script style="display:none;" type="text/javascript">
(function($) {
$('a#dismiss-nelioab-campaign').on('click', function() {
$.post( ajaxurl, {action:'nelioab_campaign_dismiss_notice'} );
$(this).parent().parent().fadeOut();
});
})(jQuery);
</script>
</div>
<?php
}

}

153 changes: 153 additions & 0 deletions includes/nelio-efi-main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

/* =========================================================================*/
/* =========================================================================*/
/* SOME USEFUL FUNCTIONS */
/* =========================================================================*/
/* =========================================================================*/

/**
* This function returns Nelio EFI's post meta key. The key can be changed
* using the filter `nelioefi_post_meta_key'
*/
function _nelioefi_url() {
return apply_filters( 'nelioefi_post_meta_key', '_nelioefi_url' );
}


/**
* This function returns whether the post whose id is $id uses an external
* featured image or not
*/
function uses_nelioefi( $id ) {
$image_url = nelioefi_get_thumbnail_src( $id );
if ( $image_url === false )
return false;
else
return true;
}


/**
* This function returns the URL of the external featured image (if any), or
* false otherwise.
*/
function nelioefi_get_thumbnail_src( $id ) {
$image_url = get_post_meta( $id, _nelioefi_url(), true );
if ( !$image_url || strlen( $image_url ) == 0 )
return false;
return $image_url;
}


/**
* This function prints an image tag with the external featured image (if any).
* This tag, in fact, has a 1x1 px transparent gif image as its src, and
* includes the external featured image via inline CSS styling.
*/
function nelioefi_the_html_thumbnail( $id, $size = false, $attr = array() ) {
if ( uses_nelioefi( $id ) )
echo nelioefi_get_html_thumbnail( $id );
}


/**
* This function returns the image tag with the external featured image (if
* any). This tag, in fact, has a 1x1 px transparent gif image as its src,
* and includes the external featured image via inline CSS styling.
*/
function nelioefi_get_html_thumbnail( $id, $size = false, $attr = array() ) {
if ( uses_nelioefi( $id ) === false )
return false;

$image_url = nelioefi_get_thumbnail_src( $id );

$width = false;
$height = false;
$additional_classes = '';

global $_wp_additional_image_sizes;
if ( is_array( $size ) ) {
$width = $size[0];
$height = $size[1];
}
else if ( isset( $_wp_additional_image_sizes[ $size ] ) ) {
$width = $_wp_additional_image_sizes[ $size ]['width'];
$height = $_wp_additional_image_sizes[ $size ]['height'];
$additional_classes = 'attachment-' . $size . ' ';
}

if ( $width && $width > 0 ) $width = "width:${width}px;";
else $width = '';

if ( $height && $height > 0 ) $height = "height:${height}px;";
else $height = '';

if ( isset( $attr['class'] ) )
$additional_classes .= $attr['class'];

$html = sprintf(
'<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" ' .
'style="background:url(\'%s\') no-repeat center center;' .
'-webkit-background-size:cover;' .
'-moz-background-size:cover;' .
'-o-background-size:cover;' .
'background-size:cover;' .
'%s%s" class="%s wp-post-image nelioefi" />',
$image_url, $width, $height, $additional_classes );

return $html;
}


/* =========================================================================*/
/* =========================================================================*/
/* ALL HOOKS START HERE */
/* =========================================================================*/
/* =========================================================================*/

// Overriding post thumbnail when necessary
add_filter( 'genesis_pre_get_image', 'nelioefi_genesis_thumbnail', 10, 3 );
function nelioefi_genesis_thumbnail( $unknown_param, $args, $post ) {
$image_url = get_post_meta( $post->ID, _nelioefi_url(), true );

if ( !$image_url || strlen( $image_url ) == 0 ) {
return false;
}

if ( $args['format'] == 'html' ) {
$html = nelioefi_replace_thumbnail( '', $post->ID, 0, $args['size'], $args['attr'] );
$html = str_replace( 'style="', 'style="min-width:150px;min-height:150px;', $html );
return $html;
}
else {
return $image_url;
}
}


// Overriding post thumbnail when necessary
add_filter( 'post_thumbnail_html', 'nelioefi_replace_thumbnail', 10, 5 );
function nelioefi_replace_thumbnail( $html, $post_id, $post_image_id, $size, $attr ) {
if ( uses_nelioefi( $post_id ) )
$html = nelioefi_get_html_thumbnail( $post_id, $size, $attr );
return $html;
}


add_action( 'the_post', 'nelioefi_fake_featured_image_if_necessary' );
function nelioefi_fake_featured_image_if_necessary( $post ) {
if ( is_array( $post ) ) $post_ID = $post['ID'];
else $post_ID = $post->ID;

$has_nelioefi = strlen( get_post_meta( $post_ID, _nelioefi_url(), true ) ) > 0;
$wordpress_featured_image = get_post_meta( $post_ID, '_thumbnail_id', true );

if ( $has_nelioefi && !$wordpress_featured_image )
update_post_meta( $post_ID, '_thumbnail_id', -1 );
if ( !$has_nelioefi && $wordpress_featured_image == -1 )
delete_post_meta( $post_ID, '_thumbnail_id' );
}



52 changes: 52 additions & 0 deletions main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright 2013 Nelio Software S.L.
* This script is distributed under the terms of the GNU General Public
* License.
*
* This script is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License. This script 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, see <http://www.gnu.org/licenses/>.
*/


/*
* Plugin Name: Nelio External Featured Image
* Description: Use external images as your posts/page featured images. If you use an external service for storing your images and then linking them from your WordPress site, this plugin will perfectly fit your needs.
* Version: 1.0.9
* Author: Nelio Software
* Plugin URI: http://neliosoftware.com
* Text Domain: nelioefi
*/

// ==========================================================================
// PLUGIN INFORMATION
// ==========================================================================
define( 'NELIOEFI_PLUGIN_VERSION', '1.0.9' );
define( 'NELIOEFI_PLUGIN_NAME', 'Nelio External Featured Image' );
define( 'NELIOEFI_PLUGIN_DIR_NAME', basename( dirname( __FILE__ ) ) );

// Defining a few important directories
define( 'NELIOEFI_ROOT_DIR', rtrim( plugin_dir_path( __FILE__ ), '/' ) );
define( 'NELIOEFI_DIR', NELIOEFI_ROOT_DIR . '/includes' );
define( 'NELIOEFI_ADMIN_DIR', NELIOEFI_DIR . '/admin' );

// Some URLs...
define( 'NELIOEFI_ASSETS_URL', plugins_url() . '/' . NELIOEFI_PLUGIN_DIR_NAME . '/assets' );


// REGULAR STUFF
require_once( NELIOEFI_DIR . '/nelio-efi-main.php' );

// ADMIN STUFF
if ( is_admin() ) {
require_once( NELIOEFI_ADMIN_DIR . '/edit-post.php' );
require_once( NELIOEFI_ADMIN_DIR . '/nelioab-campaign.php' );
}

Loading

0 comments on commit 5c25289

Please sign in to comment.