Fix image fullwidth toggle [MAILPOET-1365]

Prevent WordPress $content_width global to override
mailpoet_image_size when calling wp_get_attachment_image_src()
in ACL generated posts.
This commit is contained in:
Fred. P
2018-05-30 11:37:50 +02:00
parent b7e33aa997
commit ac47866295
6 changed files with 92 additions and 9 deletions

View File

@@ -5,6 +5,8 @@ namespace MailPoet\Config;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
class Env { class Env {
const NEWSLETTER_CONTENT_WIDTH = 1320;
static $version; static $version;
static $plugin_name; static $plugin_name;
static $plugin_path; static $plugin_path;

View File

@@ -213,7 +213,7 @@ class Initializer {
} }
function setupImages() { function setupImages() {
add_image_size('mailpoet_newsletter_max', 1320); add_image_size('mailpoet_newsletter_max', Env::NEWSLETTER_CONTENT_WIDTH);
} }
function setupChangelog() { function setupChangelog() {
@@ -301,4 +301,4 @@ class Initializer {
} }
return WPNotice::displayError($exception); return WPNotice::displayError($exception);
} }
} }

View File

@@ -4,6 +4,7 @@ namespace MailPoet\Newsletter\Editor;
use MailPoet\Newsletter\Editor\PostContentManager; use MailPoet\Newsletter\Editor\PostContentManager;
use MailPoet\Newsletter\Editor\MetaInformationManager; use MailPoet\Newsletter\Editor\MetaInformationManager;
use MailPoet\Newsletter\Editor\StructureTransformer; use MailPoet\Newsletter\Editor\StructureTransformer;
use MailPoet\WP\Functions as WPFunctions;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
@@ -68,14 +69,10 @@ class PostTransformer {
} }
private function getFeaturedImage($post_id, $post_title, $image_full_width) { private function getFeaturedImage($post_id, $post_title, $image_full_width) {
if(has_post_thumbnail($post_id)) { if(has_post_thumbnail($post_id)) {
$thumbnail_id = get_post_thumbnail_id($post_id); $thumbnail_id = get_post_thumbnail_id($post_id);
$image_info = WPFunctions::getImageInfo($thumbnail_id);
// get attachment data (src, width, height)
$image_info = wp_get_attachment_image_src(
$thumbnail_id,
'mailpoet_newsletter_max'
);
// get alt text // get alt text
$alt_text = trim(strip_tags(get_post_meta( $alt_text = trim(strip_tags(get_post_meta(

View File

@@ -1,6 +1,8 @@
<?php <?php
namespace MailPoet\WP; namespace MailPoet\WP;
use Mailpoet\Config\Env;
class Functions { class Functions {
static function wpRemotePost() { static function wpRemotePost() {
return self::callWithFallback('wp_remote_post', func_get_args()); return self::callWithFallback('wp_remote_post', func_get_args());
@@ -33,4 +35,23 @@ class Functions {
} }
return call_user_func_array($func, $args); return call_user_func_array($func, $args);
} }
}
static function getImageInfo($id) {
/*
* In some cases wp_get_attachment_image_src ignore the second parameter
* and use global variable $content_width value instead.
* By overriding it ourselves when ensure a constant behaviour regardless
* of the user setup.
*
* https://mailpoet.atlassian.net/browse/MAILPOET-1365
*/
global $content_width; // default is NULL
$content_width_copy = $content_width;
$content_width = Env::NEWSLETTER_CONTENT_WIDTH;
$image_info = wp_get_attachment_image_src($id, 'mailpoet_newsletter_max');
$content_width = $content_width_copy;
return $image_info;
}
}

BIN
tests/_data/test-image.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

View File

@@ -0,0 +1,63 @@
<?php
namespace MailPoet\Test\WP;
use MailPoet\WP\Functions as WPFunctions;
use MailPoet\Config\Env;
class FunctionsTest extends \MailPoetTest {
function _before() {
global $content_width;
$this->_content_width = $content_width;
$content_width = 150;
}
function makeAttachment($upload, $parent_post_id = 0) {
$type = '';
if(!empty($upload['type'])) {
$type = $upload['type'];
} else {
$mime = wp_check_filetype($upload['file']);
if ($mime)
$type = $mime['type'];
}
$attachment = array(
'post_title' => basename($upload['file']),
'post_content' => '',
'post_type' => 'attachment',
'post_parent' => $parent_post_id,
'post_mime_type' => $type,
'guid' => $upload['url'],
);
// Save the data
$id = wp_insert_attachment($attachment, $upload['file'], $parent_post_id);
$metadata = wp_generate_attachment_metadata($id, $upload['file']);
wp_update_attachment_metadata($id, $metadata);
return $this->ids[] = $id;
}
function testItCanGetImageInfo() {
expect(
function_exists('wp_generate_attachment_metadata')
)->true();
$filename = 'tests/_data/test-image.jpg';
$contents = file_get_contents($filename);
$upload = wp_upload_bits(basename($filename), null, $contents);
$id = $this->makeAttachment($upload);
expect($id)->notEmpty();
$image = WPFunctions::getImageInfo($id);
expect($image[1])->equals(Env::NEWSLETTER_CONTENT_WIDTH);
wp_delete_attachment($id, $force_delete = true);
}
function _after() {
global $content_width;
$content_width = $this->_content_width;
}
}