This repository has been archived by the owner on Apr 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
class-image-comparison.php
138 lines (114 loc) · 4.3 KB
/
class-image-comparison.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
namespace Shortcake_Bakery\Shortcodes;
class Image_Comparison extends Shortcode {
public static function get_shortcode_ui_args() {
return array(
'label' => esc_html__( 'Image Comparison', 'shortcake-bakery' ),
'listItemImage' => 'dashicons-format-gallery',
'attrs' => array(
array(
'label' => esc_html__( 'Left Image', 'shortcake-bakery' ),
'attr' => 'left',
'type' => 'attachment',
'libraryType' => array( 'image' ),
'addButton' => esc_html__( 'Select Image', 'shortcake-bakery' ),
'frameTitle' => esc_html__( 'Select Image', 'shortcake-bakery' ),
),
array(
'label' => esc_html__( 'Right Image', 'shortcake-bakery' ),
'attr' => 'right',
'type' => 'attachment',
'libraryType' => array( 'image' ),
'addButton' => esc_html__( 'Select Image', 'shortcake-bakery' ),
'frameTitle' => esc_html__( 'Select Image', 'shortcake-bakery' ),
),
array(
'label' => esc_html__( 'Slider Start Position', 'shortcake-bakery' ),
'attr' => 'position',
'type' => 'select',
'options' => array(
'center' => esc_html__( 'Center', 'shortcake-bakery' ),
'mostlyleft' => esc_html__( 'Mostly Left', 'shortcake-bakery' ),
'mostlyright' => esc_html__( 'Mostly Right', 'shortcake-bakery' ),
),
),
),
);
}
public static function setup_actions() {
add_action( 'wp_enqueue_scripts', 'Shortcake_Bakery\Shortcodes\Image_Comparison::action_init_register_scripts' );
add_action(
'shortcode_ui_after_do_shortcode', function( $shortcode ) {
if ( false !== stripos( $shortcode, '[' . self::get_shortcode_tag() ) ) {
echo '<link rel="stylesheet" href="' . esc_url( SHORTCAKE_BAKERY_URL_ROOT . 'assets/css/juxtapose.css' ) . '">';
echo '<script type="text/javascript" src="' . esc_url( SHORTCAKE_BAKERY_URL_ROOT . 'assets/js/juxtapose.js' ) . '"></script>';
}
}
);
}
public static function action_init_register_scripts() {
wp_register_script( 'juxtapose-js', SHORTCAKE_BAKERY_URL_ROOT . 'assets/js/juxtapose.js', array( 'jquery' ) );
wp_register_style( 'juxtapose-css', SHORTCAKE_BAKERY_URL_ROOT . 'assets/css/juxtapose.css' );
}
public static function callback( $attrs, $content = '' ) {
if ( empty( $attrs['left'] ) || empty( $attrs['right'] ) ) {
if ( current_user_can( 'edit_posts' ) ) {
return '<div class="shortcake-bakery-error"><p>' . esc_html__( 'Two images required for image comparison.', 'shortcake-bakery' ) . '</p></div>';
} else {
return '';
}
}
if ( empty( $attrs['position'] ) ) {
$attrs['position'] = 'center';
}
switch ( $attrs['position'] ) {
case 'center':
$attrs['position'] = 50;
break;
case 'mostlyleft':
$attrs['position'] = 10;
break;
case 'mostlyright':
$attrs['position'] = 90;
break;
}
$left_image = wp_get_attachment_image_src( $attrs['left'], 'large', false );
$right_image = wp_get_attachment_image_src( $attrs['right'], 'large', false );
$left_caption = get_post_field( 'post_excerpt', $attrs['left'] );
$right_caption = get_post_field( 'post_excerpt', $attrs['right'] );
$left_meta = wp_get_attachment_metadata( $attrs['left'] );
$right_meta = wp_get_attachment_metadata( $attrs['right'] );
$left_credit = $left_meta['image_meta']['credit'];
$right_credit = $right_meta['image_meta']['credit'];
if ( ! $left_image || ! $right_image ) {
return;
}
wp_enqueue_script( 'juxtapose-js' );
wp_enqueue_style( 'juxtapose-css' );
/* Begin container */
$out = '<section class="image-comparison">';
$out .= '<div class="juxtapose" data-startingposition="';
$out .= esc_attr( $attrs['position'] );
$out .= '" data-showlabels="true" data-showcredits="true" data-animate="true">';
/* Left Image */
$out .= '<img src="';
$out .= esc_url( $left_image[0] );
$out .= '" data-label="';
$out .= esc_attr( $left_caption );
$out .= '" data-credit="';
$out .= esc_attr( $left_credit );
$out .= '">';
/* Right Image */
$out .= '<img src="';
$out .= esc_url( $right_image[0] );
$out .= '" data-label="';
$out .= esc_attr( $right_caption );
$out .= '" data-credit="';
$out .= esc_attr( $right_credit );
$out .= '">';
/* Close container */
$out .= '</div>';
$out .= '</section>';
return $out;
}
}