Version Description
Download this release
Release Info
Developer | bradyvercher |
Plugin | Simple Image Widget |
Version | 4.4.1 |
Comparing to | |
See all releases |
Code changes from version 4.4.0 to 4.4.1
- assets/js/simple-image-widget.js +3 -3
- includes/class-simple-image-widget-legacy.php +158 -158
- includes/class-simple-image-widget-plugin.php +374 -374
- includes/class-simple-image-widget-template-loader.php +265 -265
- includes/class-simple-image-widget.php +576 -576
- readme.txt +149 -141
- simple-image-widget.php +79 -79
- templates/widget.php +45 -45
assets/js/simple-image-widget.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
/*global _:false, ajaxurl:false, Backbone:false, wp:false */
|
2 |
|
3 |
window.SimpleImageWidget = window.SimpleImageWidget || {};
|
4 |
|
@@ -18,7 +18,7 @@ window.SimpleImageWidget = window.SimpleImageWidget || {};
|
|
18 |
* Control module object.
|
19 |
*/
|
20 |
Control = function( el, options ) {
|
21 |
-
var defaults, selector
|
22 |
|
23 |
this.$el = $( el );
|
24 |
|
@@ -159,7 +159,7 @@ window.SimpleImageWidget = window.SimpleImageWidget || {};
|
|
159 |
}
|
160 |
|
161 |
if ( ! options ) {
|
162 |
-
name = l10n.imageSizeNames
|
163 |
options = '<option value="full">' + name + '</option>';
|
164 |
}
|
165 |
|
1 |
+
/*global _:false, ajaxurl:false, Backbone:false, jQuery:false, wp:false */
|
2 |
|
3 |
window.SimpleImageWidget = window.SimpleImageWidget || {};
|
4 |
|
18 |
* Control module object.
|
19 |
*/
|
20 |
Control = function( el, options ) {
|
21 |
+
var defaults, selector;
|
22 |
|
23 |
this.$el = $( el );
|
24 |
|
159 |
}
|
160 |
|
161 |
if ( ! options ) {
|
162 |
+
name = l10n.imageSizeNames.full || l10n.fullSizeLabel;
|
163 |
options = '<option value="full">' + name + '</option>';
|
164 |
}
|
165 |
|
includes/class-simple-image-widget-legacy.php
CHANGED
@@ -1,158 +1,158 @@
|
|
1 |
-
<?php
|
2 |
-
/**
|
3 |
-
* Legacy support.
|
4 |
-
*
|
5 |
-
* @package SimpleImageWidget
|
6 |
-
* @copyright Copyright (c) 2015 Cedaro, LLC
|
7 |
-
* @license GPL-2.0+
|
8 |
-
* @since 4.0.0
|
9 |
-
*/
|
10 |
-
|
11 |
-
/**
|
12 |
-
* Class to add support for features and data from previous versions.
|
13 |
-
*
|
14 |
-
* @package SimpleImageWidget
|
15 |
-
* @since 4.0.0
|
16 |
-
*/
|
17 |
-
class Simple_Image_Widget_Legacy {
|
18 |
-
/**
|
19 |
-
* Load legacy support.
|
20 |
-
*
|
21 |
-
* @since 4.0.0
|
22 |
-
*/
|
23 |
-
public function load() {
|
24 |
-
add_filter( 'simple_image_widget_output', array( $this, 'output' ), 10, 4 );
|
25 |
-
add_filter( 'simple_image_widget_fields', array( $this, 'fields' ), 10, 2 );
|
26 |
-
add_action( 'simple_image_widget_field-legacy', array( $this, 'display_fields' ), 10, 2 );
|
27 |
-
add_filter( 'simple_image_widget_instance', array( $this, 'sanitize_data' ), 10, 4 );
|
28 |
-
}
|
29 |
-
|
30 |
-
/**
|
31 |
-
* Legacy widget output.
|
32 |
-
*
|
33 |
-
* @since 4.0.0
|
34 |
-
*
|
35 |
-
* @param string $output HTML output.
|
36 |
-
* @param array $args Registered sidebar arguments including before_title, after_title, before_widget, and after_widget.
|
37 |
-
* @param array $instance The widget instance settings.
|
38 |
-
* @param string $id_base Base widget type id.
|
39 |
-
* @return string HTML output.
|
40 |
-
*/
|
41 |
-
public function output( $output, $args, $instance, $id_base ) {
|
42 |
-
if ( 'simpleimage' != $id_base || ! empty( $instance['image_id'] ) || empty( $instance['image'] ) ) {
|
43 |
-
return $output;
|
44 |
-
}
|
45 |
-
|
46 |
-
// Legacy output.
|
47 |
-
$output = ( empty( $instance['title'] ) ) ? '' : $args['before_title'] . $instance['title'] . $args['after_title'];
|
48 |
-
|
49 |
-
// Add the image.
|
50 |
-
$output = sprintf(
|
51 |
-
'%s<img src="%s" alt="%s">%s',
|
52 |
-
$instance['link_open'],
|
53 |
-
esc_url( $instance['image'] ),
|
54 |
-
( empty( $instance['alt'] ) ) ? '' : esc_attr( $instance['alt'] ),
|
55 |
-
$instance['link_close']
|
56 |
-
);
|
57 |
-
|
58 |
-
// Add the text.
|
59 |
-
if ( ! empty( $instance['text'] ) ) {
|
60 |
-
$output .= apply_filters( 'the_content', $instance['text'] );
|
61 |
-
}
|
62 |
-
|
63 |
-
// Add a more link.
|
64 |
-
if ( ! empty( $instance['link_open'] ) && ! empty( $instance['link_text'] ) ) {
|
65 |
-
$output .= '<p class="more">' . $instance['link_open'] . $instance['link_text'] . $instance['link_close'] . '</p>';
|
66 |
-
}
|
67 |
-
|
68 |
-
return $output;
|
69 |
-
}
|
70 |
-
|
71 |
-
/**
|
72 |
-
* Remove the image size field for versions of WordPress older than 3.5.
|
73 |
-
*
|
74 |
-
* @since 4.0.0
|
75 |
-
*
|
76 |
-
* @param array $fields List of field ids.
|
77 |
-
* @param string $id_base Base widget type id.
|
78 |
-
* @return array
|
79 |
-
*/
|
80 |
-
public function fields( $fields, $id_base ) {
|
81 |
-
if ( 'simpleimage' == $id_base && is_simple_image_widget_legacy() ) {
|
82 |
-
$key = array_search( 'image_size', $fields );
|
83 |
-
if ( false !== $key ) {
|
84 |
-
unset( $fields[ $key ] );
|
85 |
-
}
|
86 |
-
|
87 |
-
// Add a field for the old widget stuff.
|
88 |
-
array_unshift( $fields, 'legacy' );
|
89 |
-
}
|
90 |
-
|
91 |
-
return $fields;
|
92 |
-
}
|
93 |
-
|
94 |
-
/**
|
95 |
-
* Display legacy fields in the widget edit form.
|
96 |
-
*
|
97 |
-
* @since 4.0.0
|
98 |
-
*
|
99 |
-
* @param array $instance The widget instance settings.
|
100 |
-
* @param WP_Widget $widget Widget instance.
|
101 |
-
*/
|
102 |
-
public function display_fields( $instance, $widget ) {
|
103 |
-
if ( is_simple_image_widget_legacy() || ! empty( $instance['image'] ) ) :
|
104 |
-
?>
|
105 |
-
<div class="simple-image-widget-legacy-fields">
|
106 |
-
<?php if ( ! is_simple_image_widget_legacy() ) : ?>
|
107 |
-
<p>
|
108 |
-
<em><?php _e( 'These fields are here to maintain your data from an earlier version.', 'simple-image-widget' ); ?></em>
|
109 |
-
</p>
|
110 |
-
<p>
|
111 |
-
<em><?php _e( 'Select an image, then clear these values, and they will disappear when you save the widget.', 'simple-image-widget' ); ?></em>
|
112 |
-
</p>
|
113 |
-
<?php endif; ?>
|
114 |
-
|
115 |
-
<p>
|
116 |
-
<label for="<?php echo esc_attr( $widget->get_field_id( 'image' ) ); ?>"><?php _e( 'Image URL:', 'simple-image-widget' ); ?></label>
|
117 |
-
<input type="text" name="<?php echo esc_attr( $widget->get_field_name( 'image' ) ); ?>" id="<?php echo esc_attr( $widget->get_field_id( 'image' ) ); ?>" value="<?php echo esc_url( $instance['image'] ); ?>" class="widefat">
|
118 |
-
</p>
|
119 |
-
<p>
|
120 |
-
<label for="<?php echo esc_attr( $widget->get_field_id( 'alt' ) ); ?>"><?php _e( 'Alternate Text:', 'simple-image-widget' ); ?></label>
|
121 |
-
<input type="text" name="<?php echo esc_attr( $widget->get_field_name( 'alt' ) ); ?>" id="<?php echo esc_attr( $widget->get_field_id( 'alt' ) ); ?>" value="<?php echo esc_attr( $instance['alt'] ); ?>" class="widefat">
|
122 |
-
</p>
|
123 |
-
</div>
|
124 |
-
<?php
|
125 |
-
endif;
|
126 |
-
}
|
127 |
-
|
128 |
-
/**
|
129 |
-
* Sanitize legacy field values.
|
130 |
-
*
|
131 |
-
* Called in Simple_Image_Widget::update().
|
132 |
-
*
|
133 |
-
* @since 4.0.0
|
134 |
-
*
|
135 |
-
* @param array $instance Merged widget settings.
|
136 |
-
* @param array $new_instance New widget settings.
|
137 |
-
* @param array $old_instance Previous widget settings.
|
138 |
-
* @param string $id_base Base widget type id.
|
139 |
-
* @return array Sanitized settings.
|
140 |
-
*/
|
141 |
-
public function sanitize_data( $instance, $new_instance, $old_instance, $id_base ) {
|
142 |
-
if ( 'simpleimage' == $id_base ) {
|
143 |
-
// Legacy image URL.
|
144 |
-
$instance['image'] = empty( $new_instance['image'] ) ? '' : esc_url_raw( $new_instance['image'] );
|
145 |
-
if ( empty( $instance['image'] ) ) {
|
146 |
-
unset( $instance['image'] );
|
147 |
-
}
|
148 |
-
|
149 |
-
// Legacy alt text.
|
150 |
-
$instance['alt'] = empty( $new_instance['alt'] ) ? '' : wp_strip_all_tags( $instance['alt'] );
|
151 |
-
if ( empty( $instance['alt'] ) ) {
|
152 |
-
unset( $instance['alt'] );
|
153 |
-
}
|
154 |
-
}
|
155 |
-
|
156 |
-
return $instance;
|
157 |
-
}
|
158 |
-
}
|
1 |
+
<?php
|
2 |
+
/**
|
3 |
+
* Legacy support.
|
4 |
+
*
|
5 |
+
* @package SimpleImageWidget
|
6 |
+
* @copyright Copyright (c) 2015 Cedaro, LLC
|
7 |
+
* @license GPL-2.0+
|
8 |
+
* @since 4.0.0
|
9 |
+
*/
|
10 |
+
|
11 |
+
/**
|
12 |
+
* Class to add support for features and data from previous versions.
|
13 |
+
*
|
14 |
+
* @package SimpleImageWidget
|
15 |
+
* @since 4.0.0
|
16 |
+
*/
|
17 |
+
class Simple_Image_Widget_Legacy {
|
18 |
+
/**
|
19 |
+
* Load legacy support.
|
20 |
+
*
|
21 |
+
* @since 4.0.0
|
22 |
+
*/
|
23 |
+
public function load() {
|
24 |
+
add_filter( 'simple_image_widget_output', array( $this, 'output' ), 10, 4 );
|
25 |
+
add_filter( 'simple_image_widget_fields', array( $this, 'fields' ), 10, 2 );
|
26 |
+
add_action( 'simple_image_widget_field-legacy', array( $this, 'display_fields' ), 10, 2 );
|
27 |
+
add_filter( 'simple_image_widget_instance', array( $this, 'sanitize_data' ), 10, 4 );
|
28 |
+
}
|
29 |
+
|
30 |
+
/**
|
31 |
+
* Legacy widget output.
|
32 |
+
*
|
33 |
+
* @since 4.0.0
|
34 |
+
*
|
35 |
+
* @param string $output HTML output.
|
36 |
+
* @param array $args Registered sidebar arguments including before_title, after_title, before_widget, and after_widget.
|
37 |
+
* @param array $instance The widget instance settings.
|
38 |
+
* @param string $id_base Base widget type id.
|
39 |
+
* @return string HTML output.
|
40 |
+
*/
|
41 |
+
public function output( $output, $args, $instance, $id_base ) {
|
42 |
+
if ( 'simpleimage' != $id_base || ! empty( $instance['image_id'] ) || empty( $instance['image'] ) ) {
|
43 |
+
return $output;
|
44 |
+
}
|
45 |
+
|
46 |
+
// Legacy output.
|
47 |
+
$output = ( empty( $instance['title'] ) ) ? '' : $args['before_title'] . $instance['title'] . $args['after_title'];
|
48 |
+
|
49 |
+
// Add the image.
|
50 |
+
$output = sprintf(
|
51 |
+
'%s<img src="%s" alt="%s">%s',
|
52 |
+
$instance['link_open'],
|
53 |
+
esc_url( $instance['image'] ),
|
54 |
+
( empty( $instance['alt'] ) ) ? '' : esc_attr( $instance['alt'] ),
|
55 |
+
$instance['link_close']
|
56 |
+
);
|
57 |
+
|
58 |
+
// Add the text.
|
59 |
+
if ( ! empty( $instance['text'] ) ) {
|
60 |
+
$output .= apply_filters( 'the_content', $instance['text'] );
|
61 |
+
}
|
62 |
+
|
63 |
+
// Add a more link.
|
64 |
+
if ( ! empty( $instance['link_open'] ) && ! empty( $instance['link_text'] ) ) {
|
65 |
+
$output .= '<p class="more">' . $instance['link_open'] . $instance['link_text'] . $instance['link_close'] . '</p>';
|
66 |
+
}
|
67 |
+
|
68 |
+
return $output;
|
69 |
+
}
|
70 |
+
|
71 |
+
/**
|
72 |
+
* Remove the image size field for versions of WordPress older than 3.5.
|
73 |
+
*
|
74 |
+
* @since 4.0.0
|
75 |
+
*
|
76 |
+
* @param array $fields List of field ids.
|
77 |
+
* @param string $id_base Base widget type id.
|
78 |
+
* @return array
|
79 |
+
*/
|
80 |
+
public function fields( $fields, $id_base ) {
|
81 |
+
if ( 'simpleimage' == $id_base && is_simple_image_widget_legacy() ) {
|
82 |
+
$key = array_search( 'image_size', $fields );
|
83 |
+
if ( false !== $key ) {
|
84 |
+
unset( $fields[ $key ] );
|
85 |
+
}
|
86 |
+
|
87 |
+
// Add a field for the old widget stuff.
|
88 |
+
array_unshift( $fields, 'legacy' );
|
89 |
+
}
|
90 |
+
|
91 |
+
return $fields;
|
92 |
+
}
|
93 |
+
|
94 |
+
/**
|
95 |
+
* Display legacy fields in the widget edit form.
|
96 |
+
*
|
97 |
+
* @since 4.0.0
|
98 |
+
*
|
99 |
+
* @param array $instance The widget instance settings.
|
100 |
+
* @param WP_Widget $widget Widget instance.
|
101 |
+
*/
|
102 |
+
public function display_fields( $instance, $widget ) {
|
103 |
+
if ( is_simple_image_widget_legacy() || ! empty( $instance['image'] ) ) :
|
104 |
+
?>
|
105 |
+
<div class="simple-image-widget-legacy-fields">
|
106 |
+
<?php if ( ! is_simple_image_widget_legacy() ) : ?>
|
107 |
+
<p>
|
108 |
+
<em><?php _e( 'These fields are here to maintain your data from an earlier version.', 'simple-image-widget' ); ?></em>
|
109 |
+
</p>
|
110 |
+
<p>
|
111 |
+
<em><?php _e( 'Select an image, then clear these values, and they will disappear when you save the widget.', 'simple-image-widget' ); ?></em>
|
112 |
+
</p>
|
113 |
+
<?php endif; ?>
|
114 |
+
|
115 |
+
<p>
|
116 |
+
<label for="<?php echo esc_attr( $widget->get_field_id( 'image' ) ); ?>"><?php _e( 'Image URL:', 'simple-image-widget' ); ?></label>
|
117 |
+
<input type="text" name="<?php echo esc_attr( $widget->get_field_name( 'image' ) ); ?>" id="<?php echo esc_attr( $widget->get_field_id( 'image' ) ); ?>" value="<?php echo esc_url( $instance['image'] ); ?>" class="widefat">
|
118 |
+
</p>
|
119 |
+
<p>
|
120 |
+
<label for="<?php echo esc_attr( $widget->get_field_id( 'alt' ) ); ?>"><?php _e( 'Alternate Text:', 'simple-image-widget' ); ?></label>
|
121 |
+
<input type="text" name="<?php echo esc_attr( $widget->get_field_name( 'alt' ) ); ?>" id="<?php echo esc_attr( $widget->get_field_id( 'alt' ) ); ?>" value="<?php echo esc_attr( $instance['alt'] ); ?>" class="widefat">
|
122 |
+
</p>
|
123 |
+
</div>
|
124 |
+
<?php
|
125 |
+
endif;
|
126 |
+
}
|
127 |
+
|
128 |
+
/**
|
129 |
+
* Sanitize legacy field values.
|
130 |
+
*
|
131 |
+
* Called in Simple_Image_Widget::update().
|
132 |
+
*
|
133 |
+
* @since 4.0.0
|
134 |
+
*
|
135 |
+
* @param array $instance Merged widget settings.
|
136 |
+
* @param array $new_instance New widget settings.
|
137 |
+
* @param array $old_instance Previous widget settings.
|
138 |
+
* @param string $id_base Base widget type id.
|
139 |
+
* @return array Sanitized settings.
|
140 |
+
*/
|
141 |
+
public function sanitize_data( $instance, $new_instance, $old_instance, $id_base ) {
|
142 |
+
if ( 'simpleimage' == $id_base ) {
|
143 |
+
// Legacy image URL.
|
144 |
+
$instance['image'] = empty( $new_instance['image'] ) ? '' : esc_url_raw( $new_instance['image'] );
|
145 |
+
if ( empty( $instance['image'] ) ) {
|
146 |
+
unset( $instance['image'] );
|
147 |
+
}
|
148 |
+
|
149 |
+
// Legacy alt text.
|
150 |
+
$instance['alt'] = empty( $new_instance['alt'] ) ? '' : wp_strip_all_tags( $instance['alt'] );
|
151 |
+
if ( empty( $instance['alt'] ) ) {
|
152 |
+
unset( $instance['alt'] );
|
153 |
+
}
|
154 |
+
}
|
155 |
+
|
156 |
+
return $instance;
|
157 |
+
}
|
158 |
+
}
|
includes/class-simple-image-widget-plugin.php
CHANGED
@@ -1,374 +1,374 @@
|
|
1 |
-
<?php
|
2 |
-
/**
|
3 |
-
* Simple Image Widget
|
4 |
-
*
|
5 |
-
* @package SimpleImageWidget
|
6 |
-
* @copyright Copyright (c) 2015 Cedaro, LLC
|
7 |
-
* @license GPL-2.0+
|
8 |
-
* @since 3.0.0
|
9 |
-
*/
|
10 |
-
|
11 |
-
/**
|
12 |
-
* The main plugin class for loading the widget and attaching hooks.
|
13 |
-
*
|
14 |
-
* @package SimpleImageWidget
|
15 |
-
* @since 3.0.0
|
16 |
-
*/
|
17 |
-
class Simple_Image_Widget_Plugin {
|
18 |
-
/**
|
19 |
-
* Set up the widget.
|
20 |
-
*
|
21 |
-
* @since 3.0.0
|
22 |
-
*/
|
23 |
-
public function load() {
|
24 |
-
self::load_textdomain();
|
25 |
-
add_action( 'widgets_init', array( $this, 'register_widget' ) );
|
26 |
-
|
27 |
-
$compat = new Simple_Image_Widget_Legacy();
|
28 |
-
$compat->load();
|
29 |
-
|
30 |
-
if ( is_simple_image_widget_legacy() ) {
|
31 |
-
return;
|
32 |
-
}
|
33 |
-
|
34 |
-
add_action( 'init', array( $this, 'register_assets' ) );
|
35 |
-
add_action( 'sidebar_admin_setup', array( $this, 'enqueue_admin_assets' ) );
|
36 |
-
add_filter( 'screen_settings', array( $this, 'widgets_screen_settings' ), 10, 2 );
|
37 |
-
add_action( 'wp_ajax_simple_image_widget_find_posts', array( $this, 'ajax_find_posts' ) );
|
38 |
-
add_action( 'wp_ajax_simple_image_widget_preferences', array( $this, 'ajax_save_user_preferences' ) );
|
39 |
-
}
|
40 |
-
|
41 |
-
/**
|
42 |
-
* Localize the plugin strings.
|
43 |
-
*
|
44 |
-
* @since 3.0.0
|
45 |
-
*/
|
46 |
-
public function load_textdomain() {
|
47 |
-
load_plugin_textdomain( 'simple-image-widget' );
|
48 |
-
}
|
49 |
-
|
50 |
-
/**
|
51 |
-
* Register the image widget.
|
52 |
-
*
|
53 |
-
* @since 3.0.0
|
54 |
-
*/
|
55 |
-
public function register_widget() {
|
56 |
-
register_widget( 'Simple_Image_Widget' );
|
57 |
-
}
|
58 |
-
|
59 |
-
/**
|
60 |
-
* Register and localize generic scripts and styles.
|
61 |
-
*
|
62 |
-
* A preliminary attempt has been made to abstract the
|
63 |
-
* 'simple-image-widget-control' script a bit in order to allow it to be
|
64 |
-
* re-used anywhere a similiar media selection feature is needed.
|
65 |
-
*
|
66 |
-
* Custom image size labels need to be added using the
|
67 |
-
* 'image_size_names_choose' filter.
|
68 |
-
*
|
69 |
-
* @since 3.0.0
|
70 |
-
*/
|
71 |
-
public function register_assets() {
|
72 |
-
wp_register_style(
|
73 |
-
'simple-image-widget-admin',
|
74 |
-
dirname( plugin_dir_url( __FILE__ ) ) . '/assets/css/simple-image-widget.css'
|
75 |
-
);
|
76 |
-
|
77 |
-
wp_register_script(
|
78 |
-
'simple-image-widget-admin',
|
79 |
-
dirname( plugin_dir_url( __FILE__ ) ) . '/assets/js/simple-image-widget.js',
|
80 |
-
array( 'media-upload', 'media-views', 'wp-backbone', 'wp-util' )
|
81 |
-
);
|
82 |
-
|
83 |
-
wp_localize_script(
|
84 |
-
'simple-image-widget-admin',
|
85 |
-
'SimpleImageWidget',
|
86 |
-
array(
|
87 |
-
'l10n' => array(
|
88 |
-
'frameTitle' => __( 'Choose an Attachment', 'simple-image-widget' ),
|
89 |
-
'frameUpdateText' => __( 'Update Attachment', 'simple-image-widget' ),
|
90 |
-
'fullSizeLabel' => __( 'Full Size', 'simple-image-widget' ),
|
91 |
-
'imageSizeNames' => self::get_image_size_names(),
|
92 |
-
'responseError' => __( 'An error has occurred. Please reload the page and try again.', 'simple-image-widget' ),
|
93 |
-
),
|
94 |
-
'screenOptionsNonce' => wp_create_nonce( 'save-siw-preferences' ),
|
95 |
-
)
|
96 |
-
);
|
97 |
-
|
98 |
-
add_action( 'customize_controls_print_footer_scripts', array( $this, 'print_find_posts_templates' ) );
|
99 |
-
add_action( 'admin_footer', array( $this, 'print_find_posts_templates' ) );
|
100 |
-
}
|
101 |
-
|
102 |
-
/**
|
103 |
-
* Add checkboxes to the screen options tab on the Widgets screen for
|
104 |
-
* togglable fields.
|
105 |
-
*
|
106 |
-
* @since 4.1.0
|
107 |
-
*
|
108 |
-
* @param string $settings Screen options output.
|
109 |
-
* @param WP_Screen $screen Current screen.
|
110 |
-
* @return string
|
111 |
-
*/
|
112 |
-
public function widgets_screen_settings( $settings, $screen ) {
|
113 |
-
if ( 'widgets' !== $screen->id ) {
|
114 |
-
return $settings;
|
115 |
-
}
|
116 |
-
|
117 |
-
$settings .= sprintf( '<h5>%s</h5>', __( 'Simple Image Widget', 'simple-image-widget' ) );
|
118 |
-
|
119 |
-
$fields = array(
|
120 |
-
'image_size' => __( 'Image Size', 'simple-image-widget' ),
|
121 |
-
'link' => __( 'Link', 'simple-image-widget' ),
|
122 |
-
'link_classes' => __( 'Link Classes', 'simple-image-widget' ),
|
123 |
-
'link_text' => __( 'Link Text', 'simple-image-widget' ),
|
124 |
-
'new_window' => __( 'New Window', 'simple-image-widget' ),
|
125 |
-
'text' => __( 'Text', 'simple-image-widget' ),
|
126 |
-
);
|
127 |
-
|
128 |
-
/**
|
129 |
-
* List of hideable fields.
|
130 |
-
*
|
131 |
-
* @since 4.1.0
|
132 |
-
*
|
133 |
-
* @param array $fields List of fields with ids as keys and labels as values.
|
134 |
-
*/
|
135 |
-
$fields = apply_filters( 'simple_image_widget_hideable_fields', $fields );
|
136 |
-
$hidden_fields = $this->get_hidden_fields();
|
137 |
-
|
138 |
-
foreach ( $fields as $id => $label ) {
|
139 |
-
$settings .= sprintf(
|
140 |
-
'<label><input type="checkbox" value="%1$s"%2$s class="simple-image-widget-field-toggle"> %3$s</label>',
|
141 |
-
esc_attr( $id ),
|
142 |
-
checked( in_array( $id, $hidden_fields ), false, false ),
|
143 |
-
esc_html( $label )
|
144 |
-
);
|
145 |
-
}
|
146 |
-
|
147 |
-
return $settings;
|
148 |
-
}
|
149 |
-
|
150 |
-
/**
|
151 |
-
* Enqueue scripts needed for selecting media.
|
152 |
-
*
|
153 |
-
* @since 3.0.0
|
154 |
-
*
|
155 |
-
* @param string $hook_suffix Screen id.
|
156 |
-
*/
|
157 |
-
public function enqueue_admin_assets() {
|
158 |
-
wp_enqueue_media();
|
159 |
-
wp_enqueue_script( 'simple-image-widget-admin' );
|
160 |
-
wp_enqueue_script( 'simple-image-widget-find-posts' );
|
161 |
-
wp_enqueue_style( 'simple-image-widget-admin' );
|
162 |
-
}
|
163 |
-
|
164 |
-
/**
|
165 |
-
* Get localized image size names.
|
166 |
-
*
|
167 |
-
* The 'image_size_names_choose' filter exists in core and should be
|
168 |
-
* hooked by plugin authors to provide localized labels for custom image
|
169 |
-
* sizes added using add_image_size().
|
170 |
-
*
|
171 |
-
* @see image_size_input_fields()
|
172 |
-
* @see http://core.trac.wordpress.org/ticket/20663
|
173 |
-
*
|
174 |
-
* @since 3.0.0
|
175 |
-
*
|
176 |
-
* @return array Array of thumbnail sizes.
|
177 |
-
*/
|
178 |
-
public static function get_image_size_names() {
|
179 |
-
return apply_filters(
|
180 |
-
'image_size_names_choose',
|
181 |
-
array(
|
182 |
-
'thumbnail' => __( 'Thumbnail', 'simple-image-widget' ),
|
183 |
-
'medium' => __( 'Medium', 'simple-image-widget' ),
|
184 |
-
'large' => __( 'Large', 'simple-image-widget' ),
|
185 |
-
'full' => __( 'Full Size', 'simple-image-widget' ),
|
186 |
-
)
|
187 |
-
);
|
188 |
-
}
|
189 |
-
|
190 |
-
/**
|
191 |
-
* Retrieve a list of hidden fields.
|
192 |
-
*
|
193 |
-
* @since 4.1.0
|
194 |
-
*
|
195 |
-
* @return array List of field ids.
|
196 |
-
*/
|
197 |
-
public static function get_hidden_fields() {
|
198 |
-
$hidden_fields = get_user_option( 'siw_hidden_fields', get_current_user_id() );
|
199 |
-
|
200 |
-
// Fields that are hidden by default.
|
201 |
-
if ( false === $hidden_fields ) {
|
202 |
-
$hidden_fields = array( 'link_classes' );
|
203 |
-
}
|
204 |
-
|
205 |
-
/**
|
206 |
-
* List of hidden field ids.
|
207 |
-
*
|
208 |
-
* @since 4.1.0
|
209 |
-
*
|
210 |
-
* @param array $hidden_fields List of hidden field ids.
|
211 |
-
*/
|
212 |
-
return (array) apply_filters( 'simple_image_widget_hidden_fields', $hidden_fields );
|
213 |
-
}
|
214 |
-
|
215 |
-
/**
|
216 |
-
* Ajax handler for finding posts.
|
217 |
-
*
|
218 |
-
* @since 4.2.0
|
219 |
-
*
|
220 |
-
* @see wp_ajax_find_posts()
|
221 |
-
*/
|
222 |
-
public function ajax_find_posts() {
|
223 |
-
check_ajax_referer( 'siw-find-posts', 'nonce' );
|
224 |
-
|
225 |
-
$post_types = array();
|
226 |
-
|
227 |
-
if ( ! empty( $_POST['post_types'] ) ) {
|
228 |
-
foreach ( $_POST['post_types'] as $post_type ) {
|
229 |
-
$post_types[ $post_type ] = get_post_type_object( $post_type );
|
230 |
-
}
|
231 |
-
}
|
232 |
-
|
233 |
-
if ( empty( $post_types ) ) {
|
234 |
-
$post_types['post'] = get_post_type_object( 'post' );
|
235 |
-
}
|
236 |
-
|
237 |
-
$args = array(
|
238 |
-
'post_type' => array_keys( $post_types ),
|
239 |
-
'post_status' => 'any',
|
240 |
-
'posts_per_page' => 50,
|
241 |
-
);
|
242 |
-
|
243 |
-
if ( ! empty( $_POST['s'] ) ) {
|
244 |
-
$args['s'] = wp_unslash( $_POST['s'] );
|
245 |
-
}
|
246 |
-
|
247 |
-
$posts = get_posts( $args );
|
248 |
-
|
249 |
-
if ( ! $posts ) {
|
250 |
-
wp_send_json_error( __( 'No items found.', 'simple-image-widget' ) );
|
251 |
-
}
|
252 |
-
|
253 |
-
$html = $this->get_found_posts_html( $posts );
|
254 |
-
|
255 |
-
wp_send_json_success( $html );
|
256 |
-
}
|
257 |
-
|
258 |
-
/**
|
259 |
-
* AJAX callback to save the user's hidden fields.
|
260 |
-
*
|
261 |
-
* @since 4.1.0
|
262 |
-
*/
|
263 |
-
public function ajax_save_user_preferences() {
|
264 |
-
$nonce_action = 'save-siw-preferences';
|
265 |
-
check_ajax_referer( $nonce_action, 'nonce' );
|
266 |
-
$data = array( 'nonce' => wp_create_nonce( $nonce_action ) );
|
267 |
-
|
268 |
-
if ( ! $user = wp_get_current_user() ) {
|
269 |
-
wp_send_json_error( $data );
|
270 |
-
}
|
271 |
-
|
272 |
-
$hidden = isset( $_POST['hidden'] ) ? explode( ',', $_POST['hidden'] ) : array();
|
273 |
-
if ( is_array( $hidden ) ) {
|
274 |
-
update_user_option( $user->ID, 'siw_hidden_fields', $hidden );
|
275 |
-
}
|
276 |
-
|
277 |
-
wp_send_json_success( $data );
|
278 |
-
}
|
279 |
-
|
280 |
-
/**
|
281 |
-
* Retrieve HTML for displaying a list of posts.
|
282 |
-
*
|
283 |
-
* @since 4.2.0
|
284 |
-
*
|
285 |
-
* @param array $posts Array of post objects.
|
286 |
-
* @return string
|
287 |
-
*/
|
288 |
-
protected function get_found_posts_html( $posts ) {
|
289 |
-
$html = sprintf(
|
290 |
-
'<table class="widefat"><thead><tr><th>%1$s</th><th class="no-break">%2$s</th><th class="no-break">%3$s</th><th class="no-break">%4$s</th></tr></thead><tbody>',
|
291 |
-
__( 'Title', 'simple-image-widget' ),
|
292 |
-
__( 'Type', 'simple-image-widget' ),
|
293 |
-
__( 'Date', 'simple-image-widget' ),
|
294 |
-
__( 'Status', 'simple-image-widget' )
|
295 |
-
);
|
296 |
-
|
297 |
-
foreach ( $posts as $post ) {
|
298 |
-
$title = trim( $post->post_title ) ? $post->post_title : __( '(no title)' );
|
299 |
-
$post_link = 'attachment' == get_post_type( $post->ID ) ? wp_get_attachment_url( $post->ID ) : get_permalink( $post->ID );
|
300 |
-
$status = '';
|
301 |
-
|
302 |
-
switch ( $post->post_status ) {
|
303 |
-
case 'publish' :
|
304 |
-
case 'private' :
|
305 |
-
$status = __( 'Published', 'simple-image-widget' );
|
306 |
-
break;
|
307 |
-
case 'future' :
|
308 |
-
$status = __( 'Scheduled', 'simple-image-widget' );
|
309 |
-
break;
|
310 |
-
case 'pending' :
|
311 |
-
$status = __( 'Pending Review', 'simple-image-widget' );
|
312 |
-
break;
|
313 |
-
case 'draft' :
|
314 |
-
$status = __( 'Draft', 'simple-image-widget' );
|
315 |
-
break;
|
316 |
-
}
|
317 |
-
|
318 |
-
if ( '0000-00-00 00:00:00' == $post->post_date ) {
|
319 |
-
$time = '';
|
320 |
-
} else {
|
321 |
-
/* translators: date format in table columns, see http://php.net/date */
|
322 |
-
$time = mysql2date( __( 'Y/m/d' ), $post->post_date );
|
323 |
-
}
|
324 |
-
|
325 |
-
$post_type_label = get_post_type_object( $post->post_type )->labels->singular_name;
|
326 |
-
if ( 'attachment' == get_post_type( $post->ID ) ) {
|
327 |
-
if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) ) {
|
328 |
-
$post_type_label = esc_html( strtoupper( $matches[1] ) );
|
329 |
-
} else {
|
330 |
-
$post_type_label = strtoupper( str_replace( 'image/', '', get_post_mime_type( $post->ID ) ) );
|
331 |
-
}
|
332 |
-
}
|
333 |
-
|
334 |
-
$html .= sprintf(
|
335 |
-
'<tr class="found-posts"><td>%1$s <input type="hidden" value="%2$s"></td><td class="no-break">%3$s</td><td class="no-break">%4$s</td><td class="no-break">%5$s</td></tr>',
|
336 |
-
esc_html( $title ),
|
337 |
-
esc_url( apply_filters( 'simple_image_widget_find_posts_post_link', $post_link ), $post->ID ),
|
338 |
-
esc_html( $post_type_label ),
|
339 |
-
esc_html( $time ),
|
340 |
-
esc_html( $status )
|
341 |
-
);
|
342 |
-
}
|
343 |
-
|
344 |
-
$html .= '</tbody></table>';
|
345 |
-
|
346 |
-
return $html;
|
347 |
-
}
|
348 |
-
|
349 |
-
/**
|
350 |
-
* Print JavaScript templates in the Customizer footer.
|
351 |
-
*
|
352 |
-
* @since 4.2.0
|
353 |
-
*/
|
354 |
-
public function print_find_posts_templates() {
|
355 |
-
?>
|
356 |
-
<script type="text/html" id="tmpl-simple-image-widget-modal">
|
357 |
-
<div class="simple-image-widget-modal-head find-box-head">
|
358 |
-
<?php _e( 'Find Post', 'simple-image-widget' ); ?>
|
359 |
-
<div class="simple-image-widget-modal-close js-close"></div>
|
360 |
-
</div>
|
361 |
-
<div class="simple-image-widget-modal-inside find-box-inside">
|
362 |
-
<div class="simple-image-widget-modal-search find-box-search">
|
363 |
-
<?php wp_nonce_field( 'siw-find-posts', 'siw-find-posts-ajax-nonce', false ); ?>
|
364 |
-
<input type="text" name="s" value="" class="simple-image-widget-modal-search-field">
|
365 |
-
<span class="spinner"></span>
|
366 |
-
<input type="button" value="<?php esc_attr_e( 'Search', 'simple-image-widget' ); ?>" class="button simple-image-widget-modal-search-button" />
|
367 |
-
<div class="clear"></div>
|
368 |
-
</div>
|
369 |
-
<div class="simple-image-widget-modal-response"></div>
|
370 |
-
</div>
|
371 |
-
</script>
|
372 |
-
<?php
|
373 |
-
}
|
374 |
-
}
|
1 |
+
<?php
|
2 |
+
/**
|
3 |
+
* Simple Image Widget
|
4 |
+
*
|
5 |
+
* @package SimpleImageWidget
|
6 |
+
* @copyright Copyright (c) 2015 Cedaro, LLC
|
7 |
+
* @license GPL-2.0+
|
8 |
+
* @since 3.0.0
|
9 |
+
*/
|
10 |
+
|
11 |
+
/**
|
12 |
+
* The main plugin class for loading the widget and attaching hooks.
|
13 |
+
*
|
14 |
+
* @package SimpleImageWidget
|
15 |
+
* @since 3.0.0
|
16 |
+
*/
|
17 |
+
class Simple_Image_Widget_Plugin {
|
18 |
+
/**
|
19 |
+
* Set up the widget.
|
20 |
+
*
|
21 |
+
* @since 3.0.0
|
22 |
+
*/
|
23 |
+
public function load() {
|
24 |
+
self::load_textdomain();
|
25 |
+
add_action( 'widgets_init', array( $this, 'register_widget' ) );
|
26 |
+
|
27 |
+
$compat = new Simple_Image_Widget_Legacy();
|
28 |
+
$compat->load();
|
29 |
+
|
30 |
+
if ( is_simple_image_widget_legacy() ) {
|
31 |
+
return;
|
32 |
+
}
|
33 |
+
|
34 |
+
add_action( 'init', array( $this, 'register_assets' ) );
|
35 |
+
add_action( 'sidebar_admin_setup', array( $this, 'enqueue_admin_assets' ) );
|
36 |
+
add_filter( 'screen_settings', array( $this, 'widgets_screen_settings' ), 10, 2 );
|
37 |
+
add_action( 'wp_ajax_simple_image_widget_find_posts', array( $this, 'ajax_find_posts' ) );
|
38 |
+
add_action( 'wp_ajax_simple_image_widget_preferences', array( $this, 'ajax_save_user_preferences' ) );
|
39 |
+
}
|
40 |
+
|
41 |
+
/**
|
42 |
+
* Localize the plugin strings.
|
43 |
+
*
|
44 |
+
* @since 3.0.0
|
45 |
+
*/
|
46 |
+
public function load_textdomain() {
|
47 |
+
load_plugin_textdomain( 'simple-image-widget' );
|
48 |
+
}
|
49 |
+
|
50 |
+
/**
|
51 |
+
* Register the image widget.
|
52 |
+
*
|
53 |
+
* @since 3.0.0
|
54 |
+
*/
|
55 |
+
public function register_widget() {
|
56 |
+
register_widget( 'Simple_Image_Widget' );
|
57 |
+
}
|
58 |
+
|
59 |
+
/**
|
60 |
+
* Register and localize generic scripts and styles.
|
61 |
+
*
|
62 |
+
* A preliminary attempt has been made to abstract the
|
63 |
+
* 'simple-image-widget-control' script a bit in order to allow it to be
|
64 |
+
* re-used anywhere a similiar media selection feature is needed.
|
65 |
+
*
|
66 |
+
* Custom image size labels need to be added using the
|
67 |
+
* 'image_size_names_choose' filter.
|
68 |
+
*
|
69 |
+
* @since 3.0.0
|
70 |
+
*/
|
71 |
+
public function register_assets() {
|
72 |
+
wp_register_style(
|
73 |
+
'simple-image-widget-admin',
|
74 |
+
dirname( plugin_dir_url( __FILE__ ) ) . '/assets/css/simple-image-widget.css'
|
75 |
+
);
|
76 |
+
|
77 |
+
wp_register_script(
|
78 |
+
'simple-image-widget-admin',
|
79 |
+
dirname( plugin_dir_url( __FILE__ ) ) . '/assets/js/simple-image-widget.js',
|
80 |
+
array( 'media-upload', 'media-views', 'wp-backbone', 'wp-util' )
|
81 |
+
);
|
82 |
+
|
83 |
+
wp_localize_script(
|
84 |
+
'simple-image-widget-admin',
|
85 |
+
'SimpleImageWidget',
|
86 |
+
array(
|
87 |
+
'l10n' => array(
|
88 |
+
'frameTitle' => __( 'Choose an Attachment', 'simple-image-widget' ),
|
89 |
+
'frameUpdateText' => __( 'Update Attachment', 'simple-image-widget' ),
|
90 |
+
'fullSizeLabel' => __( 'Full Size', 'simple-image-widget' ),
|
91 |
+
'imageSizeNames' => self::get_image_size_names(),
|
92 |
+
'responseError' => __( 'An error has occurred. Please reload the page and try again.', 'simple-image-widget' ),
|
93 |
+
),
|
94 |
+
'screenOptionsNonce' => wp_create_nonce( 'save-siw-preferences' ),
|
95 |
+
)
|
96 |
+
);
|
97 |
+
|
98 |
+
add_action( 'customize_controls_print_footer_scripts', array( $this, 'print_find_posts_templates' ) );
|
99 |
+
add_action( 'admin_footer', array( $this, 'print_find_posts_templates' ) );
|
100 |
+
}
|
101 |
+
|
102 |
+
/**
|
103 |
+
* Add checkboxes to the screen options tab on the Widgets screen for
|
104 |
+
* togglable fields.
|
105 |
+
*
|
106 |
+
* @since 4.1.0
|
107 |
+
*
|
108 |
+
* @param string $settings Screen options output.
|
109 |
+
* @param WP_Screen $screen Current screen.
|
110 |
+
* @return string
|
111 |
+
*/
|
112 |
+
public function widgets_screen_settings( $settings, $screen ) {
|
113 |
+
if ( 'widgets' !== $screen->id ) {
|
114 |
+
return $settings;
|
115 |
+
}
|
116 |
+
|
117 |
+
$settings .= sprintf( '<h5>%s</h5>', __( 'Simple Image Widget', 'simple-image-widget' ) );
|
118 |
+
|
119 |
+
$fields = array(
|
120 |
+
'image_size' => __( 'Image Size', 'simple-image-widget' ),
|
121 |
+
'link' => __( 'Link', 'simple-image-widget' ),
|
122 |
+
'link_classes' => __( 'Link Classes', 'simple-image-widget' ),
|
123 |
+
'link_text' => __( 'Link Text', 'simple-image-widget' ),
|
124 |
+
'new_window' => __( 'New Window', 'simple-image-widget' ),
|
125 |
+
'text' => __( 'Text', 'simple-image-widget' ),
|
126 |
+
);
|
127 |
+
|
128 |
+
/**
|
129 |
+
* List of hideable fields.
|
130 |
+
*
|
131 |
+
* @since 4.1.0
|
132 |
+
*
|
133 |
+
* @param array $fields List of fields with ids as keys and labels as values.
|
134 |
+
*/
|
135 |
+
$fields = apply_filters( 'simple_image_widget_hideable_fields', $fields );
|
136 |
+
$hidden_fields = $this->get_hidden_fields();
|
137 |
+
|
138 |
+
foreach ( $fields as $id => $label ) {
|
139 |
+
$settings .= sprintf(
|
140 |
+
'<label><input type="checkbox" value="%1$s"%2$s class="simple-image-widget-field-toggle"> %3$s</label>',
|
141 |
+
esc_attr( $id ),
|
142 |
+
checked( in_array( $id, $hidden_fields ), false, false ),
|
143 |
+
esc_html( $label )
|
144 |
+
);
|
145 |
+
}
|
146 |
+
|
147 |
+
return $settings;
|
148 |
+
}
|
149 |
+
|
150 |
+
/**
|
151 |
+
* Enqueue scripts needed for selecting media.
|
152 |
+
*
|
153 |
+
* @since 3.0.0
|
154 |
+
*
|
155 |
+
* @param string $hook_suffix Screen id.
|
156 |
+
*/
|
157 |
+
public function enqueue_admin_assets() {
|
158 |
+
wp_enqueue_media();
|
159 |
+
wp_enqueue_script( 'simple-image-widget-admin' );
|
160 |
+
wp_enqueue_script( 'simple-image-widget-find-posts' );
|
161 |
+
wp_enqueue_style( 'simple-image-widget-admin' );
|
162 |
+
}
|
163 |
+
|
164 |
+
/**
|
165 |
+
* Get localized image size names.
|
166 |
+
*
|
167 |
+
* The 'image_size_names_choose' filter exists in core and should be
|
168 |
+
* hooked by plugin authors to provide localized labels for custom image
|
169 |
+
* sizes added using add_image_size().
|
170 |
+
*
|
171 |
+
* @see image_size_input_fields()
|
172 |
+
* @see http://core.trac.wordpress.org/ticket/20663
|
173 |
+
*
|
174 |
+
* @since 3.0.0
|
175 |
+
*
|
176 |
+
* @return array Array of thumbnail sizes.
|
177 |
+
*/
|
178 |
+
public static function get_image_size_names() {
|
179 |
+
return apply_filters(
|
180 |
+
'image_size_names_choose',
|
181 |
+
array(
|
182 |
+
'thumbnail' => __( 'Thumbnail', 'simple-image-widget' ),
|
183 |
+
'medium' => __( 'Medium', 'simple-image-widget' ),
|
184 |
+
'large' => __( 'Large', 'simple-image-widget' ),
|
185 |
+
'full' => __( 'Full Size', 'simple-image-widget' ),
|
186 |
+
)
|
187 |
+
);
|
188 |
+
}
|
189 |
+
|
190 |
+
/**
|
191 |
+
* Retrieve a list of hidden fields.
|
192 |
+
*
|
193 |
+
* @since 4.1.0
|
194 |
+
*
|
195 |
+
* @return array List of field ids.
|
196 |
+
*/
|
197 |
+
public static function get_hidden_fields() {
|
198 |
+
$hidden_fields = get_user_option( 'siw_hidden_fields', get_current_user_id() );
|
199 |
+
|
200 |
+
// Fields that are hidden by default.
|
201 |
+
if ( false === $hidden_fields ) {
|
202 |
+
$hidden_fields = array( 'link_classes' );
|
203 |
+
}
|
204 |
+
|
205 |
+
/**
|
206 |
+
* List of hidden field ids.
|
207 |
+
*
|
208 |
+
* @since 4.1.0
|
209 |
+
*
|
210 |
+
* @param array $hidden_fields List of hidden field ids.
|
211 |
+
*/
|
212 |
+
return (array) apply_filters( 'simple_image_widget_hidden_fields', $hidden_fields );
|
213 |
+
}
|
214 |
+
|
215 |
+
/**
|
216 |
+
* Ajax handler for finding posts.
|
217 |
+
*
|
218 |
+
* @since 4.2.0
|
219 |
+
*
|
220 |
+
* @see wp_ajax_find_posts()
|
221 |
+
*/
|
222 |
+
public function ajax_find_posts() {
|
223 |
+
check_ajax_referer( 'siw-find-posts', 'nonce' );
|
224 |
+
|
225 |
+
$post_types = array();
|
226 |
+
|
227 |
+
if ( ! empty( $_POST['post_types'] ) ) {
|
228 |
+
foreach ( $_POST['post_types'] as $post_type ) {
|
229 |
+
$post_types[ $post_type ] = get_post_type_object( $post_type );
|
230 |
+
}
|
231 |
+
}
|
232 |
+
|
233 |
+
if ( empty( $post_types ) ) {
|
234 |
+
$post_types['post'] = get_post_type_object( 'post' );
|
235 |
+
}
|
236 |
+
|
237 |
+
$args = array(
|
238 |
+
'post_type' => array_keys( $post_types ),
|
239 |
+
'post_status' => 'any',
|
240 |
+
'posts_per_page' => 50,
|
241 |
+
);
|
242 |
+
|
243 |
+
if ( ! empty( $_POST['s'] ) ) {
|
244 |
+
$args['s'] = wp_unslash( $_POST['s'] );
|
245 |
+
}
|
246 |
+
|
247 |
+
$posts = get_posts( $args );
|
248 |
+
|
249 |
+
if ( ! $posts ) {
|
250 |
+
wp_send_json_error( __( 'No items found.', 'simple-image-widget' ) );
|
251 |
+
}
|
252 |
+
|
253 |
+
$html = $this->get_found_posts_html( $posts );
|
254 |
+
|
255 |
+
wp_send_json_success( $html );
|
256 |
+
}
|
257 |
+
|
258 |
+
/**
|
259 |
+
* AJAX callback to save the user's hidden fields.
|
260 |
+
*
|
261 |
+
* @since 4.1.0
|
262 |
+
*/
|
263 |
+
public function ajax_save_user_preferences() {
|
264 |
+
$nonce_action = 'save-siw-preferences';
|
265 |
+
check_ajax_referer( $nonce_action, 'nonce' );
|
266 |
+
$data = array( 'nonce' => wp_create_nonce( $nonce_action ) );
|
267 |
+
|
268 |
+
if ( ! $user = wp_get_current_user() ) {
|
269 |
+
wp_send_json_error( $data );
|
270 |
+
}
|
271 |
+
|
272 |
+
$hidden = isset( $_POST['hidden'] ) ? explode( ',', $_POST['hidden'] ) : array();
|
273 |
+
if ( is_array( $hidden ) ) {
|
274 |
+
update_user_option( $user->ID, 'siw_hidden_fields', $hidden );
|
275 |
+
}
|
276 |
+
|
277 |
+
wp_send_json_success( $data );
|
278 |
+
}
|
279 |
+
|
280 |
+
/**
|
281 |
+
* Retrieve HTML for displaying a list of posts.
|
282 |
+
*
|
283 |
+
* @since 4.2.0
|
284 |
+
*
|
285 |
+
* @param array $posts Array of post objects.
|
286 |
+
* @return string
|
287 |
+
*/
|
288 |
+
protected function get_found_posts_html( $posts ) {
|
289 |
+
$html = sprintf(
|
290 |
+
'<table class="widefat"><thead><tr><th>%1$s</th><th class="no-break">%2$s</th><th class="no-break">%3$s</th><th class="no-break">%4$s</th></tr></thead><tbody>',
|
291 |
+
__( 'Title', 'simple-image-widget' ),
|
292 |
+
__( 'Type', 'simple-image-widget' ),
|
293 |
+
__( 'Date', 'simple-image-widget' ),
|
294 |
+
__( 'Status', 'simple-image-widget' )
|
295 |
+
);
|
296 |
+
|
297 |
+
foreach ( $posts as $post ) {
|
298 |
+
$title = trim( $post->post_title ) ? $post->post_title : __( '(no title)', 'simple-image-widget' );
|
299 |
+
$post_link = 'attachment' == get_post_type( $post->ID ) ? wp_get_attachment_url( $post->ID ) : get_permalink( $post->ID );
|
300 |
+
$status = '';
|
301 |
+
|
302 |
+
switch ( $post->post_status ) {
|
303 |
+
case 'publish' :
|
304 |
+
case 'private' :
|
305 |
+
$status = __( 'Published', 'simple-image-widget' );
|
306 |
+
break;
|
307 |
+
case 'future' :
|
308 |
+
$status = __( 'Scheduled', 'simple-image-widget' );
|
309 |
+
break;
|
310 |
+
case 'pending' :
|
311 |
+
$status = __( 'Pending Review', 'simple-image-widget' );
|
312 |
+
break;
|
313 |
+
case 'draft' :
|
314 |
+
$status = __( 'Draft', 'simple-image-widget' );
|
315 |
+
break;
|
316 |
+
}
|
317 |
+
|
318 |
+
if ( '0000-00-00 00:00:00' == $post->post_date ) {
|
319 |
+
$time = '';
|
320 |
+
} else {
|
321 |
+
/* translators: date format in table columns, see http://php.net/date */
|
322 |
+
$time = mysql2date( __( 'Y/m/d', 'simple-image-widget' ), $post->post_date );
|
323 |
+
}
|
324 |
+
|
325 |
+
$post_type_label = get_post_type_object( $post->post_type )->labels->singular_name;
|
326 |
+
if ( 'attachment' == get_post_type( $post->ID ) ) {
|
327 |
+
if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) ) {
|
328 |
+
$post_type_label = esc_html( strtoupper( $matches[1] ) );
|
329 |
+
} else {
|
330 |
+
$post_type_label = strtoupper( str_replace( 'image/', '', get_post_mime_type( $post->ID ) ) );
|
331 |
+
}
|
332 |
+
}
|
333 |
+
|
334 |
+
$html .= sprintf(
|
335 |
+
'<tr class="found-posts"><td>%1$s <input type="hidden" value="%2$s"></td><td class="no-break">%3$s</td><td class="no-break">%4$s</td><td class="no-break">%5$s</td></tr>',
|
336 |
+
esc_html( $title ),
|
337 |
+
esc_url( apply_filters( 'simple_image_widget_find_posts_post_link', $post_link ), $post->ID ),
|
338 |
+
esc_html( $post_type_label ),
|
339 |
+
esc_html( $time ),
|
340 |
+
esc_html( $status )
|
341 |
+
);
|
342 |
+
}
|
343 |
+
|
344 |
+
$html .= '</tbody></table>';
|
345 |
+
|
346 |
+
return $html;
|
347 |
+
}
|
348 |
+
|
349 |
+
/**
|
350 |
+
* Print JavaScript templates in the Customizer footer.
|
351 |
+
*
|
352 |
+
* @since 4.2.0
|
353 |
+
*/
|
354 |
+
public function print_find_posts_templates() {
|
355 |
+
?>
|
356 |
+
<script type="text/html" id="tmpl-simple-image-widget-modal">
|
357 |
+
<div class="simple-image-widget-modal-head find-box-head">
|
358 |
+
<?php _e( 'Find Post', 'simple-image-widget' ); ?>
|
359 |
+
<div class="simple-image-widget-modal-close js-close"></div>
|
360 |
+
</div>
|
361 |
+
<div class="simple-image-widget-modal-inside find-box-inside">
|
362 |
+
<div class="simple-image-widget-modal-search find-box-search">
|
363 |
+
<?php wp_nonce_field( 'siw-find-posts', 'siw-find-posts-ajax-nonce', false ); ?>
|
364 |
+
<input type="text" name="s" value="" class="simple-image-widget-modal-search-field">
|
365 |
+
<span class="spinner"></span>
|
366 |
+
<input type="button" value="<?php esc_attr_e( 'Search', 'simple-image-widget' ); ?>" class="button simple-image-widget-modal-search-button" />
|
367 |
+
<div class="clear"></div>
|
368 |
+
</div>
|
369 |
+
<div class="simple-image-widget-modal-response"></div>
|
370 |
+
</div>
|
371 |
+
</script>
|
372 |
+
<?php
|
373 |
+
}
|
374 |
+
}
|
includes/class-simple-image-widget-template-loader.php
CHANGED
@@ -1,265 +1,265 @@
|
|
1 |
-
<?php
|
2 |
-
/**
|
3 |
-
* Template loader.
|
4 |
-
*
|
5 |
-
* Based off version 1.1.0 of the Gamajo Template Loader by Gary Jones. Changes
|
6 |
-
* allow for overriding class properties during instantiation and adding a
|
7 |
-
* load_template() method that accepts arbitrary data to extra into the local
|
8 |
-
* template scope.
|
9 |
-
*
|
10 |
-
* @package SimpleImageWidget
|
11 |
-
* @since 4.0.0
|
12 |
-
* @author Gary Jones
|
13 |
-
* @link http://github.com/GaryJones/Gamajo-Template-Loader
|
14 |
-
* @copyright 2013 Gary Jones
|
15 |
-
* @license GPL-2.0+
|
16 |
-
*/
|
17 |
-
|
18 |
-
/**
|
19 |
-
* Template loader.
|
20 |
-
*
|
21 |
-
* @package SimpleImageWidget
|
22 |
-
* @since 4.0.0
|
23 |
-
* @author Gary Jones
|
24 |
-
*/
|
25 |
-
class Simple_Image_Widget_Template_Loader {
|
26 |
-
/**
|
27 |
-
* Prefix for filter names.
|
28 |
-
*
|
29 |
-
* @since 4.0.0
|
30 |
-
*
|
31 |
-
* @type string
|
32 |
-
*/
|
33 |
-
protected $filter_prefix = 'simple_image_widget';
|
34 |
-
|
35 |
-
/**
|
36 |
-
* Directory name where custom templates for this plugin should be found in
|
37 |
-
* the theme.
|
38 |
-
*
|
39 |
-
* @since 4.0.0
|
40 |
-
*
|
41 |
-
* @type string
|
42 |
-
*/
|
43 |
-
protected $theme_template_directory = 'simple-image-widget';
|
44 |
-
|
45 |
-
/**
|
46 |
-
* Reference to the root directory path of this plugin.
|
47 |
-
*
|
48 |
-
* @since 4.0.0
|
49 |
-
*
|
50 |
-
* @type string
|
51 |
-
*/
|
52 |
-
protected $plugin_directory = SIW_DIR;
|
53 |
-
|
54 |
-
/**
|
55 |
-
* Directory name where templates are found in this plugin.
|
56 |
-
*
|
57 |
-
* @since 4.0.0
|
58 |
-
*
|
59 |
-
* @type string
|
60 |
-
*/
|
61 |
-
protected $plugin_template_directory = 'templates';
|
62 |
-
|
63 |
-
/**
|
64 |
-
* Contructor method to set up the loader.
|
65 |
-
*
|
66 |
-
* Accepts an array of class properties when instantiated to override the
|
67 |
-
* defaults.
|
68 |
-
*
|
69 |
-
* @since 4.0.0
|
70 |
-
*
|
71 |
-
* @param array $args List of class properties.
|
72 |
-
*/
|
73 |
-
public function __construct( $args = array() ) {
|
74 |
-
$keys = array_keys( get_object_vars( $this ) );
|
75 |
-
foreach ( $keys as $key ) {
|
76 |
-
if ( isset( $args[ $key ] ) ) {
|
77 |
-
$this->$key = $args[ $key ];
|
78 |
-
}
|
79 |
-
}
|
80 |
-
}
|
81 |
-
|
82 |
-
/**
|
83 |
-
* Retrieve a template part.
|
84 |
-
*
|
85 |
-
* @since 4.0.0
|
86 |
-
*
|
87 |
-
* @uses Simple_Image_Widget_Template_Loader::get_template_possble_parts()
|
88 |
-
* Create file names of templates.
|
89 |
-
* @uses Simple_Image_Widget_Template_Loader::locate_template() Retrieve the
|
90 |
-
* name of the highest priority template file that exists.
|
91 |
-
*
|
92 |
-
* @param string $slug
|
93 |
-
* @param string $name Optional. Default null.
|
94 |
-
* @param bool $load Optional. Default true.
|
95 |
-
* @return string
|
96 |
-
*/
|
97 |
-
public function get_template_part( $slug, $name = null, $load = true ) {
|
98 |
-
/**
|
99 |
-
* Execute code for this part.
|
100 |
-
*
|
101 |
-
* @since 4.0.0
|
102 |
-
*
|
103 |
-
* @param string $slug
|
104 |
-
* @param string $name
|
105 |
-
*/
|
106 |
-
do_action( 'get_template_part_' . $slug, $slug, $name );
|
107 |
-
|
108 |
-
// Get files names of templates, for given slug and name.
|
109 |
-
$templates = $this->get_template_file_names( $slug, $name );
|
110 |
-
|
111 |
-
// Return the part that is found.
|
112 |
-
return $this->locate_template( $templates, $load, false );
|
113 |
-
}
|
114 |
-
|
115 |
-
/**
|
116 |
-
* Given a slug and optional name, create the file names of templates.
|
117 |
-
*
|
118 |
-
* @since 4.0.0
|
119 |
-
*
|
120 |
-
* @param string $slug
|
121 |
-
* @param string $name
|
122 |
-
* @return array
|
123 |
-
*/
|
124 |
-
protected function get_template_file_names( $slug, $name ) {
|
125 |
-
$templates = array();
|
126 |
-
if ( isset( $name ) ) {
|
127 |
-
$templates[] = $slug . '-' . $name . '.php';
|
128 |
-
}
|
129 |
-
$templates[] = $slug . '.php';
|
130 |
-
|
131 |
-
/**
|
132 |
-
* Allow template choices to be filtered.
|
133 |
-
*
|
134 |
-
* The resulting array should be in the order of most specific first, to
|
135 |
-
* least specific last.
|
136 |
-
* e.g. 0 => recipe-instructions.php, 1 => recipe.php
|
137 |
-
*
|
138 |
-
* @since 4.0.0
|
139 |
-
*
|
140 |
-
* @param array $templates Names of template files that should be looked for, for given slug and name.
|
141 |
-
* @param string $slug Template slug.
|
142 |
-
* @param string $name Template name.
|
143 |
-
*/
|
144 |
-
return apply_filters( $this->filter_prefix . '_get_template_part', $templates, $slug, $name );
|
145 |
-
}
|
146 |
-
|
147 |
-
/**
|
148 |
-
* Retrieve the name of the highest priority template file that exists.
|
149 |
-
*
|
150 |
-
* Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which
|
151 |
-
* inherit from a parent theme can just overload one file. If the template is
|
152 |
-
* not found in either of those, it looks in the theme-compat folder last.
|
153 |
-
*
|
154 |
-
* @since 4.0.0
|
155 |
-
*
|
156 |
-
* @uses Simple_Image_Widget_Template_Loader::get_template_paths() Return a
|
157 |
-
* list of paths to check for template locations.
|
158 |
-
*
|
159 |
-
* @param string|array $template_names Template file(s) to search for, in order.
|
160 |
-
* @param bool $load If true the template file will be loaded if it is found.
|
161 |
-
* @param bool $require_once Whether to require_once or require. Default true.
|
162 |
-
* Has no effect if $load is false.
|
163 |
-
* @return string The template filename if one is located.
|
164 |
-
*/
|
165 |
-
public function locate_template( $template_names, $load = false, $require_once = true ) {
|
166 |
-
// No file found yet.
|
167 |
-
$located = false;
|
168 |
-
|
169 |
-
// Remove empty entries.
|
170 |
-
$template_names = array_filter( (array) $template_names );
|
171 |
-
$template_paths = $this->get_template_paths();
|
172 |
-
|
173 |
-
// Try to find a template file.
|
174 |
-
foreach ( $template_names as $template_name ) {
|
175 |
-
// Trim off any slashes from the template name.
|
176 |
-
$template_name = ltrim( $template_name, '/' );
|
177 |
-
|
178 |
-
// Try locating this template file by looping through the template paths.
|
179 |
-
foreach ( $template_paths as $template_path ) {
|
180 |
-
if ( file_exists( $template_path . $template_name ) ) {
|
181 |
-
$located = $template_path . $template_name;
|
182 |
-
break 2;
|
183 |
-
}
|
184 |
-
}
|
185 |
-
}
|
186 |
-
|
187 |
-
if ( $load && $located ) {
|
188 |
-
load_template( $located, $require_once );
|
189 |
-
}
|
190 |
-
|
191 |
-
return $located;
|
192 |
-
}
|
193 |
-
|
194 |
-
/**
|
195 |
-
* Load a template file.
|
196 |
-
*
|
197 |
-
* @since 4.0.0
|
198 |
-
*
|
199 |
-
* @param string $template_file Absolute path to a file or list of template parts.
|
200 |
-
* @param array $data Optional. List of variables to extract into the template scope.
|
201 |
-
*/
|
202 |
-
public function load_template( $template_file, $data = array() ) {
|
203 |
-
global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
|
204 |
-
|
205 |
-
if ( is_array( $data ) && ! empty( $data ) ) {
|
206 |
-
extract( $data, EXTR_SKIP );
|
207 |
-
unset( $data );
|
208 |
-
}
|
209 |
-
|
210 |
-
if ( file_exists( $template_file ) ) {
|
211 |
-
require( $template_file );
|
212 |
-
}
|
213 |
-
}
|
214 |
-
|
215 |
-
/**
|
216 |
-
* Return a list of paths to check for template locations.
|
217 |
-
*
|
218 |
-
* Default is to check in a child theme (if relevant) before a parent theme,
|
219 |
-
* so that themes which inherit from a parent theme can just overload one
|
220 |
-
* file. If the template is not found in either of those, it looks in the
|
221 |
-
* theme-compat folder last.
|
222 |
-
*
|
223 |
-
* @since 4.0.0
|
224 |
-
*
|
225 |
-
* @return mixed|void
|
226 |
-
*/
|
227 |
-
protected function get_template_paths() {
|
228 |
-
$theme_directory = trailingslashit( $this->theme_template_directory );
|
229 |
-
|
230 |
-
$file_paths = array(
|
231 |
-
10 => trailingslashit( get_template_directory() ) . $theme_directory,
|
232 |
-
100 => $this->get_templates_dir(),
|
233 |
-
);
|
234 |
-
|
235 |
-
// Only add this conditionally, so non-child themes don't redundantly check active theme twice.
|
236 |
-
if ( is_child_theme() ) {
|
237 |
-
$file_paths[1] = trailingslashit( get_stylesheet_directory() ) . $theme_directory;
|
238 |
-
}
|
239 |
-
|
240 |
-
/**
|
241 |
-
* Allow ordered list of template paths to be amended.
|
242 |
-
*
|
243 |
-
* @since 4.0.0
|
244 |
-
*
|
245 |
-
* @param array $var Default is directory in child theme at index 1, parent theme at 10, and plugin at 100.
|
246 |
-
*/
|
247 |
-
$file_paths = apply_filters( $this->filter_prefix . '_template_paths', $file_paths );
|
248 |
-
|
249 |
-
// sort the file paths based on priority
|
250 |
-
ksort( $file_paths, SORT_NUMERIC );
|
251 |
-
|
252 |
-
return array_map( 'trailingslashit', $file_paths );
|
253 |
-
}
|
254 |
-
|
255 |
-
/**
|
256 |
-
* Return the path to the templates directory in this plugin.
|
257 |
-
*
|
258 |
-
* @since 4.0.0
|
259 |
-
*
|
260 |
-
* @return string
|
261 |
-
*/
|
262 |
-
protected function get_templates_dir() {
|
263 |
-
return trailingslashit( $this->plugin_directory ) . $this->plugin_template_directory;
|
264 |
-
}
|
265 |
-
}
|
1 |
+
<?php
|
2 |
+
/**
|
3 |
+
* Template loader.
|
4 |
+
*
|
5 |
+
* Based off version 1.1.0 of the Gamajo Template Loader by Gary Jones. Changes
|
6 |
+
* allow for overriding class properties during instantiation and adding a
|
7 |
+
* load_template() method that accepts arbitrary data to extra into the local
|
8 |
+
* template scope.
|
9 |
+
*
|
10 |
+
* @package SimpleImageWidget
|
11 |
+
* @since 4.0.0
|
12 |
+
* @author Gary Jones
|
13 |
+
* @link http://github.com/GaryJones/Gamajo-Template-Loader
|
14 |
+
* @copyright 2013 Gary Jones
|
15 |
+
* @license GPL-2.0+
|
16 |
+
*/
|
17 |
+
|
18 |
+
/**
|
19 |
+
* Template loader.
|
20 |
+
*
|
21 |
+
* @package SimpleImageWidget
|
22 |
+
* @since 4.0.0
|
23 |
+
* @author Gary Jones
|
24 |
+
*/
|
25 |
+
class Simple_Image_Widget_Template_Loader {
|
26 |
+
/**
|
27 |
+
* Prefix for filter names.
|
28 |
+
*
|
29 |
+
* @since 4.0.0
|
30 |
+
*
|
31 |
+
* @type string
|
32 |
+
*/
|
33 |
+
protected $filter_prefix = 'simple_image_widget';
|
34 |
+
|
35 |
+
/**
|
36 |
+
* Directory name where custom templates for this plugin should be found in
|
37 |
+
* the theme.
|
38 |
+
*
|
39 |
+
* @since 4.0.0
|
40 |
+
*
|
41 |
+
* @type string
|
42 |
+
*/
|
43 |
+
protected $theme_template_directory = 'simple-image-widget';
|
44 |
+
|
45 |
+
/**
|
46 |
+
* Reference to the root directory path of this plugin.
|
47 |
+
*
|
48 |
+
* @since 4.0.0
|
49 |
+
*
|
50 |
+
* @type string
|
51 |
+
*/
|
52 |
+
protected $plugin_directory = SIW_DIR;
|
53 |
+
|
54 |
+
/**
|
55 |
+
* Directory name where templates are found in this plugin.
|
56 |
+
*
|
57 |
+
* @since 4.0.0
|
58 |
+
*
|
59 |
+
* @type string
|
60 |
+
*/
|
61 |
+
protected $plugin_template_directory = 'templates';
|
62 |
+
|
63 |
+
/**
|
64 |
+
* Contructor method to set up the loader.
|
65 |
+
*
|
66 |
+
* Accepts an array of class properties when instantiated to override the
|
67 |
+
* defaults.
|
68 |
+
*
|
69 |
+
* @since 4.0.0
|
70 |
+
*
|
71 |
+
* @param array $args List of class properties.
|
72 |
+
*/
|
73 |
+
public function __construct( $args = array() ) {
|
74 |
+
$keys = array_keys( get_object_vars( $this ) );
|
75 |
+
foreach ( $keys as $key ) {
|
76 |
+
if ( isset( $args[ $key ] ) ) {
|
77 |
+
$this->$key = $args[ $key ];
|
78 |
+
}
|
79 |
+
}
|
80 |
+
}
|
81 |
+
|
82 |
+
/**
|
83 |
+
* Retrieve a template part.
|
84 |
+
*
|
85 |
+
* @since 4.0.0
|
86 |
+
*
|
87 |
+
* @uses Simple_Image_Widget_Template_Loader::get_template_possble_parts()
|
88 |
+
* Create file names of templates.
|
89 |
+
* @uses Simple_Image_Widget_Template_Loader::locate_template() Retrieve the
|
90 |
+
* name of the highest priority template file that exists.
|
91 |
+
*
|
92 |
+
* @param string $slug
|
93 |
+
* @param string $name Optional. Default null.
|
94 |
+
* @param bool $load Optional. Default true.
|
95 |
+
* @return string
|
96 |
+
*/
|
97 |
+
public function get_template_part( $slug, $name = null, $load = true ) {
|
98 |
+
/**
|
99 |
+
* Execute code for this part.
|
100 |
+
*
|
101 |
+
* @since 4.0.0
|
102 |
+
*
|
103 |
+
* @param string $slug
|
104 |
+
* @param string $name
|
105 |
+
*/
|
106 |
+
do_action( 'get_template_part_' . $slug, $slug, $name );
|
107 |
+
|
108 |
+
// Get files names of templates, for given slug and name.
|
109 |
+
$templates = $this->get_template_file_names( $slug, $name );
|
110 |
+
|
111 |
+
// Return the part that is found.
|
112 |
+
return $this->locate_template( $templates, $load, false );
|
113 |
+
}
|
114 |
+
|
115 |
+
/**
|
116 |
+
* Given a slug and optional name, create the file names of templates.
|
117 |
+
*
|
118 |
+
* @since 4.0.0
|
119 |
+
*
|
120 |
+
* @param string $slug
|
121 |
+
* @param string $name
|
122 |
+
* @return array
|
123 |
+
*/
|
124 |
+
protected function get_template_file_names( $slug, $name ) {
|
125 |
+
$templates = array();
|
126 |
+
if ( isset( $name ) ) {
|
127 |
+
$templates[] = $slug . '-' . $name . '.php';
|
128 |
+
}
|
129 |
+
$templates[] = $slug . '.php';
|
130 |
+
|
131 |
+
/**
|
132 |
+
* Allow template choices to be filtered.
|
133 |
+
*
|
134 |
+
* The resulting array should be in the order of most specific first, to
|
135 |
+
* least specific last.
|
136 |
+
* e.g. 0 => recipe-instructions.php, 1 => recipe.php
|
137 |
+
*
|
138 |
+
* @since 4.0.0
|
139 |
+
*
|
140 |
+
* @param array $templates Names of template files that should be looked for, for given slug and name.
|
141 |
+
* @param string $slug Template slug.
|
142 |
+
* @param string $name Template name.
|
143 |
+
*/
|
144 |
+
return apply_filters( $this->filter_prefix . '_get_template_part', $templates, $slug, $name );
|
145 |
+
}
|
146 |
+
|
147 |
+
/**
|
148 |
+
* Retrieve the name of the highest priority template file that exists.
|
149 |
+
*
|
150 |
+
* Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which
|
151 |
+
* inherit from a parent theme can just overload one file. If the template is
|
152 |
+
* not found in either of those, it looks in the theme-compat folder last.
|
153 |
+
*
|
154 |
+
* @since 4.0.0
|
155 |
+
*
|
156 |
+
* @uses Simple_Image_Widget_Template_Loader::get_template_paths() Return a
|
157 |
+
* list of paths to check for template locations.
|
158 |
+
*
|
159 |
+
* @param string|array $template_names Template file(s) to search for, in order.
|
160 |
+
* @param bool $load If true the template file will be loaded if it is found.
|
161 |
+
* @param bool $require_once Whether to require_once or require. Default true.
|
162 |
+
* Has no effect if $load is false.
|
163 |
+
* @return string The template filename if one is located.
|
164 |
+
*/
|
165 |
+
public function locate_template( $template_names, $load = false, $require_once = true ) {
|
166 |
+
// No file found yet.
|
167 |
+
$located = false;
|
168 |
+
|
169 |
+
// Remove empty entries.
|
170 |
+
$template_names = array_filter( (array) $template_names );
|
171 |
+
$template_paths = $this->get_template_paths();
|
172 |
+
|
173 |
+
// Try to find a template file.
|
174 |
+
foreach ( $template_names as $template_name ) {
|
175 |
+
// Trim off any slashes from the template name.
|
176 |
+
$template_name = ltrim( $template_name, '/' );
|
177 |
+
|
178 |
+
// Try locating this template file by looping through the template paths.
|
179 |
+
foreach ( $template_paths as $template_path ) {
|
180 |
+
if ( file_exists( $template_path . $template_name ) ) {
|
181 |
+
$located = $template_path . $template_name;
|
182 |
+
break 2;
|
183 |
+
}
|
184 |
+
}
|
185 |
+
}
|
186 |
+
|
187 |
+
if ( $load && $located ) {
|
188 |
+
load_template( $located, $require_once );
|
189 |
+
}
|
190 |
+
|
191 |
+
return $located;
|
192 |
+
}
|
193 |
+
|
194 |
+
/**
|
195 |
+
* Load a template file.
|
196 |
+
*
|
197 |
+
* @since 4.0.0
|
198 |
+
*
|
199 |
+
* @param string $template_file Absolute path to a file or list of template parts.
|
200 |
+
* @param array $data Optional. List of variables to extract into the template scope.
|
201 |
+
*/
|
202 |
+
public function load_template( $template_file, $data = array() ) {
|
203 |
+
global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
|
204 |
+
|
205 |
+
if ( is_array( $data ) && ! empty( $data ) ) {
|
206 |
+
extract( $data, EXTR_SKIP );
|
207 |
+
unset( $data );
|
208 |
+
}
|
209 |
+
|
210 |
+
if ( file_exists( $template_file ) ) {
|
211 |
+
require( $template_file );
|
212 |
+
}
|
213 |
+
}
|
214 |
+
|
215 |
+
/**
|
216 |
+
* Return a list of paths to check for template locations.
|
217 |
+
*
|
218 |
+
* Default is to check in a child theme (if relevant) before a parent theme,
|
219 |
+
* so that themes which inherit from a parent theme can just overload one
|
220 |
+
* file. If the template is not found in either of those, it looks in the
|
221 |
+
* theme-compat folder last.
|
222 |
+
*
|
223 |
+
* @since 4.0.0
|
224 |
+
*
|
225 |
+
* @return mixed|void
|
226 |
+
*/
|
227 |
+
protected function get_template_paths() {
|
228 |
+
$theme_directory = trailingslashit( $this->theme_template_directory );
|
229 |
+
|
230 |
+
$file_paths = array(
|
231 |
+
10 => trailingslashit( get_template_directory() ) . $theme_directory,
|
232 |
+
100 => $this->get_templates_dir(),
|
233 |
+
);
|
234 |
+
|
235 |
+
// Only add this conditionally, so non-child themes don't redundantly check active theme twice.
|
236 |
+
if ( is_child_theme() ) {
|
237 |
+
$file_paths[1] = trailingslashit( get_stylesheet_directory() ) . $theme_directory;
|
238 |
+
}
|
239 |
+
|
240 |
+
/**
|
241 |
+
* Allow ordered list of template paths to be amended.
|
242 |
+
*
|
243 |
+
* @since 4.0.0
|
244 |
+
*
|
245 |
+
* @param array $var Default is directory in child theme at index 1, parent theme at 10, and plugin at 100.
|
246 |
+
*/
|
247 |
+
$file_paths = apply_filters( $this->filter_prefix . '_template_paths', $file_paths );
|
248 |
+
|
249 |
+
// sort the file paths based on priority
|
250 |
+
ksort( $file_paths, SORT_NUMERIC );
|
251 |
+
|
252 |
+
return array_map( 'trailingslashit', $file_paths );
|
253 |
+
}
|
254 |
+
|
255 |
+
/**
|
256 |
+
* Return the path to the templates directory in this plugin.
|
257 |
+
*
|
258 |
+
* @since 4.0.0
|
259 |
+
*
|
260 |
+
* @return string
|
261 |
+
*/
|
262 |
+
protected function get_templates_dir() {
|
263 |
+
return trailingslashit( $this->plugin_directory ) . $this->plugin_template_directory;
|
264 |
+
}
|
265 |
+
}
|
includes/class-simple-image-widget.php
CHANGED
@@ -1,576 +1,576 @@
|
|
1 |
-
<?php
|
2 |
-
/**
|
3 |
-
* The image widget.
|
4 |
-
*
|
5 |
-
* @package SimpleImageWidget
|
6 |
-
* @copyright Copyright (c) 2015 Cedaro, LLC
|
7 |
-
* @license GPL-2.0+
|
8 |
-
* @since 3.0.0
|
9 |
-
*/
|
10 |
-
|
11 |
-
/**
|
12 |
-
* Image widget class.
|
13 |
-
*
|
14 |
-
* @package SimpleImageWidget
|
15 |
-
* @since 3.0.0
|
16 |
-
*/
|
17 |
-
class Simple_Image_Widget extends WP_Widget {
|
18 |
-
/**
|
19 |
-
* Setup widget options.
|
20 |
-
*
|
21 |
-
* Child classes may override the defaults.
|
22 |
-
*
|
23 |
-
* @since 3.0.0
|
24 |
-
* @see WP_Widget::construct()
|
25 |
-
*
|
26 |
-
* @param string $id_base Optional Base ID for the widget, lower case, if
|
27 |
-
* left empty a portion of the widget's class name will be used. Must be unique.
|
28 |
-
* @param string $name Name for the widget displayed on the configuration page.
|
29 |
-
* @param array $widget_options {
|
30 |
-
* Widget options. Passed to wp_register_sidebar_widget(). Optional.
|
31 |
-
*
|
32 |
-
* @type string $description Widget description. Shown on the configuration page.
|
33 |
-
* @type string $classname HTML class.
|
34 |
-
* }
|
35 |
-
* @param array $control_options {
|
36 |
-
* Passed to wp_register_widget_control(). Optional.
|
37 |
-
*
|
38 |
-
* @type int $width Width of the widget edit form.
|
39 |
-
* )
|
40 |
-
*/
|
41 |
-
public function __construct( $id_base = false, $name = false, $widget_options = array(), $control_options = array() ) {
|
42 |
-
$id_base = ( $id_base ) ? $id_base : 'simpleimage'; // Legacy ID.
|
43 |
-
$name = ( $name ) ? $name : __( 'Image', 'simple-image-widget' );
|
44 |
-
|
45 |
-
$widget_options = wp_parse_args(
|
46 |
-
$widget_options,
|
47 |
-
array(
|
48 |
-
'classname' => 'widget_simpleimage', // Legacy class name.
|
49 |
-
'customize_selective_refresh' => true,
|
50 |
-
'description' => __( 'An image from your Media Library.', 'simple-image-widget' ),
|
51 |
-
)
|
52 |
-
);
|
53 |
-
|
54 |
-
$control_options = wp_parse_args( $control_options, array() );
|
55 |
-
|
56 |
-
parent::__construct( $id_base, $name, $widget_options, $control_options );
|
57 |
-
|
58 |
-
// Flush widget group cache when an attachment is saved, deleted, or the theme is switched.
|
59 |
-
add_action( 'save_post', array( $this, 'flush_group_cache' ) );
|
60 |
-
add_action( 'delete_attachment', array( $this, 'flush_group_cache' ) );
|
61 |
-
add_action( 'switch_theme', array( $this, 'flush_group_cache' ) );
|
62 |
-
}
|
63 |
-
|
64 |
-
/**
|
65 |
-
* Display the widget.
|
66 |
-
*
|
67 |
-
* Filters the instance data, fetches the output, displays it, then caches
|
68 |
-
* it. Overload or filter the render() method to modify output.
|
69 |
-
*
|
70 |
-
* @since 3.0.0
|
71 |
-
*
|
72 |
-
* @param array $args Registered sidebar arguments including before_title, after_title, before_widget, and after_widget.
|
73 |
-
* @param array $instance The widget instance settings.
|
74 |
-
*/
|
75 |
-
public function widget( $args, $instance ) {
|
76 |
-
$cache = (array) wp_cache_get( 'simple_image_widget', 'widget' );
|
77 |
-
$cache_id = $this->siw_get_cache_key( $args, $instance );
|
78 |
-
|
79 |
-
if ( ! $this->is_preview() && isset( $cache[ $cache_id ] ) ) {
|
80 |
-
echo $cache[ $cache_id ];
|
81 |
-
return;
|
82 |
-
}
|
83 |
-
|
84 |
-
// Copy the original values so they can be used in hooks.
|
85 |
-
$instance['text_raw'] = empty( $instance['text'] ) ? '' : $instance['text'];
|
86 |
-
$instance['title_raw'] = empty( $instance['title'] ) ? '' : $instance['title'];
|
87 |
-
$instance['text'] = apply_filters( 'widget_text', $instance['text_raw'], $instance, $this->id_base );
|
88 |
-
$instance['title'] = apply_filters( 'widget_title', $instance['title_raw'], $instance, $this->id_base );
|
89 |
-
|
90 |
-
// Start building the output.
|
91 |
-
$output = '';
|
92 |
-
|
93 |
-
// Make sure the image ID is a valid attachment.
|
94 |
-
if ( ! empty( $instance['image_id'] ) ) {
|
95 |
-
$image = get_post( $instance['image_id'] );
|
96 |
-
if ( ! $image || 'attachment' != get_post_type( $image ) ) {
|
97 |
-
$output = '<!-- Image Widget Error: Invalid Attachment ID -->';
|
98 |
-
}
|
99 |
-
}
|
100 |
-
|
101 |
-
if ( empty( $output ) ) {
|
102 |
-
$instance['link_open'] = '';
|
103 |
-
$instance['link_close'] = '';
|
104 |
-
$instance['text_link_open'] = '';
|
105 |
-
$instance['text_link_close'] = '';
|
106 |
-
|
107 |
-
if ( ! empty ( $instance['link'] ) ) {
|
108 |
-
$target = ( empty( $instance['new_window'] ) ) ? '' : ' target="_blank"';
|
109 |
-
|
110 |
-
$instance['link_open'] = '<a href="' . esc_url( $instance['link'] ) . '"' . $target . '>';
|
111 |
-
$instance['link_close'] = '</a>';
|
112 |
-
|
113 |
-
// This is to differentiate between the image link and text link.
|
114 |
-
$instance['text_link_open'] = $instance['link_open'];
|
115 |
-
$instance['text_link_close'] = $instance['link_close'];
|
116 |
-
|
117 |
-
// The link classes should only be added to the text link.
|
118 |
-
if ( ! empty( $instance['link_classes'] ) ) {
|
119 |
-
$instance['text_link_open'] = sprintf(
|
120 |
-
'<a href="%1$s" class="%3$s"%2$s>',
|
121 |
-
esc_url( $instance['link'] ),
|
122 |
-
$target,
|
123 |
-
esc_attr( $instance['link_classes'] )
|
124 |
-
);
|
125 |
-
}
|
126 |
-
}
|
127 |
-
|
128 |
-
$output = $this->render( $args, $instance );
|
129 |
-
}
|
130 |
-
|
131 |
-
echo $output;
|
132 |
-
|
133 |
-
if ( ! $this->is_preview() ) {
|
134 |
-
$cache[ $cache_id ] = $output;
|
135 |
-
wp_cache_set( 'simple_image_widget', array_filter( $cache ), 'widget' );
|
136 |
-
}
|
137 |
-
}
|
138 |
-
|
139 |
-
/**
|
140 |
-
* Generate the widget output.
|
141 |
-
*
|
142 |
-
* This is typically done in the widget() method, but moving it to a
|
143 |
-
* separate method allows for the routine to be easily overloaded by a class
|
144 |
-
* extending this one without having to reimplement all the caching and
|
145 |
-
* filtering, or resorting to adding a filter, calling the parent method,
|
146 |
-
* then removing the filter.
|
147 |
-
*
|
148 |
-
* @since 3.0.0
|
149 |
-
*
|
150 |
-
* @param array $args Registered sidebar arguments including before_title, after_title, before_widget, and after_widget.
|
151 |
-
* @param array $instance The widget instance settings.
|
152 |
-
* @return string HTML output.
|
153 |
-
*/
|
154 |
-
public function render( $args, $instance ) {
|
155 |
-
$output = $args['before_widget'];
|
156 |
-
|
157 |
-
/**
|
158 |
-
* Widget HTML output.
|
159 |
-
*
|
160 |
-
* @since 3.0.0
|
161 |
-
*
|
162 |
-
* @param string $output Widget output.
|
163 |
-
* @param array $args Registered sidebar arguments including before_title, after_title, before_widget, and after_widget.
|
164 |
-
* @param array $instance The widget instance settings.
|
165 |
-
* @param string $id_base Widget type id.
|
166 |
-
*/
|
167 |
-
$inside = apply_filters( 'simple_image_widget_output', '', $args, $instance, $this->id_base );
|
168 |
-
|
169 |
-
if ( $inside ) {
|
170 |
-
$output .= $inside;
|
171 |
-
} else {
|
172 |
-
$data = array();
|
173 |
-
$data['args'] = $args;
|
174 |
-
$data['after_title'] = $args['after_title'];
|
175 |
-
$data['before_title'] = $args['before_title'];
|
176 |
-
$data['image_size'] = $image_size = ( ! empty( $instance['image_size'] ) ) ? $instance['image_size'] : apply_filters( 'simple_image_widget_output_default_size', 'medium', $this->id_base );
|
177 |
-
$data['title'] = ( empty( $instance['title'] ) ) ? '' : $instance['title'];
|
178 |
-
$data = array_merge( $instance, $data );
|
179 |
-
$data = apply_filters( 'simple_image_widget_template_data', $data );
|
180 |
-
|
181 |
-
ob_start();
|
182 |
-
$templates = $this->get_template_names( $args, $instance );
|
183 |
-
|
184 |
-
$template_loader = new Simple_Image_Widget_Template_Loader();
|
185 |
-
$template = $template_loader->locate_template( $templates );
|
186 |
-
$template_loader->load_template( $template, $data );
|
187 |
-
$output .= ob_get_clean();
|
188 |
-
}
|
189 |
-
|
190 |
-
$output .= $args['after_widget'];
|
191 |
-
|
192 |
-
return $output;
|
193 |
-
}
|
194 |
-
|
195 |
-
/**
|
196 |
-
* Display the form to edit widget settings.
|
197 |
-
*
|
198 |
-
* @since 3.0.0
|
199 |
-
*
|
200 |
-
* @param array $instance The widget settings.
|
201 |
-
*/
|
202 |
-
public function form( $instance ) {
|
203 |
-
$instance = wp_parse_args(
|
204 |
-
(array) $instance,
|
205 |
-
array(
|
206 |
-
'alt' => '', // Legacy.
|
207 |
-
'image' => '', // Legacy URL field.
|
208 |
-
'image_id' => '',
|
209 |
-
'image_size' => 'full',
|
210 |
-
'link' => '',
|
211 |
-
'link_classes' => '',
|
212 |
-
'link_text' => '',
|
213 |
-
'new_window' => '',
|
214 |
-
'title' => '',
|
215 |
-
'text' => '',
|
216 |
-
)
|
217 |
-
);
|
218 |
-
|
219 |
-
$instance['image_id'] = absint( $instance['image_id'] );
|
220 |
-
$instance['title'] = wp_strip_all_tags( $instance['title'] );
|
221 |
-
|
222 |
-
$button_class = array( 'button', 'button-hero', 'simple-image-widget-control-choose' );
|
223 |
-
$image_id = $instance['image_id'];
|
224 |
-
|
225 |
-
/**
|
226 |
-
* The list of fields to display.
|
227 |
-
*
|
228 |
-
* The order of fields can be modified, new fields can be registered, or
|
229 |
-
* existing fields can be removed here. Use the widget type id to limit
|
230 |
-
* fields to a particular type of widget.
|
231 |
-
*
|
232 |
-
* @since 3.0.0
|
233 |
-
*
|
234 |
-
* @param array $fields List of field ids.
|
235 |
-
* @param string $id_base Widget type id.
|
236 |
-
*/
|
237 |
-
$fields = (array) apply_filters( 'simple_image_widget_fields', $this->form_fields(), $this->id_base );
|
238 |
-
?>
|
239 |
-
|
240 |
-
<div class="simple-image-widget-form">
|
241 |
-
|
242 |
-
<?php
|
243 |
-
/**
|
244 |
-
* Display additional information or HTML before the widget edit form.
|
245 |
-
*
|
246 |
-
* @since 3.0.0
|
247 |
-
*
|
248 |
-
* @param array $instance The widget setttings.
|
249 |
-
* @param string $id_base Widget type id.
|
250 |
-
*/
|
251 |
-
do_action( 'simple_image_widget_form_before', $instance, $this->id_base );
|
252 |
-
?>
|
253 |
-
|
254 |
-
<p>
|
255 |
-
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'simple-image-widget' ); ?></label>
|
256 |
-
<input type="text" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat">
|
257 |
-
</p>
|
258 |
-
|
259 |
-
<?php if ( ! is_simple_image_widget_legacy() ) : ?>
|
260 |
-
<p class="simple-image-widget-control<?php echo ( $image_id ) ? ' has-image' : ''; ?>"
|
261 |
-
data-title="<?php esc_attr_e( 'Choose an Image', 'simple-image-widget' ); ?>"
|
262 |
-
data-update-text="<?php esc_attr_e( 'Update Image', 'simple-image-widget' ); ?>"
|
263 |
-
data-target=".image-id">
|
264 |
-
<?php
|
265 |
-
if ( $image_id ) {
|
266 |
-
echo wp_get_attachment_image( $image_id, 'medium', false );
|
267 |
-
unset( $button_class[ array_search( 'button-hero', $button_class ) ] );
|
268 |
-
}
|
269 |
-
?>
|
270 |
-
<input type="hidden" name="<?php echo esc_attr( $this->get_field_name( 'image_id' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'image_id' ) ); ?>" value="<?php echo absint( $image_id ); ?>" class="image-id simple-image-widget-control-target">
|
271 |
-
<a href="#" class="<?php echo esc_attr( join( ' ', $button_class ) ); ?>"><?php _e( 'Choose an Image', 'simple-image-widget' ); ?></a>
|
272 |
-
</p>
|
273 |
-
<?php endif; ?>
|
274 |
-
|
275 |
-
<?php
|
276 |
-
if ( ! empty( $fields ) ) {
|
277 |
-
foreach ( $fields as $field ) {
|
278 |
-
switch ( $field ) {
|
279 |
-
case 'image_size' :
|
280 |
-
$sizes = $this->get_image_sizes( $image_id );
|
281 |
-
?>
|
282 |
-
<p class="<?php echo esc_attr( $this->siw_field_class( 'image_size' ) ); ?>">
|
283 |
-
<label for="<?php echo esc_attr( $this->get_field_id( 'image_size' ) ); ?>"><?php _e( 'Size:', 'simple-image-widget' ); ?></label>
|
284 |
-
<select name="<?php echo esc_attr( $this->get_field_name( 'image_size' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'image_size' ) ); ?>" class="widefat image-size"<?php echo ( sizeof( $sizes ) < 2 ) ? ' disabled="disabled"' : ''; ?>>
|
285 |
-
<?php
|
286 |
-
foreach ( $sizes as $id => $label ) {
|
287 |
-
printf(
|
288 |
-
'<option value="%s"%s>%s</option>',
|
289 |
-
esc_attr( $id ),
|
290 |
-
selected( $instance['image_size'], $id, false ),
|
291 |
-
esc_html( $label )
|
292 |
-
);
|
293 |
-
}
|
294 |
-
?>
|
295 |
-
</select>
|
296 |
-
</p>
|
297 |
-
<?php
|
298 |
-
break;
|
299 |
-
|
300 |
-
case 'link' :
|
301 |
-
?>
|
302 |
-
<p class="<?php echo esc_attr( $this->siw_field_class( 'link' ) ); ?>">
|
303 |
-
<label for="<?php echo esc_attr( $this->get_field_id( 'link' ) ); ?>"><?php _e( 'Link:', 'simple-image-widget' ); ?></label>
|
304 |
-
<span class="simple-image-widget-input-group">
|
305 |
-
<input type="text" name="<?php echo esc_attr( $this->get_field_name( 'link' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'link' ) ); ?>" value="<?php echo esc_url( $instance['link'] ); ?>" class="simple-image-widget-input-group-field">
|
306 |
-
<button class="simple-image-widget-find-posts-button simple-image-widget-input-group-button dashicons dashicons-search"></button>
|
307 |
-
</span>
|
308 |
-
</p>
|
309 |
-
<p class="<?php echo esc_attr( $this->siw_field_class( 'new_window' ) ); ?>" style="margin-top: -0.75em; padding-left: 2px">
|
310 |
-
<label for="<?php echo esc_attr( $this->get_field_id( 'new_window' ) ); ?>">
|
311 |
-
<input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'new_window' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'new_window' ) ); ?>" <?php checked( $instance['new_window'] ); ?>>
|
312 |
-
<?php _e( 'Open in new window?', 'simple-image-widget' ); ?>
|
313 |
-
</label>
|
314 |
-
</p>
|
315 |
-
<?php
|
316 |
-
break;
|
317 |
-
|
318 |
-
case 'link_classes' :
|
319 |
-
?>
|
320 |
-
<p class="<?php echo esc_attr( $this->siw_field_class( 'link_classes' ) ); ?>">
|
321 |
-
<label for="<?php echo esc_attr( $this->get_field_id( 'link_classes' ) ); ?>"><?php _e( 'Link Classes:', 'simple-image-widget' ); ?></label>
|
322 |
-
<input type="text" name="<?php echo esc_attr( $this->get_field_name( 'link_classes' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'link_classes' ) ); ?>" value="<?php echo esc_attr( $instance['link_classes'] ); ?>" class="widefat">
|
323 |
-
</p>
|
324 |
-
<?php
|
325 |
-
break;
|
326 |
-
|
327 |
-
case 'link_text' :
|
328 |
-
?>
|
329 |
-
<p class="<?php echo esc_attr( $this->siw_field_class( 'link_text' ) ); ?>">
|
330 |
-
<label for="<?php echo esc_attr( $this->get_field_id( 'link_text' ) ); ?>"><?php _e( 'Link Text:', 'simple-image-widget' ); ?></label>
|
331 |
-
<input type="text" name="<?php echo esc_attr( $this->get_field_name( 'link_text' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'link_text' ) ); ?>" value="<?php echo esc_attr( $instance['link_text'] ); ?>" class="widefat">
|
332 |
-
</p>
|
333 |
-
<?php
|
334 |
-
break;
|
335 |
-
|
336 |
-
case 'text' :
|
337 |
-
?>
|
338 |
-
<p class="<?php echo esc_attr( $this->siw_field_class( 'text' ) ); ?>">
|
339 |
-
<label for="<?php echo esc_attr( $this->get_field_id( 'text' ) ); ?>"><?php _e( 'Text:', 'simple-image-widget' ); ?></label>
|
340 |
-
<textarea name="<?php echo esc_attr( $this->get_field_name( 'text' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'text' ) ); ?>" rows="4" class="widefat"><?php echo esc_textarea( $instance['text'] ); ?></textarea>
|
341 |
-
</p>
|
342 |
-
<?php
|
343 |
-
break;
|
344 |
-
|
345 |
-
default :
|
346 |
-
/**
|
347 |
-
* Display a custom field.
|
348 |
-
*
|
349 |
-
* This action will fire for custom fields
|
350 |
-
* registered with the 'simple_image_widget_fields'
|
351 |
-
* filter.
|
352 |
-
*
|
353 |
-
* @since 3.0.0
|
354 |
-
*
|
355 |
-
* @param array $instance The widget setttings.
|
356 |
-
* @param string $widget Widget instance.
|
357 |
-
*/
|
358 |
-
do_action( 'simple_image_widget_field-' . sanitize_key( $field ), $instance, $this );
|
359 |
-
}
|
360 |
-
}
|
361 |
-
}
|
362 |
-
|
363 |
-
/**
|
364 |
-
* Display additional information or HTML after the widget edit form.
|
365 |
-
*
|
366 |
-
* @since 3.0.0
|
367 |
-
*
|
368 |
-
* @param array $instance The widget setttings.
|
369 |
-
* @param string $id_base Widget type id.
|
370 |
-
*/
|
371 |
-
do_action( 'simple_image_widget_form_after', $instance, $this->id_base );
|
372 |
-
?>
|
373 |
-
|
374 |
-
</div><!-- /.simple-image-widget-form -->
|
375 |
-
<?php
|
376 |
-
}
|
377 |
-
|
378 |
-
/**
|
379 |
-
* The list of extra fields that should be shown in the widget form.
|
380 |
-
*
|
381 |
-
* Can be easily overloaded by a child class.
|
382 |
-
*
|
383 |
-
* @since 3.0.0
|
384 |
-
*
|
385 |
-
* @return string List of field ids.
|
386 |
-
*/
|
387 |
-
public function form_fields() {
|
388 |
-
return array( 'image_size', 'link', 'link_text', 'link_classes', 'text' );
|
389 |
-
}
|
390 |
-
|
391 |
-
/**
|
392 |
-
* Save and sanitize widget settings.
|
393 |
-
*
|
394 |
-
* @since 3.0.0
|
395 |
-
*
|
396 |
-
* @param array $new_instance New widget settings.
|
397 |
-
* @param array $old_instance Previous widget settings.
|
398 |
-
* @return array Sanitized settings.
|
399 |
-
*/
|
400 |
-
public function update( $new_instance, $old_instance ) {
|
401 |
-
$instance = wp_parse_args( $new_instance, $old_instance );
|
402 |
-
|
403 |
-
$instance = apply_filters( 'simple_image_widget_instance', $instance, $new_instance, $old_instance, $this->id_base );
|
404 |
-
|
405 |
-
$instance['title'] = wp_strip_all_tags( $new_instance['title'] );
|
406 |
-
$instance['image_id'] = absint( $new_instance['image_id'] );
|
407 |
-
$instance['new_window'] = isset( $new_instance['new_window'] );
|
408 |
-
|
409 |
-
// Optional field that can be removed via a filter.
|
410 |
-
foreach ( array( 'link', 'link_classes', 'link_text', 'text' ) as $key ) {
|
411 |
-
if ( ! isset( $new_instance[ $key ] ) ) {
|
412 |
-
continue;
|
413 |
-
}
|
414 |
-
|
415 |
-
switch ( $key ) {
|
416 |
-
case 'link' :
|
417 |
-
$instance['link'] = esc_url_raw( $new_instance['link'] );
|
418 |
-
break;
|
419 |
-
case 'link_classes' :
|
420 |
-
$instance['link_classes'] = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $new_instance['link_classes'] ) ) );
|
421 |
-
break;
|
422 |
-
case 'link_text' :
|
423 |
-
$instance['link_text'] = wp_kses_data( $new_instance['link_text'] );
|
424 |
-
break;
|
425 |
-
case 'text' :
|
426 |
-
$instance['text'] = stripslashes( wp_filter_post_kses( addslashes( $new_instance['text'] ) ) );
|
427 |
-
break;
|
428 |
-
}
|
429 |
-
}
|
430 |
-
|
431 |
-
$this->flush_group_cache();
|
432 |
-
|
433 |
-
return $instance;
|
434 |
-
}
|
435 |
-
|
436 |
-
/**
|
437 |
-
* Determine if the widget is being displayed in the customizer.
|
438 |
-
*
|
439 |
-
* @since 4.0.1
|
440 |
-
* @link https://core.trac.wordpress.org/ticket/27538
|
441 |
-
*
|
442 |
-
* @return bool
|
443 |
-
*/
|
444 |
-
public function is_preview() {
|
445 |
-
global $wp_customize;
|
446 |
-
|
447 |
-
if ( method_exists( get_parent_class( $this ), 'is_preview' ) ) {
|
448 |
-
return parent::is_preview();
|
449 |
-
}
|
450 |
-
|
451 |
-
return ( isset( $wp_customize ) && $wp_customize->is_preview() ) ;
|
452 |
-
}
|
453 |
-
|
454 |
-
/**
|
455 |
-
* Get the various sizes of an image.
|
456 |
-
*
|
457 |
-
* @since 3.0.0
|
458 |
-
*
|
459 |
-
* @param int $image_id Image attachment ID.
|
460 |
-
* @return array List of image size keys and their localized labels.
|
461 |
-
*/
|
462 |
-
public function get_image_sizes( $image_id ) {
|
463 |
-
$sizes = array( 'full' => __( 'Full Size', 'simple-image-widget' ) );
|
464 |
-
|
465 |
-
$imagedata = wp_get_attachment_metadata( $image_id );
|
466 |
-
if ( isset( $imagedata['sizes'] ) ) {
|
467 |
-
$size_names = Simple_Image_Widget_Plugin::get_image_size_names();
|
468 |
-
|
469 |
-
$sizes['full'] .= ( isset( $imagedata['width'] ) && isset( $imagedata['height'] ) ) ? sprintf( ' (%d×%d)', $imagedata['width'], $imagedata['height'] ) : '';
|
470 |
-
|
471 |
-
foreach ( $imagedata['sizes'] as $_size => $data ) {
|
472 |
-
$label = ( isset( $size_names[ $_size ] ) ) ? $size_names[ $_size ] : ucwords( $_size );
|
473 |
-
$label .= sprintf( ' (%d×%d)', $data['width'], $data['height'] );
|
474 |
-
|
475 |
-
$sizes[ $_size ] = $label;
|
476 |
-
}
|
477 |
-
}
|
478 |
-
|
479 |
-
return $sizes;
|
480 |
-
}
|
481 |
-
|
482 |
-
/**
|
483 |
-
* Flush the cache for all image widgets.
|
484 |
-
*
|
485 |
-
* @since 3.0.0
|
486 |
-
*
|
487 |
-
* @param int $post_id Post ID.
|
488 |
-
*/
|
489 |
-
public function flush_group_cache( $post_id = null ) {
|
490 |
-
if ( 'save_post' == current_filter() && 'attachment' != get_post_type( $post_id ) ) {
|
491 |
-
return;
|
492 |
-
}
|
493 |
-
|
494 |
-
wp_cache_delete( 'simple_image_widget', 'widget' );
|
495 |
-
}
|
496 |
-
|
497 |
-
/**
|
498 |
-
* Retrieve a list of templates to look up.
|
499 |
-
*
|
500 |
-
* @since 4.0.0
|
501 |
-
*
|
502 |
-
* @param array $args Registered sidebar arguments including before_title, after_title, before_widget, and after_widget.
|
503 |
-
* @param array $instance The widget instance settings.
|
504 |
-
* @return array List of template names.
|
505 |
-
*/
|
506 |
-
protected function get_template_names( $args, $instance ) {
|
507 |
-
$templates = array();
|
508 |
-
if ( ! empty( $args['id'] ) ) {
|
509 |
-
$templates[] = $args['id'] . '_widget.php';
|
510 |
-
}
|
511 |
-
$templates[] = 'widget.php';
|
512 |
-
|
513 |
-
/**
|
514 |
-
* List of template names to look up to render output.
|
515 |
-
*
|
516 |
-
* Child widgets should consider adding a new template using the widget type id ($this->id_base).
|
517 |
-
*
|
518 |
-
* @since 4.0.0
|
519 |
-
*
|
520 |
-
* @param array $templates List of template names.
|
521 |
-
* @param array $args Registered sidebar arguments including before_title, after_title, before_widget, and after_widget.
|
522 |
-
* @param array $instance The widget instance settings.
|
523 |
-
* @param string $id_base Widget type id.
|
524 |
-
*/
|
525 |
-
return apply_filters(
|
526 |
-
'simple_image_widget_templates',
|
527 |
-
$templates,
|
528 |
-
$args,
|
529 |
-
$instance,
|
530 |
-
$this->id_base
|
531 |
-
);
|
532 |
-
}
|
533 |
-
|
534 |
-
/**
|
535 |
-
* Retrieve HTML classes for a field container.
|
536 |
-
*
|
537 |
-
* @since 4.1.0
|
538 |
-
*
|
539 |
-
* @param string $id Field id.
|
540 |
-
* @return string
|
541 |
-
*/
|
542 |
-
protected function siw_field_class( $id ) {
|
543 |
-
$classes = array( 'simple-image-widget-field', 'simple-image-widget-field-' . sanitize_html_class( $id ) );
|
544 |
-
|
545 |
-
$hidden_fields = Simple_Image_Widget_Plugin::get_hidden_fields();
|
546 |
-
if ( in_array( $id, $hidden_fields ) ) {
|
547 |
-
$classes[] = 'is-hidden';
|
548 |
-
}
|
549 |
-
|
550 |
-
return implode( ' ', $classes );
|
551 |
-
}
|
552 |
-
|
553 |
-
/**
|
554 |
-
* Retrieve a cache key based on a hash of passed parameters.
|
555 |
-
*
|
556 |
-
* @since 4.2.0
|
557 |
-
*
|
558 |
-
* @return string
|
559 |
-
*/
|
560 |
-
protected function siw_get_cache_key() {
|
561 |
-
$data = array();
|
562 |
-
foreach ( func_get_args() as $arg ) {
|
563 |
-
$data = array_merge( $data, (array) $arg );
|
564 |
-
}
|
565 |
-
ksort( $data );
|
566 |
-
return 'siw_' . md5( json_encode( $data ) );
|
567 |
-
}
|
568 |
-
|
569 |
-
/**
|
570 |
-
* Remove a single image widget from the cache.
|
571 |
-
*
|
572 |
-
* @since 3.0.0
|
573 |
-
* @deprecated 4.2.0
|
574 |
-
*/
|
575 |
-
public function flush_widget_cache() {}
|
576 |
-
}
|
1 |
+
<?php
|
2 |
+
/**
|
3 |
+
* The image widget.
|
4 |
+
*
|
5 |
+
* @package SimpleImageWidget
|
6 |
+
* @copyright Copyright (c) 2015 Cedaro, LLC
|
7 |
+
* @license GPL-2.0+
|
8 |
+
* @since 3.0.0
|
9 |
+
*/
|
10 |
+
|
11 |
+
/**
|
12 |
+
* Image widget class.
|
13 |
+
*
|
14 |
+
* @package SimpleImageWidget
|
15 |
+
* @since 3.0.0
|
16 |
+
*/
|
17 |
+
class Simple_Image_Widget extends WP_Widget {
|
18 |
+
/**
|
19 |
+
* Setup widget options.
|
20 |
+
*
|
21 |
+
* Child classes may override the defaults.
|
22 |
+
*
|
23 |
+
* @since 3.0.0
|
24 |
+
* @see WP_Widget::construct()
|
25 |
+
*
|
26 |
+
* @param string $id_base Optional Base ID for the widget, lower case, if
|
27 |
+
* left empty a portion of the widget's class name will be used. Must be unique.
|
28 |
+
* @param string $name Name for the widget displayed on the configuration page.
|
29 |
+
* @param array $widget_options {
|
30 |
+
* Widget options. Passed to wp_register_sidebar_widget(). Optional.
|
31 |
+
*
|
32 |
+
* @type string $description Widget description. Shown on the configuration page.
|
33 |
+
* @type string $classname HTML class.
|
34 |
+
* }
|
35 |
+
* @param array $control_options {
|
36 |
+
* Passed to wp_register_widget_control(). Optional.
|
37 |
+
*
|
38 |
+
* @type int $width Width of the widget edit form.
|
39 |
+
* )
|
40 |
+
*/
|
41 |
+
public function __construct( $id_base = false, $name = false, $widget_options = array(), $control_options = array() ) {
|
42 |
+
$id_base = ( $id_base ) ? $id_base : 'simpleimage'; // Legacy ID.
|
43 |
+
$name = ( $name ) ? $name : __( 'Image', 'simple-image-widget' );
|
44 |
+
|
45 |
+
$widget_options = wp_parse_args(
|
46 |
+
$widget_options,
|
47 |
+
array(
|
48 |
+
'classname' => 'widget_simpleimage', // Legacy class name.
|
49 |
+
'customize_selective_refresh' => true,
|
50 |
+
'description' => __( 'An image from your Media Library.', 'simple-image-widget' ),
|
51 |
+
)
|
52 |
+
);
|
53 |
+
|
54 |
+
$control_options = wp_parse_args( $control_options, array() );
|
55 |
+
|
56 |
+
parent::__construct( $id_base, $name, $widget_options, $control_options );
|
57 |
+
|
58 |
+
// Flush widget group cache when an attachment is saved, deleted, or the theme is switched.
|
59 |
+
add_action( 'save_post', array( $this, 'flush_group_cache' ) );
|
60 |
+
add_action( 'delete_attachment', array( $this, 'flush_group_cache' ) );
|
61 |
+
add_action( 'switch_theme', array( $this, 'flush_group_cache' ) );
|
62 |
+
}
|
63 |
+
|
64 |
+
/**
|
65 |
+
* Display the widget.
|
66 |
+
*
|
67 |
+
* Filters the instance data, fetches the output, displays it, then caches
|
68 |
+
* it. Overload or filter the render() method to modify output.
|
69 |
+
*
|
70 |
+
* @since 3.0.0
|
71 |
+
*
|
72 |
+
* @param array $args Registered sidebar arguments including before_title, after_title, before_widget, and after_widget.
|
73 |
+
* @param array $instance The widget instance settings.
|
74 |
+
*/
|
75 |
+
public function widget( $args, $instance ) {
|
76 |
+
$cache = (array) wp_cache_get( 'simple_image_widget', 'widget' );
|
77 |
+
$cache_id = $this->siw_get_cache_key( $args, $instance );
|
78 |
+
|
79 |
+
if ( ! $this->is_preview() && isset( $cache[ $cache_id ] ) ) {
|
80 |
+
echo $cache[ $cache_id ];
|
81 |
+
return;
|
82 |
+
}
|
83 |
+
|
84 |
+
// Copy the original values so they can be used in hooks.
|
85 |
+
$instance['text_raw'] = empty( $instance['text'] ) ? '' : $instance['text'];
|
86 |
+
$instance['title_raw'] = empty( $instance['title'] ) ? '' : $instance['title'];
|
87 |
+
$instance['text'] = apply_filters( 'widget_text', $instance['text_raw'], $instance, $this->id_base );
|
88 |
+
$instance['title'] = apply_filters( 'widget_title', $instance['title_raw'], $instance, $this->id_base );
|
89 |
+
|
90 |
+
// Start building the output.
|
91 |
+
$output = '';
|
92 |
+
|
93 |
+
// Make sure the image ID is a valid attachment.
|
94 |
+
if ( ! empty( $instance['image_id'] ) ) {
|
95 |
+
$image = get_post( $instance['image_id'] );
|
96 |
+
if ( ! $image || 'attachment' != get_post_type( $image ) ) {
|
97 |
+
$output = '<!-- Image Widget Error: Invalid Attachment ID -->';
|
98 |
+
}
|
99 |
+
}
|
100 |
+
|
101 |
+
if ( empty( $output ) ) {
|
102 |
+
$instance['link_open'] = '';
|
103 |
+
$instance['link_close'] = '';
|
104 |
+
$instance['text_link_open'] = '';
|
105 |
+
$instance['text_link_close'] = '';
|
106 |
+
|
107 |
+
if ( ! empty ( $instance['link'] ) ) {
|
108 |
+
$target = ( empty( $instance['new_window'] ) ) ? '' : ' target="_blank"';
|
109 |
+
|
110 |
+
$instance['link_open'] = '<a href="' . esc_url( $instance['link'] ) . '"' . $target . '>';
|
111 |
+
$instance['link_close'] = '</a>';
|
112 |
+
|
113 |
+
// This is to differentiate between the image link and text link.
|
114 |
+
$instance['text_link_open'] = $instance['link_open'];
|
115 |
+
$instance['text_link_close'] = $instance['link_close'];
|
116 |
+
|
117 |
+
// The link classes should only be added to the text link.
|
118 |
+
if ( ! empty( $instance['link_classes'] ) ) {
|
119 |
+
$instance['text_link_open'] = sprintf(
|
120 |
+
'<a href="%1$s" class="%3$s"%2$s>',
|
121 |
+
esc_url( $instance['link'] ),
|
122 |
+
$target,
|
123 |
+
esc_attr( $instance['link_classes'] )
|
124 |
+
);
|
125 |
+
}
|
126 |
+
}
|
127 |
+
|
128 |
+
$output = $this->render( $args, $instance );
|
129 |
+
}
|
130 |
+
|
131 |
+
echo $output;
|
132 |
+
|
133 |
+
if ( ! $this->is_preview() ) {
|
134 |
+
$cache[ $cache_id ] = $output;
|
135 |
+
wp_cache_set( 'simple_image_widget', array_filter( $cache ), 'widget' );
|
136 |
+
}
|
137 |
+
}
|
138 |
+
|
139 |
+
/**
|
140 |
+
* Generate the widget output.
|
141 |
+
*
|
142 |
+
* This is typically done in the widget() method, but moving it to a
|
143 |
+
* separate method allows for the routine to be easily overloaded by a class
|
144 |
+
* extending this one without having to reimplement all the caching and
|
145 |
+
* filtering, or resorting to adding a filter, calling the parent method,
|
146 |
+
* then removing the filter.
|
147 |
+
*
|
148 |
+
* @since 3.0.0
|
149 |
+
*
|
150 |
+
* @param array $args Registered sidebar arguments including before_title, after_title, before_widget, and after_widget.
|
151 |
+
* @param array $instance The widget instance settings.
|
152 |
+
* @return string HTML output.
|
153 |
+
*/
|
154 |
+
public function render( $args, $instance ) {
|
155 |
+
$output = $args['before_widget'];
|
156 |
+
|
157 |
+
/**
|
158 |
+
* Widget HTML output.
|
159 |
+
*
|
160 |
+
* @since 3.0.0
|
161 |
+
*
|
162 |
+
* @param string $output Widget output.
|
163 |
+
* @param array $args Registered sidebar arguments including before_title, after_title, before_widget, and after_widget.
|
164 |
+
* @param array $instance The widget instance settings.
|
165 |
+
* @param string $id_base Widget type id.
|
166 |
+
*/
|
167 |
+
$inside = apply_filters( 'simple_image_widget_output', '', $args, $instance, $this->id_base );
|
168 |
+
|
169 |
+
if ( $inside ) {
|
170 |
+
$output .= $inside;
|
171 |
+
} else {
|
172 |
+
$data = array();
|
173 |
+
$data['args'] = $args;
|
174 |
+
$data['after_title'] = $args['after_title'];
|
175 |
+
$data['before_title'] = $args['before_title'];
|
176 |
+
$data['image_size'] = $image_size = ( ! empty( $instance['image_size'] ) ) ? $instance['image_size'] : apply_filters( 'simple_image_widget_output_default_size', 'medium', $this->id_base );
|
177 |
+
$data['title'] = ( empty( $instance['title'] ) ) ? '' : $instance['title'];
|
178 |
+
$data = array_merge( $instance, $data );
|
179 |
+
$data = apply_filters( 'simple_image_widget_template_data', $data );
|
180 |
+
|
181 |
+
ob_start();
|
182 |
+
$templates = $this->get_template_names( $args, $instance );
|
183 |
+
|
184 |
+
$template_loader = new Simple_Image_Widget_Template_Loader();
|
185 |
+
$template = $template_loader->locate_template( $templates );
|
186 |
+
$template_loader->load_template( $template, $data );
|
187 |
+
$output .= ob_get_clean();
|
188 |
+
}
|
189 |
+
|
190 |
+
$output .= $args['after_widget'];
|
191 |
+
|
192 |
+
return $output;
|
193 |
+
}
|
194 |
+
|
195 |
+
/**
|
196 |
+
* Display the form to edit widget settings.
|
197 |
+
*
|
198 |
+
* @since 3.0.0
|
199 |
+
*
|
200 |
+
* @param array $instance The widget settings.
|
201 |
+
*/
|
202 |
+
public function form( $instance ) {
|
203 |
+
$instance = wp_parse_args(
|
204 |
+
(array) $instance,
|
205 |
+
array(
|
206 |
+
'alt' => '', // Legacy.
|
207 |
+
'image' => '', // Legacy URL field.
|
208 |
+
'image_id' => '',
|
209 |
+
'image_size' => 'full',
|
210 |
+
'link' => '',
|
211 |
+
'link_classes' => '',
|
212 |
+
'link_text' => '',
|
213 |
+
'new_window' => '',
|
214 |
+
'title' => '',
|
215 |
+
'text' => '',
|
216 |
+
)
|
217 |
+
);
|
218 |
+
|
219 |
+
$instance['image_id'] = absint( $instance['image_id'] );
|
220 |
+
$instance['title'] = wp_strip_all_tags( $instance['title'] );
|
221 |
+
|
222 |
+
$button_class = array( 'button', 'button-hero', 'simple-image-widget-control-choose' );
|
223 |
+
$image_id = $instance['image_id'];
|
224 |
+
|
225 |
+
/**
|
226 |
+
* The list of fields to display.
|
227 |
+
*
|
228 |
+
* The order of fields can be modified, new fields can be registered, or
|
229 |
+
* existing fields can be removed here. Use the widget type id to limit
|
230 |
+
* fields to a particular type of widget.
|
231 |
+
*
|
232 |
+
* @since 3.0.0
|
233 |
+
*
|
234 |
+
* @param array $fields List of field ids.
|
235 |
+
* @param string $id_base Widget type id.
|
236 |
+
*/
|
237 |
+
$fields = (array) apply_filters( 'simple_image_widget_fields', $this->form_fields(), $this->id_base );
|
238 |
+
?>
|
239 |
+
|
240 |
+
<div class="simple-image-widget-form">
|
241 |
+
|
242 |
+
<?php
|
243 |
+
/**
|
244 |
+
* Display additional information or HTML before the widget edit form.
|
245 |
+
*
|
246 |
+
* @since 3.0.0
|
247 |
+
*
|
248 |
+
* @param array $instance The widget setttings.
|
249 |
+
* @param string $id_base Widget type id.
|
250 |
+
*/
|
251 |
+
do_action( 'simple_image_widget_form_before', $instance, $this->id_base );
|
252 |
+
?>
|
253 |
+
|
254 |
+
<p>
|
255 |
+
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'simple-image-widget' ); ?></label>
|
256 |
+
<input type="text" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat">
|
257 |
+
</p>
|
258 |
+
|
259 |
+
<?php if ( ! is_simple_image_widget_legacy() ) : ?>
|
260 |
+
<p class="simple-image-widget-control<?php echo ( $image_id ) ? ' has-image' : ''; ?>"
|
261 |
+
data-title="<?php esc_attr_e( 'Choose an Image', 'simple-image-widget' ); ?>"
|
262 |
+
data-update-text="<?php esc_attr_e( 'Update Image', 'simple-image-widget' ); ?>"
|
263 |
+
data-target=".image-id">
|
264 |
+
<?php
|
265 |
+
if ( $image_id ) {
|
266 |
+
echo wp_get_attachment_image( $image_id, 'medium', false );
|
267 |
+
unset( $button_class[ array_search( 'button-hero', $button_class ) ] );
|
268 |
+
}
|
269 |
+
?>
|
270 |
+
<input type="hidden" name="<?php echo esc_attr( $this->get_field_name( 'image_id' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'image_id' ) ); ?>" value="<?php echo absint( $image_id ); ?>" class="image-id simple-image-widget-control-target">
|
271 |
+
<a href="#" class="<?php echo esc_attr( join( ' ', $button_class ) ); ?>"><?php _e( 'Choose an Image', 'simple-image-widget' ); ?></a>
|
272 |
+
</p>
|
273 |
+
<?php endif; ?>
|
274 |
+
|
275 |
+
<?php
|
276 |
+
if ( ! empty( $fields ) ) {
|
277 |
+
foreach ( $fields as $field ) {
|
278 |
+
switch ( $field ) {
|
279 |
+
case 'image_size' :
|
280 |
+
$sizes = $this->get_image_sizes( $image_id );
|
281 |
+
?>
|
282 |
+
<p class="<?php echo esc_attr( $this->siw_field_class( 'image_size' ) ); ?>">
|
283 |
+
<label for="<?php echo esc_attr( $this->get_field_id( 'image_size' ) ); ?>"><?php _e( 'Size:', 'simple-image-widget' ); ?></label>
|
284 |
+
<select name="<?php echo esc_attr( $this->get_field_name( 'image_size' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'image_size' ) ); ?>" class="widefat image-size"<?php echo ( sizeof( $sizes ) < 2 ) ? ' disabled="disabled"' : ''; ?>>
|
285 |
+
<?php
|
286 |
+
foreach ( $sizes as $id => $label ) {
|
287 |
+
printf(
|
288 |
+
'<option value="%s"%s>%s</option>',
|
289 |
+
esc_attr( $id ),
|
290 |
+
selected( $instance['image_size'], $id, false ),
|
291 |
+
esc_html( $label )
|
292 |
+
);
|
293 |
+
}
|
294 |
+
?>
|
295 |
+
</select>
|
296 |
+
</p>
|
297 |
+
<?php
|
298 |
+
break;
|
299 |
+
|
300 |
+
case 'link' :
|
301 |
+
?>
|
302 |
+
<p class="<?php echo esc_attr( $this->siw_field_class( 'link' ) ); ?>">
|
303 |
+
<label for="<?php echo esc_attr( $this->get_field_id( 'link' ) ); ?>"><?php _e( 'Link:', 'simple-image-widget' ); ?></label>
|
304 |
+
<span class="simple-image-widget-input-group">
|
305 |
+
<input type="text" name="<?php echo esc_attr( $this->get_field_name( 'link' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'link' ) ); ?>" value="<?php echo esc_url( $instance['link'] ); ?>" class="simple-image-widget-input-group-field">
|
306 |
+
<button class="simple-image-widget-find-posts-button simple-image-widget-input-group-button dashicons dashicons-search"></button>
|
307 |
+
</span>
|
308 |
+
</p>
|
309 |
+
<p class="<?php echo esc_attr( $this->siw_field_class( 'new_window' ) ); ?>" style="margin-top: -0.75em; padding-left: 2px">
|
310 |
+
<label for="<?php echo esc_attr( $this->get_field_id( 'new_window' ) ); ?>">
|
311 |
+
<input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'new_window' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'new_window' ) ); ?>" <?php checked( $instance['new_window'] ); ?>>
|
312 |
+
<?php _e( 'Open in new window?', 'simple-image-widget' ); ?>
|
313 |
+
</label>
|
314 |
+
</p>
|
315 |
+
<?php
|
316 |
+
break;
|
317 |
+
|
318 |
+
case 'link_classes' :
|
319 |
+
?>
|
320 |
+
<p class="<?php echo esc_attr( $this->siw_field_class( 'link_classes' ) ); ?>">
|
321 |
+
<label for="<?php echo esc_attr( $this->get_field_id( 'link_classes' ) ); ?>"><?php _e( 'Link Classes:', 'simple-image-widget' ); ?></label>
|
322 |
+
<input type="text" name="<?php echo esc_attr( $this->get_field_name( 'link_classes' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'link_classes' ) ); ?>" value="<?php echo esc_attr( $instance['link_classes'] ); ?>" class="widefat">
|
323 |
+
</p>
|
324 |
+
<?php
|
325 |
+
break;
|
326 |
+
|
327 |
+
case 'link_text' :
|
328 |
+
?>
|
329 |
+
<p class="<?php echo esc_attr( $this->siw_field_class( 'link_text' ) ); ?>">
|
330 |
+
<label for="<?php echo esc_attr( $this->get_field_id( 'link_text' ) ); ?>"><?php _e( 'Link Text:', 'simple-image-widget' ); ?></label>
|
331 |
+
<input type="text" name="<?php echo esc_attr( $this->get_field_name( 'link_text' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'link_text' ) ); ?>" value="<?php echo esc_attr( $instance['link_text'] ); ?>" class="widefat">
|
332 |
+
</p>
|
333 |
+
<?php
|
334 |
+
break;
|
335 |
+
|
336 |
+
case 'text' :
|
337 |
+
?>
|
338 |
+
<p class="<?php echo esc_attr( $this->siw_field_class( 'text' ) ); ?>">
|
339 |
+
<label for="<?php echo esc_attr( $this->get_field_id( 'text' ) ); ?>"><?php _e( 'Text:', 'simple-image-widget' ); ?></label>
|
340 |
+
<textarea name="<?php echo esc_attr( $this->get_field_name( 'text' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'text' ) ); ?>" rows="4" class="widefat"><?php echo esc_textarea( $instance['text'] ); ?></textarea>
|
341 |
+
</p>
|
342 |
+
<?php
|
343 |
+
break;
|
344 |
+
|
345 |
+
default :
|
346 |
+
/**
|
347 |
+
* Display a custom field.
|
348 |
+
*
|
349 |
+
* This action will fire for custom fields
|
350 |
+
* registered with the 'simple_image_widget_fields'
|
351 |
+
* filter.
|
352 |
+
*
|
353 |
+
* @since 3.0.0
|
354 |
+
*
|
355 |
+
* @param array $instance The widget setttings.
|
356 |
+
* @param string $widget Widget instance.
|
357 |
+
*/
|
358 |
+
do_action( 'simple_image_widget_field-' . sanitize_key( $field ), $instance, $this );
|
359 |
+
}
|
360 |
+
}
|
361 |
+
}
|
362 |
+
|
363 |
+
/**
|
364 |
+
* Display additional information or HTML after the widget edit form.
|
365 |
+
*
|
366 |
+
* @since 3.0.0
|
367 |
+
*
|
368 |
+
* @param array $instance The widget setttings.
|
369 |
+
* @param string $id_base Widget type id.
|
370 |
+
*/
|
371 |
+
do_action( 'simple_image_widget_form_after', $instance, $this->id_base );
|
372 |
+
?>
|
373 |
+
|
374 |
+
</div><!-- /.simple-image-widget-form -->
|
375 |
+
<?php
|
376 |
+
}
|
377 |
+
|
378 |
+
/**
|
379 |
+
* The list of extra fields that should be shown in the widget form.
|
380 |
+
*
|
381 |
+
* Can be easily overloaded by a child class.
|
382 |
+
*
|
383 |
+
* @since 3.0.0
|
384 |
+
*
|
385 |
+
* @return string List of field ids.
|
386 |
+
*/
|
387 |
+
public function form_fields() {
|
388 |
+
return array( 'image_size', 'link', 'link_text', 'link_classes', 'text' );
|
389 |
+
}
|
390 |
+
|
391 |
+
/**
|
392 |
+
* Save and sanitize widget settings.
|
393 |
+
*
|
394 |
+
* @since 3.0.0
|
395 |
+
*
|
396 |
+
* @param array $new_instance New widget settings.
|
397 |
+
* @param array $old_instance Previous widget settings.
|
398 |
+
* @return array Sanitized settings.
|
399 |
+
*/
|
400 |
+
public function update( $new_instance, $old_instance ) {
|
401 |
+
$instance = wp_parse_args( $new_instance, $old_instance );
|
402 |
+
|
403 |
+
$instance = apply_filters( 'simple_image_widget_instance', $instance, $new_instance, $old_instance, $this->id_base );
|
404 |
+
|
405 |
+
$instance['title'] = wp_strip_all_tags( $new_instance['title'] );
|
406 |
+
$instance['image_id'] = absint( $new_instance['image_id'] );
|
407 |
+
$instance['new_window'] = isset( $new_instance['new_window'] );
|
408 |
+
|
409 |
+
// Optional field that can be removed via a filter.
|
410 |
+
foreach ( array( 'link', 'link_classes', 'link_text', 'text' ) as $key ) {
|
411 |
+
if ( ! isset( $new_instance[ $key ] ) ) {
|
412 |
+
continue;
|
413 |
+
}
|
414 |
+
|
415 |
+
switch ( $key ) {
|
416 |
+
case 'link' :
|
417 |
+
$instance['link'] = esc_url_raw( $new_instance['link'] );
|
418 |
+
break;
|
419 |
+
case 'link_classes' :
|
420 |
+
$instance['link_classes'] = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $new_instance['link_classes'] ) ) );
|
421 |
+
break;
|
422 |
+
case 'link_text' :
|
423 |
+
$instance['link_text'] = wp_kses_data( $new_instance['link_text'] );
|
424 |
+
break;
|
425 |
+
case 'text' :
|
426 |
+
$instance['text'] = stripslashes( wp_filter_post_kses( addslashes( $new_instance['text'] ) ) );
|
427 |
+
break;
|
428 |
+
}
|
429 |
+
}
|
430 |
+
|
431 |
+
$this->flush_group_cache();
|
432 |
+
|
433 |
+
return $instance;
|
434 |
+
}
|
435 |
+
|
436 |
+
/**
|
437 |
+
* Determine if the widget is being displayed in the customizer.
|
438 |
+
*
|
439 |
+
* @since 4.0.1
|
440 |
+
* @link https://core.trac.wordpress.org/ticket/27538
|
441 |
+
*
|
442 |
+
* @return bool
|
443 |
+
*/
|
444 |
+
public function is_preview() {
|
445 |
+
global $wp_customize;
|
446 |
+
|
447 |
+
if ( method_exists( get_parent_class( $this ), 'is_preview' ) ) {
|
448 |
+
return parent::is_preview();
|
449 |
+
}
|
450 |
+
|
451 |
+
return ( isset( $wp_customize ) && $wp_customize->is_preview() ) ;
|
452 |
+
}
|
453 |
+
|
454 |
+
/**
|
455 |
+
* Get the various sizes of an image.
|
456 |
+
*
|
457 |
+
* @since 3.0.0
|
458 |
+
*
|
459 |
+
* @param int $image_id Image attachment ID.
|
460 |
+
* @return array List of image size keys and their localized labels.
|
461 |
+
*/
|
462 |
+
public function get_image_sizes( $image_id ) {
|
463 |
+
$sizes = array( 'full' => __( 'Full Size', 'simple-image-widget' ) );
|
464 |
+
|
465 |
+
$imagedata = wp_get_attachment_metadata( $image_id );
|
466 |
+
if ( isset( $imagedata['sizes'] ) ) {
|
467 |
+
$size_names = Simple_Image_Widget_Plugin::get_image_size_names();
|
468 |
+
|
469 |
+
$sizes['full'] .= ( isset( $imagedata['width'] ) && isset( $imagedata['height'] ) ) ? sprintf( ' (%d×%d)', $imagedata['width'], $imagedata['height'] ) : '';
|
470 |
+
|
471 |
+
foreach ( $imagedata['sizes'] as $_size => $data ) {
|
472 |
+
$label = ( isset( $size_names[ $_size ] ) ) ? $size_names[ $_size ] : ucwords( $_size );
|
473 |
+
$label .= sprintf( ' (%d×%d)', $data['width'], $data['height'] );
|
474 |
+
|
475 |
+
$sizes[ $_size ] = $label;
|
476 |
+
}
|
477 |
+
}
|
478 |
+
|
479 |
+
return $sizes;
|
480 |
+
}
|
481 |
+
|
482 |
+
/**
|
483 |
+
* Flush the cache for all image widgets.
|
484 |
+
*
|
485 |
+
* @since 3.0.0
|
486 |
+
*
|
487 |
+
* @param int $post_id Post ID.
|
488 |
+
*/
|
489 |
+
public function flush_group_cache( $post_id = null ) {
|
490 |
+
if ( 'save_post' == current_filter() && 'attachment' != get_post_type( $post_id ) ) {
|
491 |
+
return;
|
492 |
+
}
|
493 |
+
|
494 |
+
wp_cache_delete( 'simple_image_widget', 'widget' );
|
495 |
+
}
|
496 |
+
|
497 |
+
/**
|
498 |
+
* Retrieve a list of templates to look up.
|
499 |
+
*
|
500 |
+
* @since 4.0.0
|
501 |
+
*
|
502 |
+
* @param array $args Registered sidebar arguments including before_title, after_title, before_widget, and after_widget.
|
503 |
+
* @param array $instance The widget instance settings.
|
504 |
+
* @return array List of template names.
|
505 |
+
*/
|
506 |
+
protected function get_template_names( $args, $instance ) {
|
507 |
+
$templates = array();
|
508 |
+
if ( ! empty( $args['id'] ) ) {
|
509 |
+
$templates[] = $args['id'] . '_widget.php';
|
510 |
+
}
|
511 |
+
$templates[] = 'widget.php';
|
512 |
+
|
513 |
+
/**
|
514 |
+
* List of template names to look up to render output.
|
515 |
+
*
|
516 |
+
* Child widgets should consider adding a new template using the widget type id ($this->id_base).
|
517 |
+
*
|
518 |
+
* @since 4.0.0
|
519 |
+
*
|
520 |
+
* @param array $templates List of template names.
|
521 |
+
* @param array $args Registered sidebar arguments including before_title, after_title, before_widget, and after_widget.
|
522 |
+
* @param array $instance The widget instance settings.
|
523 |
+
* @param string $id_base Widget type id.
|
524 |
+
*/
|
525 |
+
return apply_filters(
|
526 |
+
'simple_image_widget_templates',
|
527 |
+
$templates,
|
528 |
+
$args,
|
529 |
+
$instance,
|
530 |
+
$this->id_base
|
531 |
+
);
|
532 |
+
}
|
533 |
+
|
534 |
+
/**
|
535 |
+
* Retrieve HTML classes for a field container.
|
536 |
+
*
|
537 |
+
* @since 4.1.0
|
538 |
+
*
|
539 |
+
* @param string $id Field id.
|
540 |
+
* @return string
|
541 |
+
*/
|
542 |
+
protected function siw_field_class( $id ) {
|
543 |
+
$classes = array( 'simple-image-widget-field', 'simple-image-widget-field-' . sanitize_html_class( $id ) );
|
544 |
+
|
545 |
+
$hidden_fields = Simple_Image_Widget_Plugin::get_hidden_fields();
|
546 |
+
if ( in_array( $id, $hidden_fields ) ) {
|
547 |
+
$classes[] = 'is-hidden';
|
548 |
+
}
|
549 |
+
|
550 |
+
return implode( ' ', $classes );
|
551 |
+
}
|
552 |
+
|
553 |
+
/**
|
554 |
+
* Retrieve a cache key based on a hash of passed parameters.
|
555 |
+
*
|
556 |
+
* @since 4.2.0
|
557 |
+
*
|
558 |
+
* @return string
|
559 |
+
*/
|
560 |
+
protected function siw_get_cache_key() {
|
561 |
+
$data = array();
|
562 |
+
foreach ( func_get_args() as $arg ) {
|
563 |
+
$data = array_merge( $data, (array) $arg );
|
564 |
+
}
|
565 |
+
ksort( $data );
|
566 |
+
return 'siw_' . md5( json_encode( $data ) );
|
567 |
+
}
|
568 |
+
|
569 |
+
/**
|
570 |
+
* Remove a single image widget from the cache.
|
571 |
+
*
|
572 |
+
* @since 3.0.0
|
573 |
+
* @deprecated 4.2.0
|
574 |
+
*/
|
575 |
+
public function flush_widget_cache() {}
|
576 |
+
}
|
readme.txt
CHANGED
@@ -1,141 +1,149 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
License
|
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 |
-
* Added
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
* Added
|
98 |
-
*
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
* Added
|
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 |
-
*
|
139 |
-
|
140 |
-
|
141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Simple Image Widget
|
2 |
+
|
3 |
+
Contributors: cedaro, bradyvercher
|
4 |
+
Tags: image widget, widget, media, media manager, sidebar, image, photo, picture
|
5 |
+
Requires at least: 4.4
|
6 |
+
Tested up to: 4.8
|
7 |
+
Stable tag: 4.4.1
|
8 |
+
License: GPL-2.0+
|
9 |
+
License URI: https://www.gnu.org/licenses/gpl-2.0.html
|
10 |
+
|
11 |
+
A simple widget that makes it a breeze to add images to your sidebars.
|
12 |
+
|
13 |
+
## Description
|
14 |
+
|
15 |
+
Simple Image Widget is what the name implies -- the easiest way to add images to your sidebars. Display advertisements, calls-to-action, or even build a slider based on image widgets.
|
16 |
+
|
17 |
+
Despite its simplicity, Simple Image Widget is built with extensibility in mind, making it super easy to spin off new image-based widgets, or customize the widget ouput using the available template hierarchy.
|
18 |
+
|
19 |
+
### Additional Resources
|
20 |
+
|
21 |
+
* [Write a review](https://wordpress.org/support/view/plugin-reviews/simple-image-widget#postform)
|
22 |
+
* [Contribute on GitHub](https://github.com/cedaro/simple-image-widget)
|
23 |
+
* [Follow @cedaroco](https://twitter.com/cedaroco)
|
24 |
+
* [Visit Cedaro](http://www.cedaro.com/?utm_source=wordpress.org&utm_medium=link&utm_content=simple-image-widget-readme&utm_campaign=plugins)
|
25 |
+
|
26 |
+
|
27 |
+
## Installation
|
28 |
+
|
29 |
+
Install just like most other plugins. [Check out the codex](http://codex.wordpress.org/Managing_Plugins#Installing_Plugins) if you have any questions.
|
30 |
+
|
31 |
+
|
32 |
+
## Frequently Asked Questions
|
33 |
+
|
34 |
+
#### Is there a way to filter the widget output?
|
35 |
+
|
36 |
+
Absolutely. Changing the output can be done a few different ways, but the most common alternatives involve using the "`simple_image_widget_output`" filter or overriding the template in your theme.
|
37 |
+
|
38 |
+
To use the template method, copy "`widget.php`" from the "`/templates`" directory in the plugin to a "`/simple-image-widget`" directory in your theme. Then update as you wish. It's also possible to create a custom template specific to each sidebar in your theme using the following default template hierarchy:
|
39 |
+
|
40 |
+
* `{theme}/simple-image-widget/{sidebar_id}_widget.php`
|
41 |
+
* `{theme}/simple-image-widget/widget.php`
|
42 |
+
* `{plugin}/templates/widget.php`
|
43 |
+
|
44 |
+
_Always use a [child theme](https://codex.wordpress.org/Child_Themes) to make changes if you acquired your theme from a third-party and you expect it to be updated. Otherwise, you run the risk of losing your customizations._
|
45 |
+
|
46 |
+
#### How do I add alt text to images in the widget?
|
47 |
+
|
48 |
+
When selecting an image in the media modal (the popup to select images), the right sidebar will be titled "Attachment Details" and contains a field for entering alt text. After entering your alt text, click the "Update Image" button to use the selected image in your widget. Most browsers don't show the alt text, so you'll need to view the HTML source to make sure it exists.
|
49 |
+
|
50 |
+
#### How do I center the widget?
|
51 |
+
|
52 |
+
The widget can be centered using CSS. Custom CSS should be added a child theme or using a plugin like [Simple Custom CSS](https://wordpress.org/plugins/simple-custom-css/) or [Jetpack](https://wordpress.org/plugins/jetpack/). The following snippet will center the contents of the widget:
|
53 |
+
|
54 |
+
`.widget_simpleimage {
|
55 |
+
text-align: center;
|
56 |
+
}`
|
57 |
+
|
58 |
+
#### Can I remove the width and height attributes?
|
59 |
+
|
60 |
+
The widget uses the core function `wp_get_attachment_image()` to display the image and it would be more trouble than it's worth to remove those attributes. Some basic CSS will typically allow you to make the image responsive if necessary:
|
61 |
+
|
62 |
+
`.widget_simpleimage img {
|
63 |
+
height: auto;
|
64 |
+
max-width: 100%;
|
65 |
+
}`
|
66 |
+
|
67 |
+
|
68 |
+
## Screenshots
|
69 |
+
|
70 |
+
1. A new image widget.
|
71 |
+
2. The widget after selecting an image.
|
72 |
+
|
73 |
+
|
74 |
+
## Changelog
|
75 |
+
|
76 |
+
### 4.4.1 - September 6, 2016
|
77 |
+
* Added missing text domains.
|
78 |
+
|
79 |
+
### 4.4.0 - April 22, 2016
|
80 |
+
* Enabled selective refresh in the Customizer.
|
81 |
+
* Removed PO and MO files in favor of WordPress.org Language Packs.
|
82 |
+
* Prevented errors if the main plugin file is accessed directly.
|
83 |
+
|
84 |
+
### 4.3.0
|
85 |
+
* Transferred to [Cedaro](http://www.cedaro.com/).
|
86 |
+
|
87 |
+
### 4.2.2
|
88 |
+
* Show media extensions in the post finder modal.
|
89 |
+
* Added Dutch translation.
|
90 |
+
|
91 |
+
### 4.2.1
|
92 |
+
* Fixed a PHP 5.2 incompatibility that prevented the correct image from showing on the front-end.
|
93 |
+
* Fixed a debug notice when searching for attachments in the new find posts modal.
|
94 |
+
|
95 |
+
### 4.2.0
|
96 |
+
* Added functionality to search for posts to link images to.
|
97 |
+
* Added Japanese translation.
|
98 |
+
* Changed the method for generating cache keys. Should provide better support for the_widget() and similar methods.
|
99 |
+
* Deprecated the method for flushing a single widget instance from the cache.
|
100 |
+
|
101 |
+
### 4.1.2
|
102 |
+
* Added Serbo-Croation translation.
|
103 |
+
|
104 |
+
### 4.1.1
|
105 |
+
* Added Finnish translation.
|
106 |
+
* Prevent a notice about non-existent title when adding a widget in the Customizer in debug mode.
|
107 |
+
|
108 |
+
### 4.1.0
|
109 |
+
* Added the ability to hide widget fields.
|
110 |
+
* Added a field to insert HTML classes on the text link. Hidden by default.
|
111 |
+
* Removed "the_content" filter from widget text to prevent other plugins from appending content.
|
112 |
+
* Renamed /scripts to /js and /styles to /css.
|
113 |
+
* Improved handling of fields that have been removed in child widgets.
|
114 |
+
|
115 |
+
### 4.0.2
|
116 |
+
* Fixed the reference to the widget's parent class to prevent an error.
|
117 |
+
|
118 |
+
### 4.0.1
|
119 |
+
* Allow more HTML tags in the text field.
|
120 |
+
* Updated customizer support and prevent cache poisoning.
|
121 |
+
* Added French translation.
|
122 |
+
|
123 |
+
### 4.0.0
|
124 |
+
* New template system to make it easier to override the output.
|
125 |
+
* Restructured to make it more intuitive for developers to extend the widget.
|
126 |
+
* Moved legacy support into a separate class that hooks into the widget.
|
127 |
+
* Works with the Widget Customizer added in WordPress 3.9.
|
128 |
+
* Improved compatibility with plugins like Page Builder by SiteOrigin.
|
129 |
+
|
130 |
+
### 3.0.4
|
131 |
+
* Fixed a slash preventing custom translations from loading.
|
132 |
+
* Dropped the text domain from custom translation filenames.
|
133 |
+
* Loading the text domain earlier so the widget title and description can be filtered.
|
134 |
+
* Minor code formatting updates.
|
135 |
+
|
136 |
+
### 3.0.3
|
137 |
+
* Fixed PHP class name formatting.
|
138 |
+
* Added 'link_open' and 'link_close' args to the $instance when rendering the widget display.
|
139 |
+
* Added a 'simple-image' CSS class to the image wrapper.
|
140 |
+
|
141 |
+
### 3.0.2
|
142 |
+
* Implemented feature for opening links in a new tab/window.
|
143 |
+
* Fixed a bug preventing links in legacy widgets to not work.
|
144 |
+
|
145 |
+
### 3.0.1
|
146 |
+
* Removed the main plugin file for the previous version.
|
147 |
+
|
148 |
+
### 3.0
|
149 |
+
* Complete rewrite with new media manager support.
|
simple-image-widget.php
CHANGED
@@ -1,79 +1,79 @@
|
|
1 |
-
<?php
|
2 |
-
/**
|
3 |
-
* Simple Image Widget
|
4 |
-
*
|
5 |
-
* @package SimpleImageWidget
|
6 |
-
* @author Brady Vercher
|
7 |
-
* @copyright Copyright (c) 2015 Cedaro, LLC
|
8 |
-
* @license GPL-2.0+
|
9 |
-
*
|
10 |
-
* @wordpress-plugin
|
11 |
-
* Plugin Name: Simple Image Widget
|
12 |
-
* Plugin URI: https://wordpress.org/plugins/simple-image-widget/
|
13 |
-
* Description: A simple image widget utilizing the new WordPress media manager.
|
14 |
-
* Version: 4.4.
|
15 |
-
* Author: Cedaro
|
16 |
-
* Author URI:
|
17 |
-
* License: GPL-2.0+
|
18 |
-
* License URI:
|
19 |
-
* Text Domain: simple-image-widget
|
20 |
-
*/
|
21 |
-
|
22 |
-
// Exit if accessed directly.
|
23 |
-
if ( ! defined( 'ABSPATH' ) ) {
|
24 |
-
exit;
|
25 |
-
}
|
26 |
-
|
27 |
-
/**
|
28 |
-
* Main plugin instance.
|
29 |
-
*
|
30 |
-
* @since 4.0.0
|
31 |
-
* @type Simple_Image_Widget $simple_image_widget
|
32 |
-
*/
|
33 |
-
global $simple_image_widget;
|
34 |
-
|
35 |
-
if ( ! defined( 'SIW_DIR' ) ) {
|
36 |
-
/**
|
37 |
-
* Plugin directory path.
|
38 |
-
*
|
39 |
-
* @since 4.0.0
|
40 |
-
* @type string SIW_DIR
|
41 |
-
*/
|
42 |
-
define( 'SIW_DIR', plugin_dir_path( __FILE__ ) );
|
43 |
-
}
|
44 |
-
|
45 |
-
/**
|
46 |
-
* Check if the installed version of WordPress supports the new media manager.
|
47 |
-
*
|
48 |
-
* @since 3.0.0
|
49 |
-
*/
|
50 |
-
function is_simple_image_widget_legacy() {
|
51 |
-
/**
|
52 |
-
* Whether the installed version of WordPress supports the new media manager.
|
53 |
-
*
|
54 |
-
* @since 4.0.0
|
55 |
-
*
|
56 |
-
* @param bool $is_legacy
|
57 |
-
*/
|
58 |
-
return apply_filters( 'is_simple_image_widget_legacy', version_compare( get_bloginfo( 'version' ), '3.4.2', '<=' ) );
|
59 |
-
}
|
60 |
-
|
61 |
-
/**
|
62 |
-
* Include functions and libraries.
|
63 |
-
*/
|
64 |
-
require_once( SIW_DIR . 'includes/class-simple-image-widget.php' );
|
65 |
-
require_once( SIW_DIR . 'includes/class-simple-image-widget-legacy.php' );
|
66 |
-
require_once( SIW_DIR . 'includes/class-simple-image-widget-plugin.php' );
|
67 |
-
require_once( SIW_DIR . 'includes/class-simple-image-widget-template-loader.php' );
|
68 |
-
|
69 |
-
/**
|
70 |
-
* Deprecated main plugin class.
|
71 |
-
*
|
72 |
-
* @since 3.0.0
|
73 |
-
* @deprecated 4.0.0
|
74 |
-
*/
|
75 |
-
class Simple_Image_Widget_Loader extends Simple_Image_Widget_Plugin {}
|
76 |
-
|
77 |
-
// Initialize and load the plugin.
|
78 |
-
$simple_image_widget = new Simple_Image_Widget_Plugin();
|
79 |
-
add_action( 'plugins_loaded', array( $simple_image_widget, 'load' ) );
|
1 |
+
<?php
|
2 |
+
/**
|
3 |
+
* Simple Image Widget
|
4 |
+
*
|
5 |
+
* @package SimpleImageWidget
|
6 |
+
* @author Brady Vercher
|
7 |
+
* @copyright Copyright (c) 2015 Cedaro, LLC
|
8 |
+
* @license GPL-2.0+
|
9 |
+
*
|
10 |
+
* @wordpress-plugin
|
11 |
+
* Plugin Name: Simple Image Widget
|
12 |
+
* Plugin URI: https://wordpress.org/plugins/simple-image-widget/
|
13 |
+
* Description: A simple image widget utilizing the new WordPress media manager.
|
14 |
+
* Version: 4.4.1
|
15 |
+
* Author: Cedaro
|
16 |
+
* Author URI: https://www.cedaro.com/?utm_source=wordpress-plugin&utm_medium=link&utm_content=simple-image-widget-author-uri&utm_campaign=plugins
|
17 |
+
* License: GPL-2.0+
|
18 |
+
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
|
19 |
+
* Text Domain: simple-image-widget
|
20 |
+
*/
|
21 |
+
|
22 |
+
// Exit if accessed directly.
|
23 |
+
if ( ! defined( 'ABSPATH' ) ) {
|
24 |
+
exit;
|
25 |
+
}
|
26 |
+
|
27 |
+
/**
|
28 |
+
* Main plugin instance.
|
29 |
+
*
|
30 |
+
* @since 4.0.0
|
31 |
+
* @type Simple_Image_Widget $simple_image_widget
|
32 |
+
*/
|
33 |
+
global $simple_image_widget;
|
34 |
+
|
35 |
+
if ( ! defined( 'SIW_DIR' ) ) {
|
36 |
+
/**
|
37 |
+
* Plugin directory path.
|
38 |
+
*
|
39 |
+
* @since 4.0.0
|
40 |
+
* @type string SIW_DIR
|
41 |
+
*/
|
42 |
+
define( 'SIW_DIR', plugin_dir_path( __FILE__ ) );
|
43 |
+
}
|
44 |
+
|
45 |
+
/**
|
46 |
+
* Check if the installed version of WordPress supports the new media manager.
|
47 |
+
*
|
48 |
+
* @since 3.0.0
|
49 |
+
*/
|
50 |
+
function is_simple_image_widget_legacy() {
|
51 |
+
/**
|
52 |
+
* Whether the installed version of WordPress supports the new media manager.
|
53 |
+
*
|
54 |
+
* @since 4.0.0
|
55 |
+
*
|
56 |
+
* @param bool $is_legacy
|
57 |
+
*/
|
58 |
+
return apply_filters( 'is_simple_image_widget_legacy', version_compare( get_bloginfo( 'version' ), '3.4.2', '<=' ) );
|
59 |
+
}
|
60 |
+
|
61 |
+
/**
|
62 |
+
* Include functions and libraries.
|
63 |
+
*/
|
64 |
+
require_once( SIW_DIR . 'includes/class-simple-image-widget.php' );
|
65 |
+
require_once( SIW_DIR . 'includes/class-simple-image-widget-legacy.php' );
|
66 |
+
require_once( SIW_DIR . 'includes/class-simple-image-widget-plugin.php' );
|
67 |
+
require_once( SIW_DIR . 'includes/class-simple-image-widget-template-loader.php' );
|
68 |
+
|
69 |
+
/**
|
70 |
+
* Deprecated main plugin class.
|
71 |
+
*
|
72 |
+
* @since 3.0.0
|
73 |
+
* @deprecated 4.0.0
|
74 |
+
*/
|
75 |
+
class Simple_Image_Widget_Loader extends Simple_Image_Widget_Plugin {}
|
76 |
+
|
77 |
+
// Initialize and load the plugin.
|
78 |
+
$simple_image_widget = new Simple_Image_Widget_Plugin();
|
79 |
+
add_action( 'plugins_loaded', array( $simple_image_widget, 'load' ) );
|
templates/widget.php
CHANGED
@@ -1,45 +1,45 @@
|
|
1 |
-
<?php
|
2 |
-
/**
|
3 |
-
* Default widget template.
|
4 |
-
*
|
5 |
-
* Copy this template to /simple-image-widget/widget.php in your theme or
|
6 |
-
* child theme to make edits.
|
7 |
-
*
|
8 |
-
* @package SimpleImageWidget
|
9 |
-
* @copyright Copyright (c) 2015 Cedaro, LLC
|
10 |
-
* @license GPL-2.0+
|
11 |
-
* @since 4.0.0
|
12 |
-
*/
|
13 |
-
?>
|
14 |
-
|
15 |
-
<?php
|
16 |
-
if ( ! empty( $title ) ) :
|
17 |
-
echo $before_title . $title . $after_title;
|
18 |
-
endif;
|
19 |
-
?>
|
20 |
-
|
21 |
-
<?php if ( ! empty( $image_id ) ) : ?>
|
22 |
-
<p class="simple-image">
|
23 |
-
<?php
|
24 |
-
echo $link_open;
|
25 |
-
echo wp_get_attachment_image( $image_id, $image_size );
|
26 |
-
echo $link_close;
|
27 |
-
?>
|
28 |
-
</p>
|
29 |
-
<?php endif; ?>
|
30 |
-
|
31 |
-
<?php
|
32 |
-
if ( ! empty( $text ) ) :
|
33 |
-
echo wpautop( $text );
|
34 |
-
endif;
|
35 |
-
?>
|
36 |
-
|
37 |
-
<?php if ( ! empty( $link_text ) ) : ?>
|
38 |
-
<p class="more">
|
39 |
-
<?php
|
40 |
-
echo $text_link_open;
|
41 |
-
echo $link_text;
|
42 |
-
echo $text_link_close;
|
43 |
-
?>
|
44 |
-
</p>
|
45 |
-
<?php endif; ?>
|
1 |
+
<?php
|
2 |
+
/**
|
3 |
+
* Default widget template.
|
4 |
+
*
|
5 |
+
* Copy this template to /simple-image-widget/widget.php in your theme or
|
6 |
+
* child theme to make edits.
|
7 |
+
*
|
8 |
+
* @package SimpleImageWidget
|
9 |
+
* @copyright Copyright (c) 2015 Cedaro, LLC
|
10 |
+
* @license GPL-2.0+
|
11 |
+
* @since 4.0.0
|
12 |
+
*/
|
13 |
+
?>
|
14 |
+
|
15 |
+
<?php
|
16 |
+
if ( ! empty( $title ) ) :
|
17 |
+
echo $before_title . $title . $after_title;
|
18 |
+
endif;
|
19 |
+
?>
|
20 |
+
|
21 |
+
<?php if ( ! empty( $image_id ) ) : ?>
|
22 |
+
<p class="simple-image">
|
23 |
+
<?php
|
24 |
+
echo $link_open;
|
25 |
+
echo wp_get_attachment_image( $image_id, $image_size );
|
26 |
+
echo $link_close;
|
27 |
+
?>
|
28 |
+
</p>
|
29 |
+
<?php endif; ?>
|
30 |
+
|
31 |
+
<?php
|
32 |
+
if ( ! empty( $text ) ) :
|
33 |
+
echo wpautop( $text );
|
34 |
+
endif;
|
35 |
+
?>
|
36 |
+
|
37 |
+
<?php if ( ! empty( $link_text ) ) : ?>
|
38 |
+
<p class="more">
|
39 |
+
<?php
|
40 |
+
echo $text_link_open;
|
41 |
+
echo $link_text;
|
42 |
+
echo $text_link_close;
|
43 |
+
?>
|
44 |
+
</p>
|
45 |
+
<?php endif; ?>
|