Skip to content
This repository was archived by the owner on Apr 2, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,27 @@ sudo: false

language: php

notifications:
on_success: never
on_failure: change

php:
- 5.3
- 5.5

env:
- WP_VERSION=latest WP_MULTISITE=0

matrix:
include:
- php: 5.3
- php: 5.6
env: WP_VERSION=latest WP_MULTISITE=1
- php: 7.1
env: WP_VERSION=latest WP_MULTISITE=0

cache:
- composer
- npm
directories:
- vendor
- node_modules

before_script:
- composer install
- npm install
- bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
- bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION

script:
- grunt phpcs
- phpunit

branches:
only:
- master
10 changes: 6 additions & 4 deletions inc/class-img-shortcode-data-migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@ private static function img_shortcode_regex() {
private static function caption_shortcode_regex() {
$caption_shortcode_regex =
'\[caption' .
'[^\]]*' . '\]\]?' .
'[^\]]*' . '\]\]?' .
self::img_shortcode_regex() .
'(?: (?P<caption>[^\]]*))' .
'\[\[?\/caption\]\]?';
return $caption_shortcode_regex;
}

public static function find_img_tags_for_replacement_on_post( $post ) {
if ( ! $post = self::maybe_get_post_from_id( $post ) ) {
$post = self::maybe_get_post_from_id( $post );
if ( ! $post ) {
return false;
}

Expand Down Expand Up @@ -82,7 +83,8 @@ public static function find_img_tags_for_replacement( $post_content ) {


public static function find_caption_shortcodes_for_replacement_on_post( $post ) {
if ( ! $post = self::maybe_get_post_from_id( $post ) ) {
$post = self::maybe_get_post_from_id( $post );
if ( ! $post ) {
return false;
}

Expand Down Expand Up @@ -157,7 +159,7 @@ public static function convert_img_tag_to_shortcode( $img_tag, $attributes ) {

if ( get_permalink( (int) $attributes['attachment'] ) === $attributes['href'] ) {
$shortcode_attrs['linkto'] = 'attachment';
} else if ( $attachment_src[0] === $attributes['href'] // link to full size image
} elseif ( $attachment_src[0] === $attributes['href'] // link to full size image
|| $attributes['src'] === $attributes['href'] // link the same as image src
) {
$shortcode_attrs['linkto'] = 'file';
Expand Down
132 changes: 67 additions & 65 deletions inc/class-img-shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,79 +43,79 @@ public static function get_shortcode_ui_args() {

$shortcode_ui_args = array(

'label' => esc_html__( 'Image', 'image-shortcake' ),
'label' => esc_html__( 'Image', 'image-shortcake' ),

'listItemImage' => 'dashicons-format-image',
'listItemImage' => 'dashicons-format-image',

'attrs' => array(
'attrs' => array(

array(
'label' => esc_html__( 'Choose Attachment', 'image-shortcake' ),
'attr' => 'attachment',
'type' => 'attachment',
'libraryType' => array( 'image' ),
'addButton' => esc_attr__( 'Select Image', 'image-shortcake' ),
'frameTitle' => esc_attr__( 'Select Image', 'image-shortcake' ),
),
array(
'label' => esc_html__( 'Choose Attachment', 'image-shortcake' ),
'attr' => 'attachment',
'type' => 'attachment',
'libraryType' => array( 'image' ),
'addButton' => esc_attr__( 'Select Image', 'image-shortcake' ),
'frameTitle' => esc_attr__( 'Select Image', 'image-shortcake' ),
),

array(
'label' => esc_html__( 'Image size', 'image-shortcake' ),
'attr' => 'size',
'type' => 'select',
'value' => 'large',
'options' => $sizes_available,
),
array(
'label' => esc_html__( 'Image size', 'image-shortcake' ),
'attr' => 'size',
'type' => 'select',
'value' => 'large',
'options' => $sizes_available,
),

array(
'label' => esc_html__( 'Alt', 'image-shortcake' ),
'attr' => 'alt',
'type' => 'text',
'encode' => true,
'placeholder' => esc_attr__( 'Alt text for the image', 'image-shortcake' ),
),
array(
'label' => esc_html__( 'Alt', 'image-shortcake' ),
'attr' => 'alt',
'type' => 'text',
'encode' => true,
'placeholder' => esc_attr__( 'Alt text for the image', 'image-shortcake' ),
),

array(
'label' => esc_html__( 'Caption', 'image-shortcake' ),
'attr' => 'caption',
'type' => 'text',
'encode' => true,
'placeholder' => esc_attr__( 'Caption for the image', 'image-shortcake' ),
),
array(
'label' => esc_html__( 'Caption', 'image-shortcake' ),
'attr' => 'caption',
'type' => 'text',
'encode' => true,
'placeholder' => esc_attr__( 'Caption for the image', 'image-shortcake' ),
),

array(
'label' => esc_html__( 'Alignment', 'image-shortcake' ),
'attr' => 'align',
'type' => 'select',
'value' => 'aligncenter',
'options' => array(
'alignleft' => esc_attr__( 'Left', 'image-shortcake' ),
'aligncenter' => esc_attr__( 'Center', 'image-shortcake' ),
'alignright' => esc_attr__( 'Right', 'image-shortcake' ),
'alignnone' => esc_attr__( 'None', 'image-shortcake' ),
),
array(
'label' => esc_html__( 'Alignment', 'image-shortcake' ),
'attr' => 'align',
'type' => 'select',
'value' => 'aligncenter',
'options' => array(
'alignleft' => esc_attr__( 'Left', 'image-shortcake' ),
'aligncenter' => esc_attr__( 'Center', 'image-shortcake' ),
'alignright' => esc_attr__( 'Right', 'image-shortcake' ),
'alignnone' => esc_attr__( 'None', 'image-shortcake' ),
),
),

array(
'label' => esc_html__( 'Link to', 'image-shortcake' ),
'attr' => 'linkto',
'type' => 'select',
'value' => get_option( 'image_default_link_type' ),
'options' => array(
'none' => esc_attr__( 'None (no link)', 'image-shortcake' ),
'attachment' => esc_attr__( 'Link to attachment file', 'image-shortcake' ),
'file' => esc_attr__( 'Link to file', 'image-shortcake' ),
'custom' => esc_attr__( 'Custom link', 'image-shortcake' ),
),
array(
'label' => esc_html__( 'Link to', 'image-shortcake' ),
'attr' => 'linkto',
'type' => 'select',
'value' => get_option( 'image_default_link_type' ),
'options' => array(
'none' => esc_attr__( 'None (no link)', 'image-shortcake' ),
'attachment' => esc_attr__( 'Link to attachment file', 'image-shortcake' ),
'file' => esc_attr__( 'Link to file', 'image-shortcake' ),
'custom' => esc_attr__( 'Custom link', 'image-shortcake' ),
),
),

array(
'label' => esc_html__( 'Custom link', 'image-shortcake' ),
'attr' => 'url',
'type' => 'text',
'placeholder' => esc_attr__( 'URL to link to (if above link is "custom")', 'image-shortcake' ),
),
array(
'label' => esc_html__( 'Custom link', 'image-shortcake' ),
'attr' => 'url',
'type' => 'text',
'placeholder' => esc_attr__( 'URL to link to (if above link is "custom")', 'image-shortcake' ),
),
);
),
);

/**
* Filter the shortcode UI definition arguments
Expand Down Expand Up @@ -193,7 +193,7 @@ public static function callback( $attr, $_null, $shortcode_tag ) {
$image_attr['src'] = esc_url( $attachment[0] );
$image_attr['width'] = intval( $attachment[1] );
$image_attr['height'] = intval( $attachment[2] );
} else if ( ! empty( $attr['src'] ) ) {
} elseif ( ! empty( $attr['src'] ) ) {
$image_attr['src'] = esc_url( $attr['src'] );
} else {
return; // An image without a src isn't much of an image
Expand Down Expand Up @@ -271,7 +271,7 @@ private static function linkify( $img_tag, $attributes ) {

if ( isset( $attributes['url'] ) ) {
$link_attrs['href'] = esc_url( $attributes['url'] );
} else if ( ! empty( $attributes['linkto'] ) && 'attachment' === $attributes['linkto'] ) {
} elseif ( ! empty( $attributes['linkto'] ) && 'attachment' === $attributes['linkto'] ) {
$link_attrs['href'] = get_permalink( $_id );
} elseif ( ! empty( $attributes['linkto'] ) && 'file' === $attributes['linkto'] ) {
$attachment_src = wp_get_attachment_image_src( $_id, 'full', false, $attributes );
Expand All @@ -287,7 +287,7 @@ private static function linkify( $img_tag, $attributes ) {
$html .= sanitize_key( $attr_name ) . '="' . esc_attr( $attr_value ) . '" ';
}

$html .= '>' . $img_tag .'</a>';
$html .= '>' . $img_tag . '</a>';

return $html;
}
Expand Down Expand Up @@ -321,7 +321,9 @@ private static function captionify( $img_tag, $attributes ) {

// Ensure the image has a width defined; caption shortcode will break otherwise.
if ( 0 === intval( $attributes['width'] ) ) {
if ( $_attachment_src = wp_get_attachment_image_src( $attributes['id'], $attributes['size'] ) ) {
$_attachment_src = wp_get_attachment_image_src( $attributes['id'], $attributes['size'] );

if ( $_attachment_src ) {
$attributes['width'] = $_attachment_src[1];
}
}
Expand Down
6 changes: 3 additions & 3 deletions inc/class-wp-cli-img-shortcode-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public function __construct() {
*/
public function migrate( $args, $assoc_args ) {

foreach ( array_filter( $args ) as $post_ID ) {
foreach ( array_filter( $args ) as $post_id ) {

$post = $this->fetcher->get_check( $post_ID );
$post = $this->fetcher->get_check( $post_id );

$_content = $post->post_content;

Expand Down Expand Up @@ -89,7 +89,7 @@ public function migrate( $args, $assoc_args ) {
global $wpdb;

// @codingStandardsIgnoreStart
$updated = $wpdb->update( $wpdb->posts, array( 'post_content' => $_content ), array( 'ID' => $post_ID ) );
$updated = $wpdb->update( $wpdb->posts, array( 'post_content' => $_content ), array( 'ID' => $post_id ) );
// @codingStandardsIgnoreEnd

if ( 1 === $updated ) {
Expand Down
1 change: 1 addition & 0 deletions phpcs.ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<rule ref="WordPress-VIP">
<exclude name="WordPress.CSRF.NonceVerification" />
<exclude name="WordPress.WhiteSpace.ControlStructureSpacing" />
<exclude name="WordPress.Files.FileName" />
<exclude name="WordPress.VIP.SuperGlobalInputUsage" />
<exclude name="WordPress.VIP.ValidatedSanitizedInput" />
</rule>
Expand Down
8 changes: 4 additions & 4 deletions tests/test-img-shortcode-data-migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function test_img_tag_from_attachment() {

$this->assertContains( $img_tag, array_keys( $replacements ) );

$this->assertContains( 'attachment="' . $this->attachment_id .'"', $replacements[ $img_tag ] );
$this->assertContains( 'attachment="' . $this->attachment_id . '"', $replacements[ $img_tag ] );
$this->assertNotContains( 'src="', $replacements[ $img_tag ] );
}

Expand All @@ -90,7 +90,7 @@ function test_img_tag_from_src() {
$replacements = Img_Shortcode_Data_Migration::find_img_tags_for_replacement_on_post( $post_id );

$this->assertNotContains( 'attachment="', $replacements[ $img_tag ] );
$this->assertContains( 'src="' . $this->image_src .'"', $replacements[ $img_tag ] );
$this->assertContains( 'src="' . $this->image_src . '"', $replacements[ $img_tag ] );

}

Expand Down Expand Up @@ -120,7 +120,7 @@ public function test_img_tags_wrapped_in_links() {
$this->assertContains( $should_be_matched, array_keys( $replacements ) );
}

$this->assertContains( 'attachment="' . $this->attachment_id .'"', $replacements[ $img_tag ] );
$this->assertContains( 'attachment="' . $this->attachment_id . '"', $replacements[ $img_tag ] );
$this->assertNotContains( 'src="', $replacements[ $img_tag ] );

$this->assertContains( 'href="http://go.to/thislink/"', $replacements[ $img_tag_link_custom ] );
Expand Down Expand Up @@ -168,7 +168,7 @@ public function test_replace_caption_shortcodes() {
*/
private function insert_attachment( $parent_post_id = 0, $image = null, $post_fields = array() ) {

$filename = rand_str().'.jpg';
$filename = rand_str() . '.jpg';
$contents = rand_str();

if ( $image ) {
Expand Down
2 changes: 1 addition & 1 deletion tests/test-img-shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ function test_other_media_send_to_editor() {
*/
private function insert_attachment( $parent_post_id = 0, $image = null, $post_fields = array() ) {

$filename = rand_str().'.jpg';
$filename = rand_str() . '.jpg';
$contents = rand_str();

if ( $image ) {
Expand Down