Content Blocks (Custom Post Widget) - Version 2.0

Version Description

Fix for bug caused by the 'Advanced Custom Fields' plugin. Thanks to creativexperience for troubleshooting this issue. Cleanup of lightbox popup, changed all instances of query_posts to get_post and get_posts. Support for featured image in custom post widget area.

Download this release

Release Info

Developer vanderwijk
Plugin Icon 128x128 Content Blocks (Custom Post Widget)
Version 2.0
Comparing to
See all releases

Code changes from version 1.9.8 to 2.0

Files changed (4) hide show
  1. custom-post-widget.php +47 -47
  2. popup.php +47 -61
  3. post-widget.php +240 -240
  4. readme.txt +6 -1
custom-post-widget.php CHANGED
@@ -1,48 +1,48 @@
1
- <?php
2
- /*
3
- Plugin Name: Custom Post Widget
4
- Plugin URI: http://www.vanderwijk.com/services/web-design/wordpress-custom-post-widget/
5
- Description: Show the content of a custom post of the type 'content_block' in a widget.
6
- Version: 1.9.8
7
- Author: Johan van der Wijk
8
- Author URI: http://www.vanderwijk.com
9
- License: GPL2
10
-
11
- Release notes: Version 1.9.8 Fix for debug message when using shortcode with the WPML plugin
12
-
13
- Copyright 2012 Johan van der Wijk (email: info@vanderwijk.com)
14
-
15
- This program is free software; you can redistribute it and/or modify
16
- it under the terms of the GNU General Public License, version 2, as
17
- published by the Free Software Foundation.
18
-
19
- This program is distributed in the hope that it will be useful,
20
- but WITHOUT ANY WARRANTY; without even the implied warranty of
21
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
- GNU General Public License for more details.
23
-
24
- You should have received a copy of the GNU General Public License
25
- along with this program; if not, write to the Free Software
26
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27
- */
28
-
29
- // Set constant path to the custom-post-widget plugin directory.
30
- define( 'CUSTOM_POST_WIDGET_DIR', plugin_dir_path( __FILE__ ) );
31
- define( 'CUSTOM_POST_WIDGET_URL', WP_PLUGIN_URL.'/'.str_replace(basename( __FILE__),'',plugin_basename(__FILE__)) );
32
-
33
- // Launch the plugin.
34
- add_action( 'plugins_loaded', 'custom_post_widget_plugin_init' );
35
-
36
- // Load the required files needed for the plugin to run in the proper order and add needed functions to the required hooks.
37
- function custom_post_widget_plugin_init() {
38
- // Load the translation of the plugin.
39
- load_plugin_textdomain( 'custom-post-widget', false, 'custom-post-widget/languages' );
40
- add_action( 'widgets_init', 'custom_post_widget_load_widgets' );
41
- }
42
-
43
- // Loads the widgets packaged with the plugin.
44
- function custom_post_widget_load_widgets() {
45
- require_once( CUSTOM_POST_WIDGET_DIR . '/post-widget.php' );
46
- register_widget( 'custom_post_widget' );
47
- }
48
  ?>
1
+ <?php
2
+ /*
3
+ Plugin Name: Custom Post Widget
4
+ Plugin URI: http://www.vanderwijk.com/services/web-design/wordpress-custom-post-widget/
5
+ Description: Show the content of a custom post of the type 'content_block' in a widget.
6
+ Version: 2.0
7
+ Author: Johan van der Wijk
8
+ Author URI: http://www.vanderwijk.com
9
+ License: GPL2
10
+
11
+ Release notes: Version 2.0 Added support for featured images
12
+
13
+ Copyright 2012 Johan van der Wijk (email: info@vanderwijk.com)
14
+
15
+ This program is free software; you can redistribute it and/or modify
16
+ it under the terms of the GNU General Public License, version 2, as
17
+ published by the Free Software Foundation.
18
+
19
+ This program is distributed in the hope that it will be useful,
20
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
21
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
+ GNU General Public License for more details.
23
+
24
+ You should have received a copy of the GNU General Public License
25
+ along with this program; if not, write to the Free Software
26
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27
+ */
28
+
29
+ // Set constant path to the custom-post-widget plugin directory.
30
+ define( 'CUSTOM_POST_WIDGET_DIR', plugin_dir_path( __FILE__ ) );
31
+ define( 'CUSTOM_POST_WIDGET_URL', WP_PLUGIN_URL.'/'.str_replace(basename( __FILE__),'',plugin_basename(__FILE__)) );
32
+
33
+ // Launch the plugin.
34
+ add_action( 'plugins_loaded', 'custom_post_widget_plugin_init' );
35
+
36
+ // Load the required files needed for the plugin to run in the proper order and add needed functions to the required hooks.
37
+ function custom_post_widget_plugin_init() {
38
+ // Load the translation of the plugin.
39
+ load_plugin_textdomain( 'custom-post-widget', false, 'custom-post-widget/languages' );
40
+ add_action( 'widgets_init', 'custom_post_widget_load_widgets' );
41
+ }
42
+
43
+ // Loads the widgets packaged with the plugin.
44
+ function custom_post_widget_load_widgets() {
45
+ require_once( CUSTOM_POST_WIDGET_DIR . '/post-widget.php' );
46
+ register_widget( 'custom_post_widget' );
47
+ }
48
  ?>
