Advertisement
bedas

post-content-cell.php

Jul 21st, 2015
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.72 KB | None | 0 0
  1. <?php
  2.  
  3. class WPDD_layout_cell_post_content extends WPDD_layout_cell {
  4.  
  5.     function __construct($name, $width, $css_class_name = '', $content = null, $css_id = '') {
  6.         parent::__construct($name, $width, $css_class_name, 'cell-post-content', $content, $css_id);
  7.  
  8.         $this->set_cell_type('cell-post-content');
  9.     }
  10.  
  11.     function frontend_render_cell_content($target) {
  12.         global $WPV_templates, $post, $id, $authordata;
  13.        
  14.         $cell_content = $this->get_content();
  15.        
  16.         if ($cell_content['page'] == 'current_page') {
  17.             do_action('ddl-layouts-render-start-post-content');
  18.         }
  19.        
  20.         // View template support is only here for backwards support before 0.9.2.
  21.         // It's not used for post content cells for 0.9.2 and later.
  22.         if (isset($WPV_templates) && isset($cell_content['ddl_view_template_id']) && $cell_content['ddl_view_template_id'] != 'None') {
  23.             $content_template_id = $cell_content['ddl_view_template_id'];
  24.             if ($cell_content['page'] == 'current_page') {
  25.                 global $post;
  26.                 $content = render_view_template($content_template_id, $post );
  27.             } elseif ($cell_content['page'] == 'this_page') {
  28.                 $post = get_post( $cell_content['selected_post'] );
  29.                 $content = render_view_template($content_template_id, $post );
  30.             }
  31.         } else {
  32.  
  33.             if (isset($WPV_templates)) {
  34.                 remove_filter('the_content', array($WPV_templates, 'the_content'), 1, 1);
  35.             }
  36.  
  37.             $content = '';
  38.             if( $target->is_layout_argument_set( 'post-content-callback' ) && function_exists( $target->get_layout_arguments( 'post-content-callback' ) ) ) {
  39.                
  40.                 global $wp_query;
  41.  
  42.                 // in case of issue uncomment this line
  43.                 //$wp_query->current_post = -1;
  44.  
  45.                 // prevent any other override to bother
  46.                 remove_all_actions( 'loop_start' );
  47.                 remove_all_actions('loop_end' );
  48.  
  49.                 if ($cell_content['page'] == 'this_page') {
  50.                     // need to switch the post.
  51.                     $original_query = isset( $wp_query ) ? clone $wp_query : null;
  52.                     $original_post = isset( $post ) ? clone $post : null;
  53.                     $original_authordata = isset ($authordata) ? clone $authordata : null;
  54.                     $original_id = $id;
  55.  
  56.  
  57.                     $wp_query = new WP_Query(array('post_type' => 'any',
  58.                                                     'ignore_sticky_posts' => true,
  59.                                                    'post__in' => array( $cell_content['selected_post'] ) ) );
  60.  
  61.  
  62.                     }
  63.  
  64.                 ob_start();
  65.  
  66.                 call_user_func( $target->get_layout_arguments( 'post-content-callback' ) );
  67.                 $content = ob_get_clean();
  68.                
  69.                 if ($cell_content['page'] == 'this_page') {
  70.                     // restore the global wp_query.
  71.                     $wp_query = isset( $original_query ) ? clone $original_query : null;
  72.                     $post = isset( $original_post ) ? clone $original_post : null;
  73.                     $authordata = isset( $original_authordata ) ? clone $original_authordata : null;
  74.                     $id = $original_id;
  75.                 }
  76.                
  77.             } else {
  78.  
  79.                 if ( $cell_content['page'] == 'current_page' && is_object($post) && property_exists($post, 'post_content')) {
  80.                     $content = apply_filters('the_content', $post->post_content);
  81.                 } elseif ($cell_content['page'] == 'this_page') {
  82.                     $other_post = get_post($cell_content['selected_post']);
  83.                     if ( is_object($other_post) && property_exists($other_post, 'post_content') ) {
  84.                         $content = apply_filters('the_content', $other_post->post_content);
  85.                     }
  86.                 }
  87.                
  88.             }
  89.            
  90.             if (isset($WPV_templates)) {
  91.                 add_filter('the_content', array($WPV_templates, 'the_content'), 1, 1);
  92.             }
  93.            
  94.            
  95.         }
  96.         $target->cell_content_callback($content, $this);
  97.        
  98.         if ($cell_content['page'] == 'current_page') {
  99.             do_action('ddl-layouts-render-end-post-content');
  100.         }
  101.        
  102.     }
  103.  
  104. }
  105.  
  106. class WPDD_layout_cell_post_content_factory extends WPDD_layout_cell_factory{
  107.  
  108.     function __construct() {
  109.  
  110.     }
  111.  
  112.     public function build($name, $width, $css_class_name = '', $content = null, $css_id, $tag) {
  113.         return new WPDD_layout_cell_post_content($name, $width, $css_class_name, $content, $css_id, $tag);
  114.     }
  115.  
  116.     public function get_cell_info($template) {
  117.         $template['cell-image-url'] = DDL_ICONS_SVG_REL_PATH.'post-content.svg';
  118.         $template['preview-image-url'] = DDL_ICONS_PNG_REL_PATH . 'post-content_expand-image.png';
  119.         $template['name'] = sprintf( __('Post', 'ddl-layouts'), '' );
  120.         $template['description'] = __('Display the content of a page, post or custom type using styling from the theme.', 'ddl-layouts');
  121.         $template['button-text'] = __('Assign Post content Box', 'ddl-layouts');
  122.         $template['dialog-title-create'] = __('Create a new Post content Cell', 'ddl-layouts');
  123.         $template['dialog-title-edit'] = __('Edit Post content Cell', 'ddl-layouts');
  124.         $template['dialog-template'] = $this->_dialog_template();
  125.         $template['category'] = sprintf( __('%s elements', 'ddl-layouts'), WPDD_Layouts::get_theme_name() );
  126.         $template['has_settings'] = false;
  127.         return $template;
  128.     }
  129.  
  130.     public function get_editor_cell_template() {
  131.         ob_start();
  132.         ?>
  133.             <div class="cell-content">
  134.                 <p class="cell-name">{{ name }}</p>
  135.                     <div class="cell-preview">
  136.                         <#
  137.                             if (content) {
  138.                                 var preview = DDLayout.post_content_cell.get_preview(content,
  139.                                             '<?php _e('Displays the content of the current page', 'ddl-layouts'); ?>',
  140.                                             '<?php _e('Displays the content of %s', 'ddl-layouts'); ?>',
  141.                                             '<?php _e('Loading', 'ddl-layouts'); ?>...',
  142.                                             '<?php echo WPDDL_RES_RELPATH . '/images/cell-icons/post-content-preview.svg'; ?>',
  143.                                             this );
  144.                                 print( preview );
  145.                             }
  146.                         #>
  147.                     </div>
  148.             </div>
  149.         <?php
  150.         return ob_get_clean();
  151.     }
  152.  
  153.     private function _dialog_template() {
  154.  
  155.         ob_start();
  156.         ?>
  157.         <ul class="ddl-form">
  158.             <li>
  159.                 <fieldset>
  160.                     <legend><?php _e('Display content for:', 'ddl-layouts'); ?></legend>
  161.                     <div class="fields-group">
  162.                         <ul>
  163.                             <li>
  164.                                 <label class="post-content-page">
  165.                                     <input type="radio" name="<?php the_ddl_name_attr('page'); ?>" value="current_page" checked="checked"/>
  166.                                     <?php _e('Current page', 'ddl-layouts'); ?>
  167.                                 </label>
  168.                             </li>
  169.                             <li>
  170.                                 <label class="post-content-page">
  171.                                     <input type="radio" name="<?php the_ddl_name_attr( 'page' ); ?>" value="this_page" />
  172.                                     <?php _e( 'A specific page:', 'ddl-layouts' ); ?>
  173.                                 </label>
  174.                             </li>
  175.                             <li id="js-post-content-specific-page">
  176.                                 <select name="<?php the_ddl_name_attr( 'post_content_post_type' ); ?>" class="js-ddl-post-content-post-type" data-nonce="<?php echo wp_create_nonce( 'ddl-post-content-post-type-select' ); ?>">
  177.                                     <option value="ddl-all-post-types"><?php _e('All post types', 'ddl-layouts'); ?></option>
  178.                                     <?php
  179.                                     $post_types = get_post_types( array( 'public' => true ), 'objects' );
  180.                                     foreach ( $post_types as $post_type ) {
  181.                                         $count_posts = wp_count_posts($post_type->name);
  182.                                         if ($count_posts->publish > 0) {
  183.                                             ?>
  184.                                                 <option value="<?php echo $post_type->name; ?>"<?php if($post_type->name == 'page') { echo ' selected="selected"';} ?>>
  185.                                                     <?php echo $post_type->labels->singular_name; ?>
  186.                                                 </option>
  187.                                             <?php
  188.                                         }
  189.                                     }
  190.                                     ?>
  191.                                 </select>
  192.                                 <?php
  193.                                     $keys = array_keys( $post_types );
  194.                                     $post_types_array = array_shift(  $keys  );
  195.                                     $this->show_posts_dropdown( $post_types_array, get_ddl_name_attr( 'selected_post' ) );
  196.                                 ?>
  197.                             </li>
  198.                         </ul>
  199.                     </div>
  200.                 </fieldset>
  201.             </li>
  202.        
  203.  
  204.         </ul>
  205.         <?php ddl_add_help_link_to_dialog(WPDLL_POST_CONTENT_CELL, __('Learn about the Post Content cell', 'ddl-layouts')); ?>
  206.         <?php wp_nonce_field( 'wpv-ct-inline-edit', 'wpv-ct-inline-edit' ); ?>
  207.  
  208.         <?php
  209.         return ob_get_clean();
  210.     }
  211.  
  212.  
  213.     public function enqueue_editor_scripts() {
  214.         wp_register_script( 'wp-post-content-editor', ( WPDDL_GUI_RELPATH . "editor/js/post-content-cell.js" ), array('jquery'), null, true );
  215.         wp_enqueue_script( 'wp-post-content-editor' );
  216.  
  217.         wp_localize_script('wp-post-content-editor', 'DDLayout_post_content_strings', array(
  218.                 'current_post' => __('This cell will display the content of the post which uses the layout.', 'ddl-layouts'),
  219.                 'this_post' => __('This cell will display the content of a specific post.', 'ddl-layouts'),
  220.                 )
  221.         );
  222.     }
  223.  
  224.     private function show_posts_dropdown($post_type, $name, $selected = 0) {
  225.         if ($post_type == 'ddl-all-post-types') {
  226.             $post_type = 'any';
  227.         }
  228.  
  229.         $attr = array('name'=> $name,
  230.                       'post_type' => $post_type,
  231.                       'show_option_none' => __('None', 'ddl-layouts'),
  232.                       'selected' => $selected);
  233.  
  234.  
  235.         add_filter('posts_clauses_request', array($this, 'posts_clauses_request_filter'), 10, 2 );
  236.  
  237.         $defaults = array(
  238.             'depth' => 0, 'child_of' => 0,
  239.             'selected' => $selected, 'echo' => 1,
  240.             'name' => 'page_id', 'id' => '',
  241.             'show_option_none' => '', 'show_option_no_change' => '',
  242.             'option_none_value' => ''
  243.         );
  244.         $r = wp_parse_args( $attr, $defaults );
  245.         extract( $r, EXTR_SKIP );
  246.  
  247.         $pages = get_posts(array('posts_per_page' => -1, 'post_type' => $post_type, 'suppress_filters' => false));
  248.         $output = '';
  249.         // Back-compat with old system where both id and name were based on $name argument
  250.         if ( empty($id) )
  251.             $id = $name;
  252.  
  253.         if ( ! empty($pages) ) {
  254.             $output = "<select name='" . esc_attr( $name ) . "' id='" . esc_attr( $id ) . "' data-post-type='" . esc_attr( $post_type ). "'>\n";
  255.             if ( $show_option_no_change )
  256.                 $output .= "\t<option value=\"-1\">$show_option_no_change</option>";
  257.             if ( $show_option_none )
  258.                 $output .= "\t<option value=\"" . esc_attr($option_none_value) . "\">$show_option_none</option>\n";
  259.             $output .= walk_page_dropdown_tree($pages, $depth, $r);
  260.             $output .= "</select>\n";
  261.         }
  262.  
  263.         echo $output;
  264.  
  265.         remove_filter('posts_clauses_request', array($this, 'posts_clauses_request_filter'), 10, 2 );
  266.  
  267.     }
  268.  
  269.     function posts_clauses_request_filter($pieces, $query ) {
  270.         global $wpdb;
  271.         // only return the fields required for the dropdown.
  272.         $pieces['fields'] = "$wpdb->posts.ID, $wpdb->posts.post_parent, $wpdb->posts.post_title";
  273.  
  274.         return $pieces;
  275.     }
  276.  
  277.  
  278. }
  279.  
  280. add_filter('dd_layouts_register_cell_factory', 'dd_layouts_register_cell_post_content_factory');
  281. function dd_layouts_register_cell_post_content_factory($factories) {
  282.     $factories['cell-post-content'] = new WPDD_layout_cell_post_content_factory;
  283.     return $factories;
  284. }
  285.  
  286.  
  287. add_action('wp_ajax_ddl_post_content_get_post_title', 'ddl_post_content_get_post_title_callback');
  288. function ddl_post_content_get_post_title_callback() {
  289.     if ( !isset($_POST["wpnonce"]) || !wp_verify_nonce($_POST["wpnonce"], 'ddl_layout_view_nonce') ) die("Undefined Nonce.");
  290.  
  291.     global $wpdb;
  292.    
  293.     echo $wpdb->get_var("SELECT post_title FROM {$wpdb->posts} WHERE ID={$_POST['post_id']}");
  294.    
  295.     die();
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement