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.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nelio committed Dec 9, 2014
1 parent ba58019 commit 0275d62
Show file tree
Hide file tree
Showing 5 changed files with 541 additions and 0 deletions.
108 changes: 108 additions & 0 deletions includes/admin/edit-post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?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="text" placeholder="ALT attribute" style="width:100%;margin-top:10px;<?php echo $show_if_img; ?>"
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 ) {
$url = '';
if ( isset( $_POST['nelioefi_url'] ) )
$url = strip_tags( $_POST['nelioefi_url'] );

update_post_meta( $post_ID, _nelioefi_url(), $url );

if ( isset( $_POST['nelioefi_alt'] ) && strlen( $url ) )
update_post_meta( $post_ID, '_nelioefi_alt', strip_tags( $_POST['nelioefi_alt'] ) );
else
update_post_meta( $post_ID, '_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
}

}

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

$alt = get_post_meta( $id, '_nelioefi_alt', true );
if ( isset( $attr['alt'] ) )
$alt = $attr['alt'];
if ( !$alt )
$alt = '';

$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" '.
'alt="%s" />',
$image_url, $width, $height, $additional_classes, $alt );

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' );
}



Loading

0 comments on commit 0275d62

Please sign in to comment.