Advertisement
Guest User

Untitled

a guest
Jan 24th, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.06 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  
  5. * Plugin Name: Display Content Piece Widget
  6.  
  7. * Plugin URI: http://
  8.  
  9. * Description: A simple little widget that can display any piece of content (eg a single post, page or individual customly defined content type).
  10.  
  11. * Version: 0.1
  12.  
  13. * Author: Chris Egerton
  14.  
  15. * Author URI: http:// *
  16.  
  17. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  18.  
  19. */
  20.  
  21. /* Add the widget function to the WP hooks. */
  22.  
  23. add_action( 'widgets_init', 'custom_content_widget' );
  24.  
  25. add_action('admin_head', 'my_action_javascript');
  26.  
  27. add_action('wp_ajax_dcp_axajcallback', 'dcp_axajcallback');
  28.  
  29.  
  30.  
  31. //Javascript should be moved to external file
  32.  
  33. function my_action_javascript() {
  34.  
  35. ?>
  36.  
  37. <script type="text/javascript" >
  38.  
  39. jQuery(document).ready(function($) {
  40.  
  41. $(".ContentTypeSelect").live('change',function(){
  42.  
  43. var id = this.id;
  44.  
  45. var type = $('#'+id).val();
  46.  
  47. var data = {
  48.  
  49. action: 'dcp_axajcallback',
  50.  
  51. dcp_action: 'list_content',
  52.  
  53. dcp_content_type: type
  54.  
  55. };
  56.  
  57. jQuery.post(ajaxurl, data, function(response) {
  58.  
  59. var auxArr = [];
  60.  
  61. $.each(response, function(k,v){
  62.  
  63. auxArr[k] = "<option value='" + v.i + "'>" + v.t + "</option>";
  64.  
  65. });
  66.  
  67. $('#' + id + '_selected').empty().append(auxArr.join(''));
  68.  
  69. });
  70.  
  71. });
  72.  
  73. });
  74.  
  75. </script>
  76.  
  77. <?php
  78.  
  79. }
  80.  
  81. //jquery / json callback for select menu population
  82.  
  83. function dcp_axajcallback() {
  84.  
  85. global $wpdb;
  86.  
  87. switch ($_POST['dcp_action']) {
  88.  
  89. case 'get_post_types' :
  90.  
  91. if ($_POST['dcp_post_types'] == 'filtered') {
  92.  
  93. $exclude = array('nav_menu_item','revision','attachment');
  94.  
  95. $types = array_diff(get_post_types(), $exclude);
  96.  
  97. header('Content-type: application/json');
  98.  
  99. echo json_encode($types);
  100.  
  101. die;
  102.  
  103. }
  104.  
  105. break;
  106.  
  107. case 'list_content' :
  108.  
  109. if (isset($_POST['dcp_content_type'])) {
  110.  
  111. $type = $_POST['dcp_content_type'];
  112.  
  113. $query = "SELECT ID as i, post_title AS t FROM $wpdb->posts
  114.  
  115. WHERE post_type = '$type' AND post_status = 'publish'";
  116.  
  117. $posts = $wpdb->get_results($query);
  118.  
  119. header('Content-type: application/json');
  120.  
  121. echo json_encode($posts);
  122.  
  123. die;
  124.  
  125. }
  126.  
  127. break;
  128.  
  129. }
  130.  
  131. }
  132.  
  133. function custom_content_widget() {
  134.  
  135. register_widget( 'dcp_content_widget' );
  136.  
  137. }
  138.  
  139. class dcp_content_widget extends WP_Widget {
  140.  
  141. function dcp_content_widget() {
  142.  
  143. /* Widget settings. */
  144.  
  145. $widget_ops = array( 'classname' => 'dcp-content', 'description' => __( "Displays content associated with a single content piece (eg an individual post, page or customly defined content type). Install the Widget Context plugin to add further options about when and on which pages this content is displayed.") );
  146.  
  147. $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'dcp_content_widget' );
  148.  
  149. $this->WP_Widget( 'dcp_content_widget', __("Display Single Post"), $widget_ops, $control_ops );
  150.  
  151. }
  152.  
  153. //Widget Display Function Extension
  154.  
  155. function widget( $args, $instance ) {
  156.  
  157. extract( $args );
  158.  
  159. $contentid = $instance['type_list_selected'];
  160.  
  161. echo $before_widget;
  162.  
  163. ?>
  164.  
  165. <?php
  166.  
  167. $custom_page_content = get_post($contentid);
  168.  
  169. $title = apply_filters('the_title',$custom_page_content->post_title);
  170.  
  171. echo $before_title;
  172.  
  173.  
  174. ?>
  175.  
  176. <div class="dcp_content_title">FEATURED SHOWS</div>
  177.  
  178. <?php
  179.  
  180. echo $after_title;
  181.  
  182. $content = $custom_page_content->post_excerpt;
  183.  
  184. $content = apply_filters('the_content',$content);
  185.  
  186. ?>
  187.  
  188.  
  189.  
  190. <div class="sidecontent">
  191.  
  192. <a href="<?php the_permalink() ?>" rel="bookmark"><?php echo $thumb;?></a>
  193. <div class="dcp_content_body">
  194. <h4><a href="<?php the_permalink() ?>" rel="bookmark"><?php echo $title;?></a></h4>
  195. <?php echo $content;?>
  196.  
  197. </div>
  198.  
  199. </div>
  200.  
  201. <?php
  202.  
  203. echo $after_widget;
  204.  
  205. }
  206.  
  207. //Widget Settings Function Extension
  208.  
  209. function form( $instance ) {
  210.  
  211. if(!empty($instance['type_list_selected'])) {
  212.  
  213. $current_post = get_post_field( 'post_title', $instance['type_list_selected']);
  214.  
  215. ?>
  216.  
  217. <p>Current Article: <em><?php echo $current_post;?><br />(Post ID: <?php echo $instance['type_list_selected']; ?>)</em></p>
  218.  
  219. <?php
  220.  
  221. }
  222.  
  223. else {
  224.  
  225. ?>
  226.  
  227. <h5>Use this widget to display small pieces of content in a sidebar or footer area etc of your site. Ideal for displaying custom content types.</h5>
  228.  
  229. <?php
  230.  
  231. }
  232.  
  233. $defaults = array( 'type_list_selected' => '', 'display_title' => '');
  234.  
  235. $instance = wp_parse_args( (array) $instance, $defaults );
  236.  
  237. ?>
  238.  
  239. <h4>Select Content to display:</h4>
  240.  
  241. <p><label for="<?php echo $this->get_field_id( 'type_list' ); ?>">Content Type:</label>
  242.  
  243. <select class="ContentTypeSelect" id='<?php echo $this->get_field_id( 'type_list' ); ?>'>
  244.  
  245. <option value=''>Select Content Type</option>
  246.  
  247. <?php
  248.  
  249. $exclude = array('nav_menu_item','revision','attachment');
  250.  
  251. $types = array_diff(get_post_types(), $exclude);
  252.  
  253. foreach($types as $type){
  254.  
  255. $name = ucwords(strtolower(str_replace('_', ' ', $type)));
  256.  
  257. ?>
  258.  
  259. <option value='<?php print($type);?>'><?php print($name);?></option>
  260.  
  261. <?php
  262.  
  263. }
  264.  
  265. ?>
  266.  
  267. </select></p>
  268.  
  269. <p><label for="<?php echo $this->get_field_id( 'type_list_selected' ); ?>">Content Article: </label>
  270.  
  271. <select class="ContentSelectList" id='<?php echo $this->get_field_id( 'type_list_selected' ); ?>' name="<?php echo $this->get_field_name( 'type_list_selected' ); ?>">
  272.  
  273. <option value=''>--------------------------</option>
  274.  
  275. </select></p>
  276.  
  277. <h4><em>Other Options:</em></h4>
  278.  
  279. <p>
  280.  
  281. <label for="<?php echo $this->get_field_id( 'display_title' ); ?>">Article Title:</label>
  282.  
  283. <select id="<?php echo $this->get_field_id( 'display_title' ); ?>" name="<?php echo $this->get_field_name( 'display_title' ); ?>">
  284.  
  285. <option <?php if ( 'visible' == $instance['display_title'] ) echo 'selected="selected"'; ?> value="visible">Title Visible</option>
  286.  
  287. <option <?php if ( 'hidden' == $instance['display_title'] ) echo 'selected="selected"'; ?> value="hidden">Title Hidden</option>
  288.  
  289. </select>
  290.  
  291. </p>
  292.  
  293. <?php
  294.  
  295. }
  296.  
  297. }
  298.  
  299. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement