1 <?php
2 3 4 5 6 7 8 9 10
11 namespace uix\ui\control;
12
13 14 15 16 17
18 class post_relation extends \uix\ui\control{
19
20 21 22 23 24 25 26
27 public $type = 'post_relation';
28
29 30 31 32 33 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 118 119 120 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 134 135 136 137 138
139 public function classes() {
140
141 return array(
142 'uix-post-relation'
143 );
144
145 }
146
147 148 149 150 151 152 153 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 202 203 204 205
206 public function set_assets() {
207
208
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 }