-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpagelets.php
More file actions
executable file
·245 lines (184 loc) · 7.26 KB
/
pagelets.php
File metadata and controls
executable file
·245 lines (184 loc) · 7.26 KB
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
<?php
/*
Plugin Name: UW Pagelets
Plugin URI: https://github.com/uweb/Pagelets
Description: Adds a custom post type that can be displayed on certain pages as a widget.
Version: 2.0
Author: Dane Odekirk
Author URI: http://daneodekirk.com
*/
if ( !class_exists( "Pagelets" ) )
{
class Pagelets {
const slug = 'pagelet';
public function __construct() {
add_action('init', array($this, 'register_pagelets'), 8);
add_filter( 'post_updated_messages', array( $this, 'pagelets_updated_messages' ) );
add_action( 'add_meta_boxes', array( $this, 'pagelets_add_custom_box' ));
add_action( 'save_post', array( $this, 'pagelet_save_postdata' ));
add_action( 'widgets_init', array($this, 'register_pagelet_widget'));
add_filter( 'manage_pagelet_posts_columns', array( $this, 'add_shortcode_column' ) );
add_action( 'manage_posts_custom_column' , array( $this, 'add_shortcode_column_content' ) , 10, 2 );
}
function register_pagelets()
{
$labels = array(
'name' => _x('Pagelets', 'post type general name'),
'singular_name' => _x('Pagelet', 'post type singular name'),
'add_new' => _x('Add New', 'pagelet'),
'add_new_item' => __('Add New Pagelet'),
'edit_item' => __('Edit Pagelet'),
'new_item' => __('New Pagelet'),
'all_items' => __('Pagelets'),
'view_item' => __('View Pagelet'),
'search_items' => __('Search Pagelets'),
'not_found' => __('No pagelets found'),
'not_found_in_trash' => __('No pagelets found in Trash'),
'parent_item_colon' => '',
'menu_name' => __('Pagelets')
);
$args = array(
'label' => 'Pagelets',
'labels' => $labels,
'public' => false,
'publicly_queryable' => false,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'page',
'has_archive' => false,
'hierarchical' => false,
'menu_position' => false,
'show_in_menu' => 'edit.php?post_type=page',
'supports' => array( 'title', 'editor', 'author', 'revisions' )
);
register_post_type('pagelet',$args);
}
function pagelets_updated_messages( $messages )
{
global $post, $post_ID;
$messages['pagelet'] = array(
0 => '', // Unused. Messages start at index 1.
1 => __('Pagelet updated.'),
2 => __('Custom field updated.'),
3 => __('Custom field deleted.'),
4 => __('Pagelet updated.'),
/* translators: %s: date and time of the revision */
5 => isset($_GET['revision']) ? sprintf( __('Pagelet restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => __('Pagelet published'),
7 => __('Pagelet saved.'),
8 => __('Pagelet submitted.'),
9 => sprintf( __('Pagelet scheduled for: <strong>%1$s</strong>'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ) ),
10 => __('Pagelet draft updated.')
);
return $messages;
}
function pagelets_add_custom_box()
{
add_meta_box(
'pagelets',
__( 'Pagelet Sidebar', 'pagelets' ),
array( $this, 'pagelet_metabox_html'),
'page', 'side'
);
}
function pagelet_metabox_html( $post )
{
$pagelet = get_post_meta($post->ID, 'pagelet', true);
$slug = Pagelets::slug;
wp_nonce_field( plugin_basename( __FILE__ ), "{$slug}_nonce" );
echo '<style type="text/css"> .custom-combobox { position:relative; display:inline-block; } .custom-combobox-toggle { position: absolute; top: 1px; bottom: 1px; margin-left:-1px; padding:0; }; </style>';
echo $this->list_pagelets($pagelet);
}
function list_pagelets($chosen)
{
$pagelets = get_posts('numberposts=-1&post_type=pagelet');
$html = '<select id="pagelet" name="pagelet">';
$html .= '<option value="none" ' . selected( $chosen, 'none', false) . '>None</option>';
foreach($pagelets as $index=>$pagelet) {
$html .= "<option value=\"$pagelet->ID\" " . selected( $chosen, $pagelet->ID , false) . " >$pagelet->post_title</option>";
}
return $html .= '</select>';
}
function pagelet_save_postdata($post_id)
{
$slug = Pagelets::slug;
$_POST += array("{$slug}_edit_nonce" => '');
if ( isset($_POST['post_type']) && 'page' != $_POST['post_type'] )
return;
if ( !current_user_can( 'edit_post', $post_id ) )
return;
if ( isset( $_POST["{$slug}_nonce"] ) && !wp_verify_nonce( $_POST["{$slug}_nonce"], plugin_basename( __FILE__ ) ) )
return;
if ( isset( $_REQUEST['pagelet'] ) ) {
update_post_meta( $post_id, 'pagelet', $_REQUEST['pagelet'] );
}
}
function register_pagelet_widget()
{
if ( !is_blog_installed() )
return;
register_widget('Pagelet_Widget');
}
function add_shortcode_column( $columns )
{
return array_merge( array_slice( $columns, 0, 2 ), array('p_shortcode'=>'Shortcode'), array_slice( $columns, 2, null ));
}
function add_shortcode_column_content( $column, $post_id )
{
if ( $column == 'p_shortcode' ) echo '[pagelet id='. $post_id .']';
}
}
new Pagelets;
}
if ( !class_exists( "Pagelet_Widget" ) )
{
class Pagelet_Widget extends WP_Widget
{
public function __construct() {
parent::__construct(
'pagelets_widget',
'Pagelets Widget',
array( 'classname' => 'pagelet_widget', 'description' => __( "Displays the pagelet attached to the current page"))
);
}
public function widget( $args, $instance ) {
global $post;
extract( $args );
$pagelet_id = get_post_meta($post->ID, 'pagelet', true);
if ( ! is_numeric($pagelet_id) ) return;
$pagelet = get_post($pagelet_id);
echo $before_widget;
echo $before_title . $pagelet->post_title . $after_title;
echo apply_filters('the_content', $pagelet->post_content);
if ( current_user_can('edit_post', $pagelet_id) )
echo '<span class="entry-meta edit-link pull-right"><a class="pull-right" target="_blank" href="' . get_edit_post_link($pagelet_id) . '">Edit Pagelet</a></span>';
echo $after_widget;
}
public function form( $instance ) {
echo "<p><small>When a pagelet is assigned to a page, this widget will be replaced by the pagelet's title and content.</small></p>";
}
}
}
if ( !class_exists( "Pagelet_Shortcode" ) )
{
class Pagelet_Shortcode
{
public function __construct() {
add_shortcode( 'pagelet', array($this, 'shortcode') );
}
public function shortcode( $atts ) {
$params = shortcode_atts(array(
'id'=>''
), $atts );
if ( ! is_numeric($params['id']) )
return '';
$pagelet = get_post($params['id']);
if ( empty($pagelet) || $pagelet->post_status != 'publish' )
return '';
$content = wpautop( $pagelet->post_content );
return $content;
}
}
new Pagelet_Shortcode;
}
?>