-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwidget.php
206 lines (189 loc) · 10.5 KB
/
widget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
/**
* Insert Pages widget.
*
* @package insert-pages
*/
/**
* Class: InsertPagesWidget extends WP_Widget
*
* Provides a widget for inserting a page into a widget area.
*
* @package insert-pages
*/
class InsertPagesWidget extends WP_Widget {
/**
* Set up the widget.
*/
public function __construct() {
// Load admin javascript for Widget options on admin page (widgets.php).
add_action( 'sidebar_admin_page', array( $this, 'widget_admin_js' ) );
// Load widget javascript in supported builders (Beaver Builder, Elementor).
add_action( 'fl_builder_layout_style_dependencies', array( $this, 'widget_admin_js' ) );
add_action( 'elementor/editor/before_enqueue_scripts', array( $this, 'widget_admin_js' ) );
// Load admin javascript for Widget options on theme customize page (customize.php).
add_action( 'customize_controls_enqueue_scripts', array( $this, 'widget_admin_js' ) );
// Call parent constructor to initialize the widget.
parent::__construct( 'ipw', __( 'Insert Page', 'insert-pages' ), array( 'description' => __( 'Insert a page into a widget area.', 'insert-pages' ) ) );
}
/**
* Load javascript for interacting with the Insert Page widget.
*/
public function widget_admin_js() {
wp_enqueue_script( 'insertpages_widget', plugins_url( '/js/widget.js', __FILE__ ), array( 'jquery' ), '20221115', array( 'in_footer' => false ) );
}
/**
* Output the content of the widget.
*
* @param array $args Widget args.
* @param array $instance Widget instance.
*/
public function widget( $args, $instance ) {
global $insert_pages_plugin;
// Print widget wrapper.
echo wp_kses_post( $args['before_widget'] );
// Build the shortcode attributes array from the widget args.
$atts = array();
if ( array_key_exists( 'page', $instance ) ) {
$atts['page'] = $instance['page'];
}
if ( array_key_exists( 'display', $instance ) ) {
$atts['display'] = $instance['display'];
}
if ( array_key_exists( 'template', $instance ) && 'template' === $instance['display'] ) {
$atts['display'] = $instance['template'];
}
if ( array_key_exists( 'class', $instance ) ) {
$atts['class'] = $instance['class'];
}
if ( array_key_exists( 'id', $instance ) ) {
$atts['id'] = $instance['id'];
}
if ( array_key_exists( 'inline', $instance ) ) {
$atts['inline'] = '1' === $instance['inline'];
}
if ( array_key_exists( 'querystring', $instance ) ) {
$atts['querystring'] = $instance['querystring'];
}
if ( array_key_exists( 'size', $instance ) && 'post-thumbnail' === $instance['display'] ) {
$atts['size'] = $instance['size'];
}
if ( array_key_exists( 'public', $instance ) ) {
$atts['public'] = '1' === $instance['public'];
}
// Render the inserted page using the plugin's shortcode handler.
$content = $insert_pages_plugin->insert_pages_handle_shortcode_insert( $atts );
// Print inserted page.
echo $content;
// Print widget wrapper.
echo wp_kses_post( $args['after_widget'] );
}
/**
* Output the options form on admin.
*
* @param array $instance The widget options.
*/
public function form( $instance ) {
// Beaver Builder loads the widget without some required wp-admin
// dependencies. Add them here.
if ( ! function_exists( 'page_template_dropdown' ) ) {
// The function page_template_dropdown() is defined in template.php.
/** WordPress Template Administration API */
require_once ABSPATH . 'wp-admin/includes/template.php';
// The function get_page_templates() is defined in theme.php.
/** WordPress Theme Administration API */
require_once ABSPATH . 'wp-admin/includes/theme.php';
}
$instance = wp_parse_args(
(array) $instance,
array(
'page' => '',
'display' => 'link',
'template' => '',
'class' => '',
'id' => '',
'inline' => '',
'querystring' => '',
'size' => '',
'public' => '',
)
); ?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'page' ) ); ?>"><?php esc_html_e( 'Page/Post ID or Slug', 'insert-pages' ); ?>:</label>
<input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'page' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'page' ) ); ?>" value="<?php echo esc_attr( $instance['page'] ); ?>" />
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'display' ) ); ?>"><?php esc_html_e( 'Display', 'insert-pages' ); ?>:</label><br />
<select class="insertpage-format-select" name="<?php echo esc_attr( $this->get_field_name( 'display' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'display' ) ); ?>">
<option value='title' <?php selected( $instance['display'], 'title' ); ?>><?php esc_html_e( 'Title', 'insert-pages' ); ?></option>
<option value='title-content' <?php selected( $instance['display'], 'title-content' ); ?>><?php esc_html_e( 'Title and content', 'insert-pages' ); ?></option>
<option value='link' <?php selected( $instance['display'], 'link' ); ?>><?php esc_html_e( 'Link', 'insert-pages' ); ?></option>
<option value='excerpt' <?php selected( $instance['display'], 'excerpt' ); ?>><?php esc_html_e( 'Excerpt', 'insert-pages' ); ?></option>
<option value='excerpt-only' <?php selected( $instance['display'], 'excerpt-only' ); ?>><?php esc_html_e( 'Excerpt only (no title)', 'insert-pages' ); ?></option>
<option value='content' <?php selected( $instance['display'], 'content' ); ?>><?php esc_html_e( 'Content', 'insert-pages' ); ?></option>
<option value='post-thumbnail' <?php selected( $instance['display'], 'post-thumbnail' ); ?>><?php esc_html_e( 'Post Thumbnail', 'insert-pages' ); ?></option>
<option value='all' <?php selected( $instance['display'], 'all' ); ?>><?php esc_html_e( 'All (includes custom fields)', 'insert-pages' ); ?></option>
<option value='template' <?php selected( $instance['display'], 'template' ); ?>><?php esc_html_e( 'Use a custom template', 'insert-pages' ); ?> »</option>
</select>
<select class="insertpage-template-select" name="<?php echo esc_attr( $this->get_field_name( 'template' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'template' ) ); ?>" disabled="disabled">
<option value='all'><?php esc_html_e( 'Default Template', 'insert-pages' ); ?></option>
<?php if ( function_exists( 'page_template_dropdown' ) ) :
page_template_dropdown( $instance['template'] );
endif; ?>
</select>
<select class="insertpage-size-select" name="<?php echo esc_attr( $this->get_field_name( 'size' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'size' ) ); ?>" disabled="disabled">
<option value="post-thumbnail"><?php esc_html_e( 'post-thumbnail', 'insert-pages' ); ?></option>
<?php if ( function_exists( 'wp_get_registered_image_subsizes' ) ) : ?>
<?php foreach ( wp_get_registered_image_subsizes() as $size_name => $size_data ) : ?>
<option value="<?php echo esc_attr( $size_name ); ?>" <?php selected( $instance['size'], $size_name ); ?>><?php echo esc_html( $size_name ); ?></option>
<?php endforeach; ?>
<?php else : ?>
<?php foreach ( get_intermediate_image_sizes() as $size_name ) : ?>
<option value="<?php echo esc_attr( $size_name ); ?>" <?php selected( $instance['size'], $size_name ); ?>><?php echo esc_html( $size_name ); ?></option>
<?php endforeach; ?>
<?php endif; ?>
</select>
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'class' ) ); ?>"><?php esc_html_e( 'Extra Classes', 'insert-pages' ); ?>:</label>
<input type="text" class="widefat" autocomplete="off" name="<?php echo esc_attr( $this->get_field_name( 'class' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'class' ) ); ?>" value="<?php echo esc_attr( $instance['class'] ); ?>" />
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'id' ) ); ?>"><?php esc_html_e( 'ID', 'insert-pages' ); ?>:</label>
<input type="text" class="widefat" autocomplete="off" name="<?php echo esc_attr( $this->get_field_name( 'id' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'id' ) ); ?>" value="<?php echo esc_attr( $instance['id'] ); ?>" />
</p>
<p>
<input class="checkbox" type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'inline' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'inline' ) ); ?>" value="1" <?php checked( $instance['inline'], '1' ); ?> />
<label for="<?php echo esc_attr( $this->get_field_id( 'inline' ) ); ?>"><?php esc_html_e( 'Inline?', 'insert-pages' ); ?></label>
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'querystring' ) ); ?>"><?php esc_html_e( 'Querystring', 'insert-pages' ); ?>:</label>
<input type="text" class="widefat" autocomplete="off" name="<?php echo esc_attr( $this->get_field_name( 'querystring' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'querystring' ) ); ?>" value="<?php echo esc_attr( $instance['querystring'] ); ?>" />
</p>
<p>
<input class="checkbox" type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'public' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'public' ) ); ?>" value="1" <?php checked( $instance['public'], '1' ); ?> />
<label for="<?php echo esc_attr( $this->get_field_id( 'public' ) ); ?>"><?php esc_html_e( 'Anonymous users can see this inserted even if its status is private', 'insert-pages' ); ?></label>
</p>
<?php
}
/**
* Process widget options on save.
*
* @param array $new_instance The new options.
* @param array $old_instance The previous options.
*/
public function update( $new_instance, $old_instance ) {
// Sanitize form options.
$instance = $old_instance;
$instance['page'] = array_key_exists( 'page', $new_instance ) ? wp_strip_all_tags( $new_instance['page'] ) : '';
$instance['display'] = array_key_exists( 'display', $new_instance ) ? wp_strip_all_tags( $new_instance['display'] ) : '';
$instance['template'] = array_key_exists( 'template', $new_instance ) ? wp_strip_all_tags( $new_instance['template'] ) : '';
$instance['class'] = array_key_exists( 'class', $new_instance ) ? wp_strip_all_tags( $new_instance['class'] ) : '';
$instance['id'] = array_key_exists( 'id', $new_instance ) ? wp_strip_all_tags( $new_instance['id'] ) : '';
$instance['inline'] = array_key_exists( 'inline', $new_instance ) ? wp_strip_all_tags( $new_instance['inline'] ) : '';
$instance['querystring'] = array_key_exists( 'querystring', $new_instance ) ? wp_strip_all_tags( $new_instance['querystring'] ) : '';
$instance['size'] = array_key_exists( 'size', $new_instance ) ? wp_strip_all_tags( $new_instance['size'] ) : '';
$instance['public'] = array_key_exists( 'public', $new_instance ) ? wp_strip_all_tags( $new_instance['public'] ) : '';
return $instance;
}
}