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.3.0 which supports virtually all themes
Browse files Browse the repository at this point in the history
  • Loading branch information
nelio committed Jul 28, 2015
1 parent ba58019 commit 22b837c
Show file tree
Hide file tree
Showing 6 changed files with 537 additions and 0 deletions.
Binary file added assets/nelioefi-placeholder.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
104 changes: 104 additions & 0 deletions includes/admin/edit-post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?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 );
$nelioefi_alt = get_post_meta( $post->ID, '_nelioefi_alt', true );
$has_img = strlen( $nelioefi_url ) > 0;
if ( $has_img ) {
$hide_if_img = 'display:none;';
$show_if_img = '';
}
else {
$hide_if_img = '';
$show_if_img = 'display:none;';
}
?>
<input type="hidden" id="nelioefi_alt" name="nelioefi_alt"
value="<?php echo esc_attr( $nelioefi_alt ); ?>" /><?php
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>

<a id="nelioefi_remove_button" href="#" onClick="javascript:nelioefiRemoveFeaturedImage();" style="<?php echo $show_if_img; ?>">Remove featured image</a>
<script>
function nelioefiRemoveFeaturedImage() {
jQuery("#nelioefi_preview_block").hide();
jQuery("#nelioefi_image_wrapper").hide();
jQuery("#nelioefi_remove_button").hide();
jQuery("#nelioefi_alt").hide();
jQuery("#nelioefi_alt").val('');
jQuery("#nelioefi_url").val('');
jQuery("#nelioefi_url").show();
jQuery("#nelioefi_preview_button").parent().show();
}
function nelioefiPreview() {
jQuery("#nelioefi_preview_block").show();
jQuery("#nelioefi_image_wrapper").css('background-image', "url('" + jQuery("#nelioefi_url").val() + "')" );
jQuery("#nelioefi_image_wrapper").show();
jQuery("#nelioefi_remove_button").show();
jQuery("#nelioefi_alt").show();
jQuery("#nelioefi_url").hide();
jQuery("#nelioefi_preview_button").parent().hide();
}
</script>
</div>
<input type="text" placeholder="Image URL" style="width:100%;margin-top:10px;<?php echo $hide_if_img; ?>"
id="nelioefi_url" name="nelioefi_url"
value="<?php echo esc_attr( $nelioefi_url ); ?>" />
<div style="text-align:right;margin-top:10px;<?php echo $hide_if_img; ?>">
<a class="button" id="nelioefi_preview_button" onClick="javascript:nelioefiPreview();">Preview</a>
</div>
<?php
}

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

if ( isset( $_POST['nelioefi_alt'] ) )
update_post_meta( $post_ID, '_nelioefi_alt', strip_tags( $_POST['nelioefi_alt'] ) );
}



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
}

}

157 changes: 157 additions & 0 deletions includes/nelio-efi-main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?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;
}


$nelioab_first_time = true;
function nelioefi_get_placeholder() {
$image_id = get_option( 'nelioefi_placeholder_id', false );

global $nelioab_first_time;
if ( $image_id && $nelioab_first_time ) {
$nelioab_first_time = false;
$aux = get_post( $image_id );
if ( empty( $aux ) ) {
update_option( 'nelioefi_placeholder_id', false );
$image_id = false;
}
}

if ( ! $image_id ) {
$aux = wp_upload_dir();
$filename = 'nelioefi-placeholder.png';
$src_path = trailingslashit( dirname( dirname( __FILE__ ) ) ) . 'assets' . DIRECTORY_SEPARATOR . $filename;
$dest_path = trailingslashit( $aux['basedir'] ) . $filename;
if ( copy( $src_path, $dest_path ) ) {
$attachment = array(
'post_mime_type' => 'image/png',
'post_title' => __( 'NelioEFI\'s Placeholder', 'nelioefi' ),
'post_content' => '',
'post_status' => 'inherit',
'guid' => trailingslashit( $aux['baseurl'] ) . $filename
);
$url = trailingslashit( $aux['baseurl'] );
$url = str_replace( 'https://', '', $url );
$url = str_replace( 'http://', '', $url );
$url = substr( $url, strpos( $url, '/' ) );
$filename = $url . $filename;
$image_id = wp_insert_attachment( $attachment, $filename );
if ( ! is_wp_error( $image_id ) ) {
update_option( 'nelioefi_placeholder_id', $image_id );
}
}
}

return get_option( 'nelioefi_placeholder_id', false );
}


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

// Set the featured image ID of the post to the transparency gif.
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;

$wp_featured_image = get_post_meta( $post_ID, '_thumbnail_id', true );
$aux = get_post( $wp_featured_image );
if ( $wp_featured_image && empty( $aux ) ) {
$wp_featured_image = false;
}

if ( uses_nelioefi( $post_ID ) && !$wp_featured_image ) {
update_post_meta( $post_ID, '_thumbnail_id', nelioefi_get_placeholder() );
}
if ( ! uses_nelioefi( $post_ID ) && $wp_featured_image == -1 ) {
delete_post_meta( $post_ID, '_thumbnail_id' );
}

}


$nelioefi_images = array();

// Modify the transparency gif.
add_filter( 'image_downsize', 'nelioefi_add_image_in_placeholder', 10, 3 );
function nelioefi_add_image_in_placeholder( $downsize, $id, $size ) {
if ( nelioefi_get_placeholder() == $id ) {
remove_filter( 'image_downsize', 'nelioefi_add_image_in_placeholder', 10, 3 );
$result = wp_get_attachment_image_src( $id, $size );
add_filter( 'image_downsize', 'nelioefi_add_image_in_placeholder', 10, 3 );

$nelioefi = get_post_meta( get_the_ID(), _nelioefi_url(), true );
if ( $nelioefi ) {
global $nelioefi_images;
$nelioefi_images[get_the_ID()] = $nelioefi;
$result[0] = $result[0] . '?id=' . get_the_ID();
return $result;
}
}
return false;
}


add_action( 'wp_footer', 'nelioefi_print_script' );
function nelioefi_print_script() {
global $nelioefi_images; ?>
<script type="text/javascript">
(function($) {
var images = <?php echo json_encode( $nelioefi_images ); ?>;
$('img[src*=nelioefi-placeholder]').each(function() {
try {
var src = $(this).attr( 'src' );
var id = src.substring( src.indexOf( '=' ) + 1 );
var value = 'url("' + images[id] + '") no-repeat center center';
$(this).css( 'background', value );
$(this).css( '-webkit-background-size', 'cover' );
$(this).css( '-moz-background-size', 'cover' );
$(this).css( '-o-background-size', 'cover' );
$(this).css( 'background-size', 'cover' );
} catch ( e ) {}
});
})(jQuery);
</script>
<?php
}

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.3.0
* Author: Nelio Software
* Plugin URI: http://neliosoftware.com
* Text Domain: nelioefi
*/

// ==========================================================================
// PLUGIN INFORMATION
// ==========================================================================
define( 'NELIOEFI_PLUGIN_VERSION', '1.3.0' );
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 22b837c

Please sign in to comment.