-
Notifications
You must be signed in to change notification settings - Fork 1
/
just-post-preview.widget.php
273 lines (237 loc) · 8.78 KB
/
just-post-preview.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
/**
* JPP_Widget_Post_Preview widget class
*
* Show post preview with different layouts with post link
*
*/
class JPP_Widget_Post_Preview extends WP_Widget {
/**
* Main widget constructor
*/
public function __construct() {
parent::__construct(
'jpp_widget_post_preview', // Base ID
__('Just Post Preview'), // Title
array( 'description' => __( "Post preview with different layouts") )
);
}
/**
* Apply custom styles and scripts to admin UI (only for Widgets or Post Edit page)
*/
public static function admin_scripts(){
// exit if we not on widget pages or post edit
if( strpos($_SERVER['SCRIPT_NAME'], 'widgets.php') === FALSE &&
strpos($_SERVER['SCRIPT_NAME'], 'post.php') === FALSE &&
strpos($_SERVER['SCRIPT_NAME'], 'post-new.php') === FALSE
){
return;
}
wp_register_script( 'jpp_post_preview_widget_js', JPP_URL . 'assets/post_preview_widget.js', array('jquery','jquery-ui-autocomplete') );
wp_enqueue_script('jpp_post_preview_widget_js');
wp_register_style( 'jpp_post_preview_widget_css', JPP_URL . 'assets/post_preview_widget.css');
wp_enqueue_style('jpp_post_preview_widget_css');
}
/**
* Print widget on frontend
*
* @param array $args theme sidebar settings for specific region
* @param array $instance widget settings
*/
public function widget($args, $instance) {
// apply defaults
$instance = array_merge(array(
'title' => '',
'post_type' => 'post',
'post_id' => 0,
'widget_layout' => 'hero',
'css_class' => 'jpp_widget',
), $instance);
$post = get_post($instance['post_id']);
if ( empty($post) ) return;
// print start widget
echo $args['before_widget'];
echo strtr('<div class="widget jpp_post_preview jpp_post_preview_{postid} jpp_layout_{layout} {extra_class}">', array(
'{postid}' => $instance['post_id'],
'{layout}' => $instance['widget_layout'],
'{extra_class}' => $instance['css_class'],
));
// print layout
$layout_file = 'jpp_layout_' . $instance['widget_layout'] . '.php';
$templates = array(
get_stylesheet_directory() . '/just-post-preview/' . $layout_file,
get_template_directory() . '/just-post-preview/' . $layout_file,
JPP_PATH . '/layouts/' . $layout_file,
);
// adds ability to patch layouts placement
$templates = apply_filters('jpp_post_preview_template', $templates);
$template_loaded = false;
foreach ($templates as $template) {
if ( is_file($template) ) {
include ($template);
$template_loaded = true;
break;
}
}
if ( !$template_loaded ) {
echo '<b>Fatal error: </b> Layout template file missing: ' . $templates[0];
}
// print end widget
echo '</div>';
echo $args['after_widget'];
}
/**
* Print Widget form
*
* @param array $instance
*/
public function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$post_type = isset( $instance['post_type'] ) ? $instance['post_type'] : '';
$post_title = isset( $instance['post_title'] ) ? esc_attr( $instance['post_title'] ) : '';
$post_id = isset( $instance['post_id'] ) ? esc_attr( $instance['post_id'] ) : '';
$widget_layout = isset( $instance['widget_layout'] ) ? $instance['widget_layout'] : '';
$css_class = isset( $instance['css_class'] ) ? esc_attr( $instance['css_class'] ) : '';
?>
<div class="jpp_form_controls">
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Internal Title:' ); ?></label>
<input readonly class="widefat jpp_post_preview_widget_title" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'post_type' ); ?>"><?php _e( 'Post type:' ); ?></label>
<select required class="widefat jpp_post_preview_post_type noinit" id="<?php echo $this->get_field_id( 'post_type' ); ?>" name="<?php echo $this->get_field_name( 'post_type' ); ?>">
<?php $this->html_options( self::get_post_types_options(), $post_type ); ?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'post_title' ); ?>"><?php _e( 'Post title:' ); ?></label>
<input required placeholder="Start type post title or insert post URL" class="widefat jpp_post_preview_post_title noinit" id="<?php echo $this->get_field_id( 'post_title' ); ?>" name="<?php echo $this->get_field_name( 'post_title' ); ?>" type="text" value="<?php echo $post_title; ?>" />
<input type="hidden" class="jpp_post_preview_post_id" id="<?php echo $this->get_field_id( 'post_id' ); ?>" name="<?php echo $this->get_field_name( 'post_id' ); ?>" value="<?php echo $post_id; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'widget_layout' ); ?>"><?php _e( 'Widget layout:' ); ?></label>
<select required class="widefat" id="<?php echo $this->get_field_id( 'widget_layout' ); ?>" name="<?php echo $this->get_field_name( 'widget_layout' ); ?>">
<?php $this->html_options( $this->get_available_layouts(), $widget_layout ); ?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'css_class' ); ?>"><?php _e( 'Additional CSS class:' ); ?></label>
<input required class="widefat" id="<?php echo $this->get_field_id( 'css_class' ); ?>" name="<?php echo $this->get_field_name( 'css_class' ); ?>" type="text" value="<?php echo $css_class ?>" />
</p>
</div>
<?php
}
/**
* Update widget settings
*
* @param array $new_instance
* @param array $old_instance
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['post_type'] = strip_tags($new_instance['post_type']);
$instance['post_id'] = strip_tags($new_instance['post_id']);
$instance['post_title'] = strip_tags($new_instance['post_title']);
$instance['widget_layout'] = strip_tags($new_instance['widget_layout']);
$instance['css_class'] = strip_tags($new_instance['css_class']);
return $instance;
}
/**
* Print <option> tags based on array and selected value
*
* @param array $options
* @param string $selected
*/
protected function html_options( $options, $selected ){
if( !is_array($options) ) return '';
foreach( $options as $value => $label) {
print strtr('<option value="{value}" {selected}>{label}</option>', array(
'{value}' => esc_attr($value),
'{selected}' => selected($selected, $value, false),
'{label}' => esc_html($label),
));
}
}
/**
* Return registered post types
*
* @return array (key => title) pairs of registered post types
*/
protected static function get_post_types_options(){
$args = array( 'public' => true );
$post_types = get_post_types($args, 'objects');
$options = array(
'_any_' => 'Any',
);
foreach($post_types as $pt){
$label = $pt->labels->name;
$value = $pt->name;
$options[$value] = $label;
}
$options = apply_filters('jpp_post_preview_post_types', $options);
return $options;
}
/**
* Get pre-defined layouts list
*
* @return array (key => name) pairs
*/
protected function get_available_layouts(){
$layouts = array(
'hero' => 'Hero post (Featured image as Background)',
'left_image' => 'Excerpt with left aligned image',
'right_image' => 'Excerpt with right aligned image',
);
$layouts = apply_filters('jpp_post_preview_layouts', $layouts);
return $layouts;
}
/**
* Callback for Post autocomplete search
*
* @global \wpdb $wpdb
*/
public static function ajax_post_autocomplete(){
$term = stripcslashes($_POST['term']);
$post_type = $_POST['post_type'];
if(empty($term)) die('');
global $wpdb;
$query = "SELECT ID, post_title, post_type FROM $wpdb->posts WHERE post_status = 'publish' ";
$query_args = array();
if ( strpos($term, 'http') === 0 ) {
$post_name = basename( parse_url($term, PHP_URL_PATH) );
$query .= " AND post_name LIKE '%%%s%%' ";
$query_args[] = $post_name;
}
else {
if ( '_any_' != $post_type ) {
$query .= " AND post_type = %s ";
$query_args[] = $post_type;
}
else {
$query .= " AND post_type NOT IN ('nav_menu_item', 'attachment', 'revision') ";
}
$query .= " AND post_title LIKE '%%%s%%' ";
$query_args[] = $term;
}
$query .= " ORDER BY post_title LIMIT 10 ";
$query = $wpdb->prepare($query, $query_args);
$posts = $wpdb->get_results($query);
$post_types = self::get_post_types_options();
$response = array();
foreach ( $posts as $post ) {
$post_type_label = !empty($post_types[$post->post_type])? $post_types[$post->post_type] : $post->post_type;
$response[] = array(
'label' => "$post->post_title ($post_type_label)",
'value' => $post->post_title,
'post_type' => $post->post_type,
'post_id' => $post->ID,
);
}
$json = json_encode($response);
header( "Content-Type: application/json" );
echo $json;
exit();
}
}