UIX Documentation
  • Namespace
  • Class
  • Tree

Namespaces

  • None
  • uix
    • data
    • ui
      • control

Classes

  • uix\data\data
  • uix\ui
  • uix\ui\box
  • uix\ui\control
  • uix\ui\control\autocomplete
  • uix\ui\control\button
  • uix\ui\control\checkbox
  • uix\ui\control\color
  • uix\ui\control\editor
  • uix\ui\control\email
  • uix\ui\control\file
  • uix\ui\control\hidden
  • uix\ui\control\number
  • uix\ui\control\post_relation
  • uix\ui\control\radio
  • uix\ui\control\select
  • uix\ui\control\separator
  • uix\ui\control\slider
  • uix\ui\control\template
  • uix\ui\control\text
  • uix\ui\control\textarea
  • uix\ui\control\toggle
  • uix\ui\footer
  • uix\ui\grid
  • uix\ui\header
  • uix\ui\help
  • uix\ui\metabox
  • uix\ui\modal
  • uix\ui\notice
  • uix\ui\page
  • uix\ui\panel
  • uix\ui\post_type
  • uix\ui\repeat
  • uix\ui\section
  • uix\ui\uix

Interfaces

  • uix\data\load
  • uix\data\save

Functions

  • uix
  • uix_autoload_class
  1 <?php
  2 /**
  3  * UIX Controls - Post Relation
  4  *
  5  * @package   controls
  6  * @author    David Cramer
  7  * @license   GPL-2.0+
  8  * @link
  9  * @copyright 2016 David Cramer
 10  */
 11 namespace uix\ui\control;
 12 
 13 /**
 14  * Standard text input field
 15  *
 16  * @since 1.0.0
 17  */
 18 class post_relation extends \uix\ui\control{
 19     
 20     /**
 21      * The type of object
 22      *
 23      * @since       1.0.0
 24      * @access public
 25      * @var         string
 26      */
 27     public $type = 'post_relation';
 28 
 29     /**
 30      * Catch the ajax search and push results
 31      *
 32      * @since 1.0.0
 33      * @access public
 34      */
 35     public function init(){
 36 
 37         $defaults = array(
 38             'add_label' => __( 'Add Related Post', 'uix' ),
 39             'config' => array(
 40                 'limit' => 1,
 41             ),
 42             'query' => array(
 43                 'post_type' => 'any',
 44                 'post_per_page' => 5,
 45             ),
 46         );
 47 
 48         $this->struct = array_merge( $defaults, $this->struct );
 49 
 50         $data = uix()->request_vars( 'post' );
 51 
 52         if( !empty( $data['uixId'] ) && $data['uixId'] === $this->id() )
 53             $this->do_lookup( $data );
 54 
 55     }
 56     public function do_lookup( $data ){
 57 
 58         $defaults = array(
 59             'post_type' => 'post',
 60             'posts_per_page' => 10,
 61             'paged' => 1,
 62         );
 63 
 64         if( !empty( $data['_value'] ) )
 65             $defaults['s'] = $data['_value'];
 66 
 67         $args = array_merge( $defaults, $this->struct['query'] );
 68 
 69         if( !empty( $data['page'] ) )
 70             $args['paged'] = (int) $data['page'];
 71 
 72         if( !empty( $data['selected'] ) )
 73             $args['post__not_in'] = explode(',', $data['selected'] );
 74 
 75         $the_query = new \WP_Query( $args );
 76 
 77         $return = array(
 78             'html' => '',
 79             'found_posts' => $the_query->found_posts,
 80             'max_num_pages' => $the_query->max_num_pages,
 81         );
 82         if ( $the_query->have_posts() ) {
 83             while ( $the_query->have_posts() ) {
 84                 $the_query->the_post();
 85 
 86                 $return['html'] .= '<div class="uix-post-relation-item">';
 87                 $return['html'] .= '<span class="uix-post-relation-add dashicons dashicons-plus" data-id="' . esc_html( $this->id() ) . '"></span>';
 88                 $return['html'] .= '<span class="uix-relation-name">' . get_the_title(). '</span>';
 89                 $return['html'] .= '<input class="uix-post-relation-id" type="hidden" name="' . esc_html( $this->name() ) . '[]" value="' . esc_attr( get_the_ID() ) . '" disabled="disabled">';
 90                 $return['html'] .= '</div>';
 91 
 92             }
 93             wp_reset_postdata();
 94 
 95             $return['html'] .= '<div class="uix-post-relation-pager">';
 96             if( $the_query->max_num_pages > 1 ){
 97                 $return['html'] .= '<button type="button" class="uix-post-relation-page button button-small" data-page="' . esc_attr($args['paged'] - 1) . '">';
 98                 $return['html'] .= '<span class="dashicons dashicons-arrow-left-alt2"></span>';
 99                 $return['html'] .= '</button>';
100 
101                 $return['html'] .= '<span class="uix-post-relation-count">' . $args['paged'] . ' ' . esc_html__('of', 'uix') . ' ' . $the_query->max_num_pages . '</span>';
102 
103                 $return['html'] .= '<button type="button" class="uix-post-relation-page button button-small" data-page="' . esc_attr($args['paged'] + 1) . '">';
104                 $return['html'] .= '<span class="dashicons dashicons-arrow-right-alt2"></span>';
105                 $return['html'] .= '</button>';
106             }
107             $return['html'] .= '</div>';
108 
109         } else {
110             $return['html'] .= '<div class="uix-post-relation-no-results">' . esc_html__('Nothing found', 'uix') . '</div>';
111         }
112 
113         wp_send_json( $return );
114     }
115 
116     /**
117      * Sets styling colors
118      *
119      * @since 1.0.0
120      * @access protected
121      */
122     protected function enqueue_active_assets(){
123 
124         echo '<style type="text/css">';
125         echo '.' . $this->id() . ' .uix-post-relation-item .uix-post-relation-add:hover{color: ' . $this->base_color() . ';}';
126         echo '.' . $this->id() . ' .uix-post-relation-item .uix-post-relation-remover:hover {color: ' . $this->base_color() . ';}';
127 
128         echo '</style>';
129 
130     }
131     
132     /**
133      * Gets the classes for the control input
134      *
135      * @since  1.0.0
136      * @access public
137      * @return array
138      */
139     public function classes() {
140 
141         return array(
142             'uix-post-relation'
143         );
144 
145     }
146 
147     /**
148      * Returns the main input field for rendering
149      *
150      * @since 1.0.0
151      * @see \uix\ui\uix
152      * @access public
153      * @return string
154      */
155     public function input(){
156 
157 
158         $data = (array) $this->get_value();
159         $input  = '<div ' . $this->build_attributes() . '>';
160 
161         foreach( $data as $item ){
162 
163             $input .= $this->render_item( $item );
164 
165         }
166 
167         $input .= '</div>';
168 
169         $input .= '<div class="uix-post-relation-footer"><button class="button button-small uix-add-relation" type="button">' . esc_html( $this->struct['add_label'] ) . '</button></div>';
170         $input .= '<div class="uix-post-relation-panel">';
171         $input .= '<span class="uix-post-relation-spinner spinner"></span>';
172         $input .= '<input type="search" class="uix-ajax" data-load-element="_parent" data-delay="250" data-method="POST" data-uix-id="' . esc_attr( $this->id() ) . '" data-event="input paginate" data-before="uix_related_post_before" data-callback="uix_related_post_handler" data-target="#' . esc_attr( $this->id() ) . '-search-results">';
173 
174         $input .= '<div class="uix-post-relation-results" id="' . esc_attr( $this->id() ) . '-search-results">';
175 
176 
177 
178         $input .= '</div>';
179 
180         $input .= '</div>';
181 
182         return $input;
183     }
184 
185     public function render_item( $item ){
186         $input = null;
187 
188         if( get_post( $item ) ){
189 
190             $input .= '<div class="uix-post-relation-item">';
191             $input .= '<span class="uix-post-relation-remover dashicons dashicons-no-alt"></span>';
192             $input .= '<span class="uix-relation-name">' . get_the_title($item) . '</span>';
193             $input .= '<input class="uix-post-relation-id" type="hidden" name="' . esc_html($this->name()) . '[]" value="' . esc_attr($item) . '">';
194             $input .= '</div>';
195         }
196 
197         return $input;
198     }
199 
200     /**
201      * register scritps and styles
202      *
203      * @since 1.0.0
204      * @access public
205      */
206     public function set_assets() {
207 
208         // Initilize core styles
209         $this->assets['style']['post-relation']        = $this->url . 'assets/controls/post-relation/css/post-relation' . UIX_ASSET_DEBUG . '.css';
210 
211         $this->assets['script']['post-relation']  = array(
212             "src"       => $this->url . 'assets/controls/post-relation/js/post-relation' . UIX_ASSET_DEBUG . '.js',
213             "in_footer" => true
214         );
215         $this->assets['script']['baldrick'] = array(
216             'src' => $this->url . 'assets/js/jquery.baldrick' . UIX_ASSET_DEBUG . '.js',
217             'deps' => array( 'jquery' ),
218         );
219         $this->assets['script']['uix-ajax'] = array(
220             'src' => $this->url . 'assets/js/ajax' . UIX_ASSET_DEBUG . '.js',
221             'deps' => array( 'baldrick' ),
222         );
223         $this->assets['style']['uix-ajax'] =  $this->url . 'assets/css/ajax' . UIX_ASSET_DEBUG . '.css';
224 
225         parent::set_assets();
226     }
227 
228 }
UIX Documentation API documentation generated by ApiGen