popup.php CHANGED
@@ -1,61 +1,47 @@
1
- <?php // Action target that displays the popup to insert a content block to a post/page
2
-
3
- function add_content_block_popup(){ ?>
4
-
5
- <script>
6
- function InsertContentBlockForm(){
7
- var content_id = jQuery("#add_content_block_id").val();
8
- if(content_id == ""){
9
- alert("<?php _e("Please select a Content Block", 'custom-post-widget') ?>");
10
- return;
11
- }
12
- var win = window.dialogArguments || opener || parent || top;
13
- win.send_to_editor("[content_block id=" + content_id + "]");
14
- }
15
- </script>
16
-
17
- <div id="content_block_form" style="display:none;">
18
- <div class="wrap">
19
- <div>
20
- <div style="padding:15px 15px 0 15px;">
21
- <h3 style="color:#5A5A5A!important; font-family:Georgia,Times New Roman,Times,serif!important; font-size:1.8em!important; font-weight:normal!important;">
22
- <?php _e("Insert Content Block", 'custom-post-widget'); ?>
23
- </h3>
24
- <span>
25
- <?php _e("Select a Content Block below to add it to your post or page.", 'custom-post-widget'); ?>
26
- </span>
27
- </div>
28
- <div style="padding:15px 15px 0 15px;">
29
- <select id="add_content_block_id">
30
- <option value="">
31
- <?php _e("Select a Content Block", 'custom-post-widget'); ?>
32
- </option>
33
- <?php query_posts('post_type=content_block&orderby=ID&order=ASC&showposts=-1');
34
- if ( have_posts() ) : while ( have_posts() ) : the_post();
35
- $currentID = get_the_ID();
36
- // Add support for WPML Plugin.
37
- if ( function_exists( 'icl_object_id' ) ){
38
- $custom_post_id = icl_object_id( $custom_post_id, 'content_block', true );
39
- }
40
- if($currentID == $custom_post_id)
41
- $extra = 'selected' and
42
- $widgetExtraTitle = get_the_title();
43
- else
44
- $extra = '';
45
- echo '<option value="'.$currentID.'" '.$extra.'>'.get_the_title().'</option>';
46
- endwhile; else:
47
- echo '<option value="empty">' . __('No content blocks available', 'custom-post-widget') . '</option>';
48
- endif; ?>
49
- </select>
50
- <br />
51
- <div style="padding:15px;">
52
- <input type="button" class="button-primary" value="<?php _e("Insert Content Block", 'custom-post-widget') ?>" onclick="InsertContentBlockForm();"/>
53
- &nbsp;&nbsp;&nbsp; <a class="button" style="color:#bbb;" href="#" onclick="tb_remove(); return false;">
54
- <?php _e("Cancel", 'custom-post-widget'); ?>
55
- </a>
56
- </div>
57
- </div>
58
- </div>
59
- </div>
60
- </div>
61
- <?php } ?>
1
+ <?php
2
+ // Displays the lightbox popup to insert a content block shortcode to a post/page
3
+ function add_content_block_popup() { ?>
4
+
5
+ <script>
6
+ function InsertContentBlockForm() {
7
+ var content_id = jQuery("#add-content-block-id").val();
8
+ if(content_id == "") {
9
+ alert("<?php _e( 'Please select a Content Block', 'custom-post-widget' ); ?>");
10
+ return;
11
+ }
12
+ var win = window.dialogArguments || opener || parent || top;
13
+ win.send_to_editor("[content_block id=" + content_id + "]");
14
+ }
15
+ </script>
16
+
17
+ <div id="content-block-form" style="display: none;">
18
+ <h3>
19
+ <?php _e( 'Insert Content Block', 'custom-post-widget' ); ?>
20
+ </h3>
21
+ <p>
22
+ <?php _e( 'Select a Content Block below to add it to your post or page.', 'custom-post-widget' ); ?>
23
+ </p>
24
+ <p>
25
+ <select id="add-content-block-id">
26
+ <option value="">
27
+ <?php _e( 'Select a Content Block', 'custom-post-widget' ); ?>
28
+ </option>
29
+ <?php
30
+ $args = array('post_type' => 'content_block', 'suppress_filters' => 0, 'numberposts' => -1, 'order' => 'ASC');
31
+ $content_block = get_posts( $args );
32
+ if ($content_block) {
33
+ foreach( $content_block as $content_block ) : setup_postdata( $content_block );
34
+ echo '<option value="' . $content_block->ID . '">' . $content_block->post_title . '</option>';
35
+ endforeach;
36
+ } else {
37
+ echo '<option value="">' . __( 'No content blocks available', 'custom-post-widget' ) . '</option>';
38
+ };
39
+ ?>
40
+ </select>
41
+ </p>
42
+ <p>
43
+ <input type="button" class="button-primary" value="<?php _e( 'Insert Content Block', 'custom-post-widget' ) ?>" onclick="InsertContentBlockForm();"/>
44
+ </p>
45
+ </div>
46
+
47
+ <?php };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
post-widget.php CHANGED
@@ -1,240 +1,240 @@
1
- <?php
2
-
3
- // Add featured image support
4
- if ( function_exists( 'add_theme_support' ) ) {
5
- add_theme_support( 'post-thumbnails' );
6
- }
7
-
8
- // First create the widget for the admin panel
9
- class custom_post_widget extends WP_Widget {
10
- function custom_post_widget() {
11
- $widget_ops = array( 'description' => __( 'Displays custom post content in a widget', 'custom-post-widget' ) );
12
- $this->WP_Widget( 'custom_post_widget', __( 'Content Block', 'custom-post-widget' ), $widget_ops );
13
- }
14
-
15
- function form( $instance ) {
16
- $custom_post_id = ''; // initialize the variable
17
- if (isset($instance['custom_post_id'])) {
18
- $custom_post_id = esc_attr($instance['custom_post_id']);
19
- };
20
- $show_custom_post_title = isset( $instance['show_custom_post_title'] ) ? $instance['show_custom_post_title'] : true;
21
- $show_featured_image = isset( $instance['show_featured_image'] ) ? $instance['show_featured_image'] : true;
22
- $apply_content_filters = isset( $instance['apply_content_filters'] ) ? $instance['apply_content_filters'] : true;
23
- ?>
24
-
25
- <p>
26
- <label for="<?php echo $this->get_field_id( 'custom_post_id' ); ?>"> <?php echo __( 'Content Block to Display:', 'custom-post-widget' ) ?>
27
- <select class="widefat" id="<?php echo $this->get_field_id( 'custom_post_id' ); ?>" name="<?php echo $this->get_field_name( 'custom_post_id' ); ?>">
28
- <?php query_posts('post_type=content_block&orderby=ID&order=ASC&showposts=-1');
29
- if ( have_posts() ) : while ( have_posts() ) : the_post();
30
- $currentID = get_the_ID();
31
- if( $currentID == $custom_post_id )
32
- $extra = 'selected' and
33
- $widgetExtraTitle = get_the_title();
34
- else
35
- $extra = '';
36
- echo '<option value="'.$currentID.'" '.$extra.'>'.get_the_title().'</option>';
37
- endwhile; else:
38
- echo '<option value="empty">' . __( 'No content blocks available', 'custom-post-widget' ) . '</option>';
39
- endif; ?>
40
- </select>
41
- </label>
42
- </p>
43
-
44
- <input type="hidden" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $widgetExtraTitle; ?>" />
45
-
46
- <p>
47
- <?php
48
- echo '<a href="post.php?post=' . $custom_post_id . '&action=edit">' . __( 'Edit Content Block', 'custom-post-widget' ) . '</a>' ;
49
- ?>
50
- </p>
51
-
52
- <?php wp_reset_query(); ?>
53
-
54
- <p>
55
- <input class="checkbox" type="checkbox" <?php checked( (bool) isset( $instance['show_custom_post_title'] ), true ); ?> id="<?php echo $this->get_field_id( 'show_custom_post_title' ); ?>" name="<?php echo $this->get_field_name( 'show_custom_post_title' ); ?>" />
56
- <label for="<?php echo $this->get_field_id( 'show_custom_post_title' ); ?>"><?php echo __( 'Show Post Title', 'custom-post-widget' ) ?></label>
57
- </p>
58
-
59
- <p>
60
- <input class="checkbox" type="checkbox" <?php checked( (bool) isset( $instance['show_featured_image'] ), true ); ?> id="<?php echo $this->get_field_id( 'show_featured_image' ); ?>" name="<?php echo $this->get_field_name( 'show_featured_image' ); ?>" />
61
- <label for="<?php echo $this->get_field_id( 'show_featured_image' ); ?>"><?php echo __( 'Show featured image', 'custom-post-widget' ) ?></label>
62
- </p>
63
-
64
- <p>
65
- <input class="checkbox" type="checkbox" <?php checked( (bool) isset( $instance['apply_content_filters'] ), true ); ?> id="<?php echo $this->get_field_id( 'apply_content_filters' ); ?>" name="<?php echo $this->get_field_name( 'apply_content_filters' ); ?>" />
66
- <label for="<?php echo $this->get_field_id( 'apply_content_filters' ); ?>"><?php echo __( 'Do not apply content filters', 'custom-post-widget' ) ?></label>
67
- </p> <?php
68
- }
69
-
70
- function update( $new_instance, $old_instance ) {
71
- $instance = $old_instance;
72
- $instance['custom_post_id'] = strip_tags( $new_instance['custom_post_id'] );
73
- $instance['show_custom_post_title'] = $new_instance['show_custom_post_title'];
74
- $instance['show_featured_image'] = $new_instance['show_featured_image'];
75
- $instance['apply_content_filters'] = $new_instance['apply_content_filters'];
76
- return $instance;
77
- }
78
-
79
- function widget($args, $instance) {
80
- extract($args);
81
- $custom_post_id = ( $instance['custom_post_id'] != '' ) ? esc_attr($instance['custom_post_id']) : __('Find', 'custom-post-widget');
82
- // Add support for WPML Plugin.
83
- if ( function_exists( 'icl_object_id' ) ){
84
- $custom_post_id = icl_object_id( $custom_post_id, 'content_block', true );
85
- }
86
- // Variables from the widget settings.
87
- $show_custom_post_title = isset( $instance['show_custom_post_title'] ) ? $instance['show_custom_post_title'] : false;
88
- $show_featured_image = isset($instance['show_featured_image']) ? $instance['show_featured_image'] : false;
89
- $apply_content_filters = isset($instance['apply_content_filters']) ? $instance['apply_content_filters'] : false;
90
-
91
- // Output the query to find the custom post
92
- query_posts( 'post_type=content_block&p=' . $custom_post_id );
93
- while (have_posts()) : the_post();
94
- echo $before_widget;
95
- if ( $show_custom_post_title ) {
96
- echo $before_title;
97
- the_title();
98
- echo $after_title; // This is the line that displays the title (only if show title is set)
99
- }
100
- if ( $show_featured_image ) {
101
- the_post_thumbnail();
102
- }
103
- if ( $apply_content_filters ) { // Don't apply the content filter if checkbox selected
104
- remove_filter('the_content', 'wpautop');
105
- the_content(); // This is where the actual content of the custom post is being displayed
106
-
107
- //remove_all_filters('the_content', 'plugin_filters');
108
- //add_filter('the_content', 'do_shortcode');
109
-
110
- } else {
111
- the_content(); // This is where the actual content of the custom post is being displayed
112
- }
113
- echo $after_widget;
114
- endwhile;
115
- wp_reset_query();
116
- }
117
-
118
- }
119
-
120
- // Create the Content Block custom post type
121
- add_action('init', 'my_content_block_post_type_init');
122
-
123
- function my_content_block_post_type_init() {
124
- $labels = array(
125
- 'name' => _x('Content Blocks', 'post type general name', 'custom-post-widget'),
126
- 'singular_name' => _x('Content Block', 'post type singular name', 'custom-post-widget'),
127
- 'plural_name' => _x('Content Blocks', 'post type plural name', 'custom-post-widget'),
128
- 'add_new' => _x('Add Content Block', 'block', 'custom-post-widget'),
129
- 'add_new_item' => __('Add New Content Block', 'custom-post-widget'),
130
- 'edit_item' => __('Edit Content Block', 'custom-post-widget'),
131
- 'new_item' => __('New Content Block', 'custom-post-widget'),
132
- 'view_item' => __('View Content Block', 'custom-post-widget'),
133
- 'search_items' => __('Search Content Blocks', 'custom-post-widget'),
134
- 'not_found' => __('No Content Blocks Found', 'custom-post-widget'),
135
- 'not_found_in_trash' => __('No Content Blocks found in Trash', 'custom-post-widget'),
136
- 'parent_item_colon' => ''
137
- );
138
- $options = array(
139
- 'labels' => $labels,
140
- 'public' => false,
141
- 'publicly_queryable' => false,
142
- 'exclude_from_search' => true,
143
- 'show_ui' => true,
144
- 'query_var' => true,
145
- 'rewrite' => true,
146
- 'capability_type' => 'post',
147
- 'hierarchical' => false,
148
- 'menu_position' => null,
149
- 'supports' => array('title','editor','revisions','thumbnail','author')
150
- );
151
- register_post_type('content_block',$options);
152
- }
153
-
154
- // Add custom styles to admin screen and menu
155
- add_action('admin_head', 'content_block_header');
156
-
157
- function content_block_header() {
158
- global $post_type; ?>
159
- <style type="text/css">
160
- <!--
161
- <?php if (($post_type == 'content_block')) : ?>
162
- #icon-edit { background:transparent url('<?php echo CUSTOM_POST_WIDGET_URL; ?>images/contentblock-32.png') no-repeat 0 0 !important;}
163
- #minor-publishing-actions { display:none; /* Hide the Save Draft and Preview buttons */}
164
- <?php endif; ?>
165
- #adminmenu #menu-posts-content_block div.wp-menu-image{background:transparent url('<?php echo CUSTOM_POST_WIDGET_URL;?>images/contentblock.png') no-repeat center -32px;}
166
- #adminmenu #menu-posts-content_block:hover div.wp-menu-image,#adminmenu #menu-posts-content_block.wp-has-current-submenu div.wp-menu-image{background:transparent url('<?php echo CUSTOM_POST_WIDGET_URL;?>images/contentblock.png') no-repeat center 0px;}
167
- -->
168
- </style>
169
- <?php
170
- }
171
-
172
- add_filter('post_updated_messages', 'content_block_messages');
173
-
174
- function content_block_messages( $messages ) {
175
- $messages['content_block'] = array(
176
- 0 => '',
177
- 1 => current_user_can( 'edit_theme_options' ) ? sprintf( __('Content Block updated. <a href="%s">Manage Widgets</a>', 'custom-post-widget'), esc_url( 'widgets.php' ) ) : sprintf( __('Content Block updated.', 'custom-post-widget'), esc_url( 'widgets.php' ) ),
178
- 2 => __('Custom field updated.', 'custom-post-widget'),
179
- 3 => __('Custom field deleted.', 'custom-post-widget'),
180
- 4 => __('Content Block updated.', 'custom-post-widget'),
181
- 5 => isset($_GET['revision']) ? sprintf( __('Content Block restored to revision from %s', 'custom-post-widget'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
182
- 6 => current_user_can( 'edit_theme_options' ) ? sprintf( __('Content Block published. <a href="%s">Manage Widgets</a>', 'custom-post-widget'), esc_url( 'widgets.php' ) ) : sprintf( __('Content Block published.', 'custom-post-widget'), esc_url( 'widgets.php' ) ),
183
- 7 => __('Block saved.', 'custom-post-widget'),
184
- 8 => current_user_can( 'edit_theme_options' ) ? sprintf( __('Content Block submitted. <a href="%s">Manage Widgets</a>', 'custom-post-widget'), esc_url( 'widgets.php' ) ) : sprintf( __('Content Block submitted.', 'custom-post-widget'), esc_url( 'widgets.php' ) ),
185
- 9 => sprintf( __('Content Block scheduled for: <strong>%1$s</strong>.', 'custom-post-widget'), date_i18n( __( 'M j, Y @ G:i' , 'custom-post-widget'), strtotime(isset($post->post_date) ? $post->post_date : null) ), esc_url( 'widgets.php' ) ),
186
- 10 => current_user_can( 'edit_theme_options' ) ? sprintf( __('Content Block draft updated. <a href="%s">Manage Widgets</a>', 'custom-post-widget'), esc_url( 'widgets.php' ) ) : sprintf( __('Content Block draft updated.', 'custom-post-widget'), esc_url( 'widgets.php' ) ),
187
- );
188
- return $messages;
189
- }
190
-
191
- // Add the ability to display the content block in a reqular post using a shortcode
192
- function custom_post_widget_shortcode($atts) {
193
- extract(shortcode_atts(array(
194
- 'id' => '',
195
- 'class' => 'content_block'
196
- ), $atts));
197
-
198
- $content = "";
199
-
200
- if($id != "") {
201
- $args = array(
202
- 'post__in' => array($id),
203
- 'post_type' => 'content_block',
204
- );
205
-
206
- $content_post = get_posts($args);
207
-
208
- foreach( $content_post as $post ) :
209
- $content .= '<div class="'. esc_attr($class) .'">';
210
- $content .= apply_filters('the_content', $post->post_content);
211
- $content .= '</div>';
212
- endforeach;
213
- }
214
-
215
- return $content;
216
- }
217
- add_shortcode('content_block', 'custom_post_widget_shortcode');
218
-
219
- // Add button above editor if not editing content_block
220
- function add_content_block_icon($initcontext) {
221
- return $initcontext.
222
- '<a id="add_content_block" style="text-decoration:none;" class="thickbox" title="' . __("Add Content Block", 'custom-post-widget') . '" href="' . CUSTOM_POST_WIDGET_URL . 'popup.php?type=add_content_block_popup&amp;TB_inline=true&amp;inlineId=content_block_form">
223
- <img onclick="return false;" alt="' . __("Add Content Block", 'custom-post-widget') . '" src="' . CUSTOM_POST_WIDGET_URL . 'images/contentblock-13.png">
224
- </a>';
225
- }
226
- // Only add content_block icon above posts and pages
227
- function check_post_type_and_remove_media_buttons() {
228
- global $current_screen;
229
- if( 'content_block' != $current_screen->post_type ) add_filter('media_buttons_context', 'add_content_block_icon' );
230
- }
231
- add_action('admin_head','check_post_type_and_remove_media_buttons');
232
-
233
- require_once( CUSTOM_POST_WIDGET_DIR . '/popup.php' );
234
-
235
- if(!defined( 'CUSTOM_POST_WIDGET_CURRENT_PAGE' ))
236
- define( 'CUSTOM_POST_WIDGET_CURRENT_PAGE', basename($_SERVER['PHP_SELF']) );
237
- if(in_array(CUSTOM_POST_WIDGET_CURRENT_PAGE, array('post.php', 'page.php', 'page-new.php', 'post-new.php'))) {
238
- add_action('admin_footer', 'add_content_block_popup');
239
- }
240
- ?>
1
+ <?php
2
+
3
+ // Add featured image support
4
+ if ( function_exists( 'add_theme_support' ) ) {
5
+ add_theme_support( 'post-thumbnails' );
6
+ }
7
+
8
+ // First create the widget for the admin panel
9
+ class custom_post_widget extends WP_Widget {
10
+ function custom_post_widget() {
11
+ $widget_ops = array( 'description' => __( 'Displays custom post content in a widget', 'custom-post-widget' ) );
12
+ $this->WP_Widget( 'custom_post_widget', __( 'Content Block', 'custom-post-widget' ), $widget_ops );
13
+ }
14
+
15
+ function form( $instance ) {
16
+ $custom_post_id = ''; // Initialize the variable
17
+ if (isset($instance['custom_post_id'])) {
18
+ $custom_post_id = esc_attr($instance['custom_post_id']);
19
+ };
20
+ $show_custom_post_title = isset( $instance['show_custom_post_title'] ) ? $instance['show_custom_post_title'] : true;
21
+ $show_featured_image = isset( $instance['show_featured_image'] ) ? $instance['show_featured_image'] : true;
22
+ $apply_content_filters = isset( $instance['apply_content_filters'] ) ? $instance['apply_content_filters'] : true;
23
+ ?>
24
+
25
+ <p>
26
+ <label for="<?php echo $this->get_field_id( 'custom_post_id' ); ?>"> <?php echo __( 'Content Block to Display:', 'custom-post-widget' ) ?>
27
+ <select class="widefat" id="<?php echo $this->get_field_id( 'custom_post_id' ); ?>" name="<?php echo $this->get_field_name( 'custom_post_id' ); ?>">
28
+ <?php
29
+ $args = array('post_type' => 'content_block', 'suppress_filters' => 0, 'numberposts' => -1, 'order' => 'ASC');
30
+ $content_block = get_posts( $args );
31
+ if ($content_block) {
32
+ foreach( $content_block as $content_block ) : setup_postdata( $content_block );
33
+ echo '<option value="' . $content_block->ID . '"';
34
+ if( $custom_post_id == $content_block->ID ) {
35
+ echo ' selected';
36
+ $widgetExtraTitle = $content_block->post_title;
37
+ };
38
+ echo '>' . $content_block->post_title . '</option>';
39
+ endforeach;
40
+ } else {
41
+ echo '<option value="">' . __( 'No content blocks available', 'custom-post-widget' ) . '</option>';
42
+ };
43
+ ?>
44
+ </select>
45
+ </label>
46
+ </p>
47
+
48
+ <input type="hidden" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $widgetExtraTitle ?>" />
49
+
50
+ <p>
51
+ <?php
52
+ echo '<a href="post.php?post=' . $custom_post_id . '&action=edit">' . __( 'Edit Content Block', 'custom-post-widget' ) . '</a>' ;
53
+ ?>
54
+ </p>
55
+
56
+ <p>
57
+ <input class="checkbox" type="checkbox" <?php checked( (bool) isset( $instance['show_custom_post_title'] ), true ); ?> id="<?php echo $this->get_field_id( 'show_custom_post_title' ); ?>" name="<?php echo $this->get_field_name( 'show_custom_post_title' ); ?>" />
58
+ <label for="<?php echo $this->get_field_id( 'show_custom_post_title' ); ?>"><?php echo __( 'Show Post Title', 'custom-post-widget' ) ?></label>
59
+ </p>
60
+
61
+ <p>
62
+ <input class="checkbox" type="checkbox" <?php checked( (bool) isset( $instance['show_featured_image'] ), true ); ?> id="<?php echo $this->get_field_id( 'show_featured_image' ); ?>" name="<?php echo $this->get_field_name( 'show_featured_image' ); ?>" />
63
+ <label for="<?php echo $this->get_field_id( 'show_featured_image' ); ?>"><?php echo __( 'Show featured image', 'custom-post-widget' ) ?></label>
64
+ </p>
65
+
66
+ <p>
67
+ <input class="checkbox" type="checkbox" <?php checked( (bool) isset( $instance['apply_content_filters'] ), true ); ?> id="<?php echo $this->get_field_id( 'apply_content_filters' ); ?>" name="<?php echo $this->get_field_name( 'apply_content_filters' ); ?>" />
68
+ <label for="<?php echo $this->get_field_id( 'apply_content_filters' ); ?>"><?php echo __( 'Do not apply content filters', 'custom-post-widget' ) ?></label>
69
+ </p> <?php
70
+ }
71
+
72
+ function update( $new_instance, $old_instance ) {
73
+ $instance = $old_instance;
74
+ $instance['custom_post_id'] = strip_tags( $new_instance['custom_post_id'] );
75
+ $instance['show_custom_post_title'] = $new_instance['show_custom_post_title'];
76
+ $instance['show_featured_image'] = $new_instance['show_featured_image'];
77
+ $instance['apply_content_filters'] = $new_instance['apply_content_filters'];
78
+ return $instance;
79
+ }
80
+
81
+ function widget($args, $instance) {
82
+ extract($args);
83
+ $custom_post_id = ( $instance['custom_post_id'] != '' ) ? esc_attr($instance['custom_post_id']) : __('Find', 'custom-post-widget');
84
+ // Add support for WPML Plugin.
85
+ if ( function_exists( 'icl_object_id' ) ){
86
+ $custom_post_id = icl_object_id( $custom_post_id, 'content_block', true );
87
+ }
88
+ // Variables from the widget settings.
89
+ $show_custom_post_title = isset( $instance['show_custom_post_title'] ) ? $instance['show_custom_post_title'] : false;
90
+ $show_featured_image = isset($instance['show_featured_image']) ? $instance['show_featured_image'] : false;
91
+ $apply_content_filters = isset($instance['apply_content_filters']) ? $instance['apply_content_filters'] : false;
92
+ $content_post = get_post($custom_post_id);
93
+ $content = $content_post->post_content;
94
+ if ( !$apply_content_filters ) { // Don't apply the content filter if checkbox selected
95
+ $content = apply_filters('the_content', $content);
96
+ }
97
+ echo $before_widget;
98
+ if ( $show_custom_post_title ) {
99
+ echo $before_title . apply_filters('widget_title',$content_post->post_title) . $after_title; // This is the line that displays the title (only if show title is set)
100
+ }
101
+ if ( $show_featured_image ) {
102
+ echo get_the_post_thumbnail($content_post->ID);
103
+ }
104
+ echo do_shortcode($content); // This is where the actual content of the custom post is being displayed
105
+ echo $after_widget;
106
+ }
107
+ }
108
+
109
+ // Create the Content Block custom post type
110
+ add_action('init', 'my_content_block_post_type_init');
111
+
112
+ function my_content_block_post_type_init() {
113
+ $labels = array(
114
+ 'name' => _x('Content Blocks', 'post type general name', 'custom-post-widget'),
115
+ 'singular_name' => _x('Content Block', 'post type singular name', 'custom-post-widget'),
116
+ 'plural_name' => _x('Content Blocks', 'post type plural name', 'custom-post-widget'),
117
+ 'add_new' => _x('Add Content Block', 'block', 'custom-post-widget'),
118
+ 'add_new_item' => __('Add New Content Block', 'custom-post-widget'),
119
+ 'edit_item' => __('Edit Content Block', 'custom-post-widget'),
120
+ 'new_item' => __('New Content Block', 'custom-post-widget'),
121
+ 'view_item' => __('View Content Block', 'custom-post-widget'),
122
+ 'search_items' => __('Search Content Blocks', 'custom-post-widget'),
123
+ 'not_found' => __('No Content Blocks Found', 'custom-post-widget'),
124
+ 'not_found_in_trash' => __('No Content Blocks found in Trash', 'custom-post-widget'),
125
+ 'parent_item_colon' => ''
126
+ );
127
+ $options = array(
128
+ 'labels' => $labels,
129
+ 'public' => false,
130
+ 'publicly_queryable' => false,
131
+ 'exclude_from_search' => true,
132
+ 'show_ui' => true,
133
+ 'query_var' => true,
134
+ 'rewrite' => true,
135
+ 'capability_type' => 'post',
136
+ 'hierarchical' => false,
137
+ 'menu_position' => null,
138
+ 'supports' => array('title','editor','revisions','thumbnail','author')
139
+ );
140
+ register_post_type('content_block',$options);
141
+ }
142
+
143
+ // Add custom styles to admin screen and menu
144
+ add_action('admin_head', 'content_block_header');
145
+
146
+ function content_block_header() {
147
+ global $post_type; ?>
148
+ <style type="text/css">
149
+ <!--
150
+ <?php if (($post_type == 'content_block')) : ?>
151
+ #icon-edit { background:transparent url('<?php echo CUSTOM_POST_WIDGET_URL; ?>images/contentblock-32.png') no-repeat 0 0 !important;}
152
+ #minor-publishing-actions { display:none; /* Hide the Save Draft and Preview buttons */}
153
+ <?php endif; ?>
154
+ #adminmenu #menu-posts-content_block div.wp-menu-image{background:transparent url('<?php echo CUSTOM_POST_WIDGET_URL;?>images/contentblock.png') no-repeat center -32px;}
155
+ #adminmenu #menu-posts-content_block:hover div.wp-menu-image,#adminmenu #menu-posts-content_block.wp-has-current-submenu div.wp-menu-image{background:transparent url('<?php echo CUSTOM_POST_WIDGET_URL;?>images/contentblock.png') no-repeat center 0px;}
156
+ -->
157
+ </style>
158
+ <?php
159
+ }
160
+
161
+ add_filter('post_updated_messages', 'content_block_messages');
162
+
163
+ function content_block_messages( $messages ) {
164
+ $messages['content_block'] = array(
165
+ 0 => '',
166
+ 1 => current_user_can( 'edit_theme_options' ) ? sprintf( __('Content Block updated. <a href="%s">Manage Widgets</a>', 'custom-post-widget'), esc_url( 'widgets.php' ) ) : sprintf( __('Content Block updated.', 'custom-post-widget'), esc_url( 'widgets.php' ) ),
167
+ 2 => __('Custom field updated.', 'custom-post-widget'),
168
+ 3 => __('Custom field deleted.', 'custom-post-widget'),
169
+ 4 => __('Content Block updated.', 'custom-post-widget'),
170
+ 5 => isset($_GET['revision']) ? sprintf( __('Content Block restored to revision from %s', 'custom-post-widget'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
171
+ 6 => current_user_can( 'edit_theme_options' ) ? sprintf( __('Content Block published. <a href="%s">Manage Widgets</a>', 'custom-post-widget'), esc_url( 'widgets.php' ) ) : sprintf( __('Content Block published.', 'custom-post-widget'), esc_url( 'widgets.php' ) ),
172
+ 7 => __('Block saved.', 'custom-post-widget'),
173
+ 8 => current_user_can( 'edit_theme_options' ) ? sprintf( __('Content Block submitted. <a href="%s">Manage Widgets</a>', 'custom-post-widget'), esc_url( 'widgets.php' ) ) : sprintf( __('Content Block submitted.', 'custom-post-widget'), esc_url( 'widgets.php' ) ),
174
+ 9 => sprintf( __('Content Block scheduled for: <strong>%1$s</strong>.', 'custom-post-widget'), date_i18n( __( 'M j, Y @ G:i' , 'custom-post-widget'), strtotime(isset($post->post_date) ? $post->post_date : null) ), esc_url( 'widgets.php' ) ),
175
+ 10 => current_user_can( 'edit_theme_options' ) ? sprintf( __('Content Block draft updated. <a href="%s">Manage Widgets</a>', 'custom-post-widget'), esc_url( 'widgets.php' ) ) : sprintf( __('Content Block draft updated.', 'custom-post-widget'), esc_url( 'widgets.php' ) ),
176
+ );
177
+ return $messages;
178
+ }
179
+
180
+ // Add the ability to display the content block in a reqular post using a shortcode
181
+ function custom_post_widget_shortcode($atts) {
182
+ extract(shortcode_atts(array(
183
+ 'id' => '',
184
+ 'class' => 'content_block'
185
+ ), $atts));
186
+
187
+ $content = "";
188
+
189
+ if($id != "") {
190
+ $args = array(
191
+ 'post__in' => array($id),
192
+ 'post_type' => 'content_block',
193
+ );
194
+
195
+ $content_post = get_posts($args);
196
+
197
+ foreach( $content_post as $post ) :
198
+ $content .= '<div class="'. esc_attr($class) .'">';
199
+ $content .= apply_filters('the_content', $post->post_content);
200
+ $content .= '</div>';
201
+ endforeach;
202
+ }
203
+
204
+ return $content;
205
+ }
206
+ add_shortcode('content_block', 'custom_post_widget_shortcode');
207
+
208
+ // Add button above editor if not editing content_block
209
+ function add_content_block_icon() {
210
+ echo '<style>
211
+ #add-content-block .wp-media-buttons-icon {
212
+ background: url(../../wp-content/plugins/custom-post-widget/images/contentblock.png) no-repeat -7px -40px;
213
+ margin-right: 3px;
214
+ }
215
+ #add-content-block:hover .wp-media-buttons-icon {
216
+ background: url(../../wp-content/plugins/custom-post-widget/images/contentblock.png) no-repeat -7px -8px;
217
+ }
218
+ #add-content-block {
219
+ padding-left: 0.4em;
220
+ }
221
+ </style>
222
+ <a id="add-content-block" class="button thickbox" title="' . __("Add Content Block", 'custom-post-widget') . '" href="' . CUSTOM_POST_WIDGET_URL . 'popup.php?type=add_content_block_popup&amp;TB_inline=true&amp;inlineId=content-block-form">
223
+ <span class="wp-media-buttons-icon"></span>' . __("Add Content Block", "custom-post-widget") . '</a>';
224
+ }
225
+
226
+ // Only add content_block icon above posts and pages
227
+ function check_post_type_and_remove_media_buttons() {
228
+ global $current_screen;
229
+ if( 'content_block' != $current_screen->post_type ) add_filter('media_buttons_context', 'add_content_block_icon' );
230
+ }
231
+ add_action('admin_head','check_post_type_and_remove_media_buttons');
232
+
233
+ require_once( CUSTOM_POST_WIDGET_DIR . '/popup.php' );
234
+
235
+ // Only add content block popup action on page and post edit
236
+ if(!defined( 'CUSTOM_POST_WIDGET_CURRENT_PAGE' ))
237
+ define( 'CUSTOM_POST_WIDGET_CURRENT_PAGE', basename($_SERVER['PHP_SELF']) );
238
+ if(in_array(CUSTOM_POST_WIDGET_CURRENT_PAGE, array('post.php', 'page.php', 'page-new.php', 'post-new.php'))) {
239
+ add_action('admin_footer', 'add_content_block_popup');
240
+ }
readme.txt CHANGED
@@ -4,7 +4,7 @@ Author URI: http://www.vanderwijk.com/
4
  Donate link: http://www.vanderwijk.com/wordpress/support/
5
  Tags: custom-post, widget, sidebar, content block, content, block, custom, post, shortcode
6
  Requires at least: 2.9.2
7
- Tested up to: 3.4.1
8
  Stable tag: 1.9.5
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
@@ -79,6 +79,11 @@ If your social media sharing plugin adds buttons to the widget areas you could c
79
 
80
  == Changelog ==
81
 
 
 
 
 
 
82
  = 1.9.8 =
83
  Fix for error when using shortcode with the WPML plugin
84
 
4
  Donate link: http://www.vanderwijk.com/wordpress/support/
5
  Tags: custom-post, widget, sidebar, content block, content, block, custom, post, shortcode
6
  Requires at least: 2.9.2
7
+ Tested up to: 3.5
8
  Stable tag: 1.9.5
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
79
 
80
  == Changelog ==
81
 
82
+ = 2.0 =
83
+ Fix for bug caused by the 'Advanced Custom Fields' plugin. Thanks to creativexperience for troubleshooting this issue.
84
+ Cleanup of lightbox popup, changed all instances of query_posts to get_post and get_posts.
85
+ Support for featured image in custom post widget area.
86
+
87
  = 1.9.8 =
88
  Fix for error when using shortcode with the WPML plugin
89