Field.php
758 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Fields class. |
| 4 | * |
| 5 | * @package RT_TPG |
| 6 | */ |
| 7 | |
| 8 | namespace RT\ThePostGrid\Models; |
| 9 | |
| 10 | use RT\ThePostGrid\Helpers\Fns; |
| 11 | use RT\ThePostGrid\Helpers\Options; |
| 12 | |
| 13 | // Do not allow directly accessing this file. |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit( 'This script cannot be accessed directly.' ); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Fields class. |
| 20 | */ |
| 21 | class Field { |
| 22 | private $type; |
| 23 | private $name; |
| 24 | private $value; |
| 25 | private $default; |
| 26 | private $label; |
| 27 | private $id; |
| 28 | private $class; |
| 29 | private $holderClass = ''; |
| 30 | private $description; |
| 31 | private $descriptionAdv; |
| 32 | private $options; |
| 33 | private $option; |
| 34 | private $optionLabel; |
| 35 | private $attr; |
| 36 | private $multiple; |
| 37 | private $alignment; |
| 38 | private $placeholder; |
| 39 | private $blank; |
| 40 | |
| 41 | function __construct() { |
| 42 | } |
| 43 | |
| 44 | private function setArgument( $key, $attr ) { |
| 45 | global $pagenow; |
| 46 | |
| 47 | $this->type = isset( $attr['type'] ) ? ( $attr['type'] ? $attr['type'] : 'text' ) : 'text'; |
| 48 | $this->multiple = isset( $attr['multiple'] ) ? ( $attr['multiple'] ? $attr['multiple'] : false ) : false; |
| 49 | $this->name = ! empty( $key ) ? $key : null; |
| 50 | $id = isset( $attr['id'] ) ? $attr['id'] : null; |
| 51 | $this->id = ! empty( $id ) ? $id : $this->name; |
| 52 | $this->default = isset( $attr['default'] ) ? $attr['default'] : null; |
| 53 | $this->value = isset( $attr['value'] ) ? ( $attr['value'] ? $attr['value'] : null ) : null; |
| 54 | |
| 55 | if ( ! $this->value ) { |
| 56 | $post_id = get_the_ID(); |
| 57 | |
| 58 | if ( ! Fns::meta_exist( $this->name, $post_id ) && $pagenow == 'post-new.php' ) { |
| 59 | $this->value = $this->default; |
| 60 | } elseif ( $this->multiple ) { |
| 61 | if ( metadata_exists( 'post', $post_id, $this->name ) ) { |
| 62 | $this->value = get_post_meta( $post_id, $this->name ); |
| 63 | } else { |
| 64 | $this->value = $this->default; |
| 65 | } |
| 66 | } elseif ( metadata_exists( 'post', $post_id, $this->name ) ) { |
| 67 | $this->value = get_post_meta( $post_id, $this->name, true ); |
| 68 | } else { |
| 69 | $this->value = $this->default; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | $this->label = isset( $attr['label'] ) ? ( $attr['label'] ? $attr['label'] : null ) : null; |
| 74 | $this->class = isset( $attr['class'] ) ? ( $attr['class'] ? $attr['class'] : null ) : null; |
| 75 | $this->holderClass = isset( $attr['holderClass'] ) ? ( $attr['holderClass'] ? $attr['holderClass'] : '' ) : ''; |
| 76 | $this->placeholder = isset( $attr['placeholder'] ) ? ( $attr['placeholder'] ? $attr['placeholder'] : null ) : null; |
| 77 | $this->description = isset( $attr['description'] ) ? ( $attr['description'] ? $attr['description'] : null ) : null; |
| 78 | $this->descriptionAdv = isset( $attr['description_adv'] ) ? ( $attr['description_adv'] ? $attr['description_adv'] : null ) : null; |
| 79 | $this->options = isset( $attr['options'] ) ? ( $attr['options'] ? $attr['options'] : [] ) : []; |
| 80 | $this->option = isset( $attr['option'] ) ? ( $attr['option'] ? $attr['option'] : null ) : null; |
| 81 | $this->optionLabel = isset( $attr['optionLabel'] ) ? ( $attr['optionLabel'] ? $attr['optionLabel'] : null ) : null; |
| 82 | $this->attr = isset( $attr['attr'] ) ? ( $attr['attr'] ? $attr['attr'] : null ) : null; |
| 83 | $this->alignment = isset( $attr['alignment'] ) ? ( $attr['alignment'] ? $attr['alignment'] : null ) : null; |
| 84 | $this->blank = ! empty( $attr['blank'] ) ? $attr['blank'] : null; |
| 85 | } |
| 86 | |
| 87 | public function Field( $key, $attr = [] ) { |
| 88 | $this->setArgument( $key, $attr ); |
| 89 | $holderId = $this->name . '_holder'; |
| 90 | |
| 91 | if ( ! rtTPG()->hasPro() ) { |
| 92 | $class = $this->holderClass; |
| 93 | } else { |
| 94 | $class = str_replace( 'pro-field', '', $this->holderClass ); |
| 95 | } |
| 96 | |
| 97 | $html = null; |
| 98 | $html .= '<div class="field-holder ' . esc_attr( $class ) . '" id="' . esc_attr( $holderId ) . '">'; |
| 99 | |
| 100 | $holderClass = explode( ' ', $this->holderClass ); |
| 101 | |
| 102 | if ( $this->label ) { |
| 103 | $html .= "<div class='field-label'>"; |
| 104 | $html .= '<label>' . Fns::htmlKses( $this->label, 'basic' ) . '</label>'; |
| 105 | |
| 106 | if ( in_array( 'pro-field', $holderClass, true ) && ! rtTPG()->hasPro() ) { |
| 107 | $html .= '<span class="rttpg-tooltip">[Pro]<span class="rttpg-tooltip-text">' . esc_html__( 'Premium Option', 'the-post-grid' ) . '</span></span>'; |
| 108 | } |
| 109 | |
| 110 | $html .= '</div>'; |
| 111 | } |
| 112 | |
| 113 | $html .= "<div class='field'>"; |
| 114 | |
| 115 | switch ( $this->type ) { |
| 116 | case 'text': |
| 117 | $html .= $this->text(); |
| 118 | break; |
| 119 | |
| 120 | case 'url': |
| 121 | $html .= $this->url(); |
| 122 | break; |
| 123 | |
| 124 | case 'number': |
| 125 | $html .= $this->number(); |
| 126 | break; |
| 127 | |
| 128 | case 'select': |
| 129 | $html .= $this->select(); |
| 130 | break; |
| 131 | |
| 132 | case 'textarea': |
| 133 | $html .= $this->textArea(); |
| 134 | break; |
| 135 | |
| 136 | case 'checkbox': |
| 137 | $html .= $this->checkbox(); |
| 138 | break; |
| 139 | |
| 140 | case 'switch': |
| 141 | $html .= $this->switchField(); |
| 142 | break; |
| 143 | |
| 144 | case 'checkboxFilter': |
| 145 | $html .= $this->checkboxFilter(); |
| 146 | break; |
| 147 | |
| 148 | case 'radio': |
| 149 | $html .= $this->radioField(); |
| 150 | break; |
| 151 | |
| 152 | case 'radio-image': |
| 153 | $html .= $this->radioImage(); |
| 154 | break; |
| 155 | |
| 156 | case 'date_range': |
| 157 | $html .= $this->dateRange(); |
| 158 | break; |
| 159 | |
| 160 | case 'script': |
| 161 | $html .= $this->script(); |
| 162 | break; |
| 163 | |
| 164 | case 'image': |
| 165 | $html .= $this->image(); |
| 166 | break; |
| 167 | |
| 168 | case 'image_size': |
| 169 | $html .= $this->imageSize(); |
| 170 | break; |
| 171 | } |
| 172 | |
| 173 | if ( $this->description ) { |
| 174 | $html .= '<p class="description">' . Fns::htmlKses( $this->description, 'basic' ) . '</p>'; |
| 175 | } |
| 176 | |
| 177 | if ( $this->descriptionAdv ) { |
| 178 | $html .= '<p class="description">' . Fns::htmlKses( $this->descriptionAdv, 'advanced' ) . '</p>'; |
| 179 | } |
| 180 | |
| 181 | $html .= '</div>'; // field. |
| 182 | $html .= '</div>'; // field holder. |
| 183 | |
| 184 | return $html; |
| 185 | } |
| 186 | |
| 187 | private function text() { |
| 188 | $holderClass = explode( ' ', $this->holderClass ); |
| 189 | |
| 190 | $h = null; |
| 191 | $h .= '<input |
| 192 | type="text" |
| 193 | class="' . esc_attr( $this->class ) . '" |
| 194 | id="' . esc_attr( $this->id ) . '" |
| 195 | value="' . esc_attr( $this->value ) . '" |
| 196 | name="' . esc_attr( $this->name ) . '" |
| 197 | placeholder="' . esc_attr( $this->placeholder ) . '" |
| 198 | ' . Fns::htmlKses( $this->attr, 'basic' ) . ' |
| 199 | />'; |
| 200 | |
| 201 | return $h; |
| 202 | } |
| 203 | |
| 204 | private function script() { |
| 205 | $type = 'script'; |
| 206 | |
| 207 | if ( $this->id == 'custom-css' ) { |
| 208 | $type = 'css'; |
| 209 | } |
| 210 | |
| 211 | $h = null; |
| 212 | $h .= '<div class="rt-script-wrapper" data-type="' . esc_attr( $type ) . '">'; |
| 213 | $h .= '<div class="rt-script-container">'; |
| 214 | $h .= "<div name='" . esc_attr( $this->name ) . "' id='ret-" . absint( wp_rand() ) . "' class='rt-script'>"; |
| 215 | $h .= '</div>'; |
| 216 | $h .= '</div>'; |
| 217 | |
| 218 | $h .= '<textarea |
| 219 | style="display: none;" |
| 220 | class="rt-script-textarea" |
| 221 | id="' . esc_attr( $this->id ) . '" |
| 222 | name="' . esc_attr( $this->name ) . '" |
| 223 | >' . wp_strip_all_tags( $this->value ) . '</textarea>'; |
| 224 | $h .= '</div>'; |
| 225 | |
| 226 | return $h; |
| 227 | } |
| 228 | |
| 229 | private function url() { |
| 230 | $h = null; |
| 231 | $h .= '<input |
| 232 | type="url" |
| 233 | class="' . esc_attr( $this->class ) . '" |
| 234 | id="' . esc_attr( $this->id ) . '" |
| 235 | value="' . esc_url( $this->value ) . '" |
| 236 | name="' . esc_attr( $this->name ) . '" |
| 237 | placeholder="' . esc_attr( $this->placeholder ) . '" |
| 238 | ' . Fns::htmlKses( $this->attr, 'basic' ) . ' |
| 239 | />'; |
| 240 | |
| 241 | return $h; |
| 242 | } |
| 243 | |
| 244 | private function number() { |
| 245 | $holderClass = explode( ' ', $this->holderClass ); |
| 246 | |
| 247 | $h = null; |
| 248 | $h .= '<input |
| 249 | type="number" |
| 250 | class="' . esc_attr( $this->class ) . '" |
| 251 | id="' . esc_attr( $this->id ) . '" |
| 252 | value="' . ( ! empty( $this->value ) ? absint( $this->value ) : null ) . '" |
| 253 | name="' . esc_attr( $this->name ) . '" |
| 254 | placeholder="' . esc_attr( $this->placeholder ) . '" |
| 255 | ' . Fns::htmlKses( $this->attr, 'basic' ) . ' |
| 256 | />'; |
| 257 | |
| 258 | return $h; |
| 259 | } |
| 260 | |
| 261 | private function select() { |
| 262 | $holderClass = explode( ' ', $this->holderClass ); |
| 263 | $atts = ( in_array( 'pro-field', $holderClass, true ) ) && ! rtTPG()->hasPro() ? 'disabled="true"' : ''; |
| 264 | $h = null; |
| 265 | |
| 266 | if ( $this->multiple ) { |
| 267 | $this->attr = " style='min-width:160px;'"; |
| 268 | $this->name = $this->name . '[]'; |
| 269 | $this->attr = $this->attr . " multiple='multiple'"; |
| 270 | $this->value = ( is_array( $this->value ) && ! empty( $this->value ) ? $this->value : [] ); |
| 271 | } else { |
| 272 | $this->value = [ $this->value ]; |
| 273 | } |
| 274 | |
| 275 | $h .= '<select ' . $atts . ' name="' . esc_attr( $this->name ) . '" id="' . esc_attr( $this->id ) . '" class="' . esc_attr( $this->class ) . '" ' . Fns::htmlKses( $this->attr, 'basic' ) . '>'; |
| 276 | |
| 277 | if ( $this->blank ) { |
| 278 | $h .= '<option value="">' . esc_html( $this->blank ) . '</option>'; |
| 279 | } |
| 280 | |
| 281 | if ( is_array( $this->options ) && ! empty( $this->options ) ) { |
| 282 | foreach ( $this->options as $key => $value ) { |
| 283 | $slt = ( in_array( $key, $this->value ) ? 'selected' : null ); |
| 284 | $h .= '<option ' . esc_attr( $slt ) . ' value="' . esc_attr( $key ) . '">' . esc_html( $value ) . '</option>'; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | $h .= '</select>'; |
| 289 | |
| 290 | return $h; |
| 291 | } |
| 292 | |
| 293 | private function textArea() { |
| 294 | $holderClass = explode( ' ', $this->holderClass ); |
| 295 | |
| 296 | $h = null; |
| 297 | $h .= '<textarea |
| 298 | class="' . esc_attr( $this->class ) . ' rt-textarea" |
| 299 | id="' . esc_attr( $this->id ) . '" |
| 300 | name="' . esc_attr( $this->name ) . '" |
| 301 | placeholder="' . esc_attr( $this->placeholder ) . '" |
| 302 | ' . Fns::htmlKses( $this->attr, 'basic' ) . ' |
| 303 | >' . wp_kses_post( $this->value ) . '</textarea>'; |
| 304 | |
| 305 | return $h; |
| 306 | } |
| 307 | |
| 308 | private function image() { |
| 309 | $holderClass = explode( ' ', $this->holderClass ); |
| 310 | |
| 311 | $h = null; |
| 312 | $img = null; |
| 313 | |
| 314 | $h .= "<div class='rt-image-holder'>"; |
| 315 | $h .= '<input type="hidden" name="' . esc_attr( $this->name ) . '" value="' . absint( $this->value ) . '" id="' . esc_attr( $this->id ) . '" class="hidden-image-id" />'; |
| 316 | $c = 'hidden'; |
| 317 | |
| 318 | if ( $id = absint( $this->value ) ) { |
| 319 | $aImg = wp_get_attachment_image_src( $id, 'thumbnail' ); |
| 320 | $img = '<img src="' . esc_url( $aImg[0] ) . '" >'; |
| 321 | $c = null; |
| 322 | } |
| 323 | |
| 324 | $h .= '<div class="rt-image-preview">' . Fns::htmlKses( $img, 'image' ) . '<span class="dashicons dashicons-plus-alt rtAddImage"></span><span class="dashicons dashicons-trash rtRemoveImage ' . esc_attr( $c ) . '"></span></div>'; |
| 325 | |
| 326 | $h .= '</div>'; |
| 327 | |
| 328 | return $h; |
| 329 | } |
| 330 | |
| 331 | private function imageSize() { |
| 332 | $width = ( ! empty( $this->value[0] ) ? $this->value[0] : null ); |
| 333 | $height = ( ! empty( $this->value[1] ) ? $this->value[1] : null ); |
| 334 | $cropV = ( ! empty( $this->value[2] ) ? $this->value[2] : 'soft' ); |
| 335 | |
| 336 | $h = null; |
| 337 | $h .= "<div class='rt-image-size-holder'>"; |
| 338 | $h .= "<div class='rt-image-size-width rt-image-size'>"; |
| 339 | $h .= '<label>Width</label>'; |
| 340 | $h .= '<input type="number" name="' . esc_attr( $this->name ) . '[]" value="' . absint( $width ) . '" />'; |
| 341 | $h .= '</div>'; |
| 342 | $h .= "<div class='rt-image-size-height rt-image-size'>"; |
| 343 | $h .= '<label>Height</label>'; |
| 344 | $h .= '<input type="number" name="' . esc_attr( $this->name ) . '[]" value="' . absint( $height ) . '" />'; |
| 345 | $h .= '</div>'; |
| 346 | $h .= "<div class='rt-image-size-crop rt-image-size'>"; |
| 347 | $h .= '<label>Crop</label>'; |
| 348 | $h .= '<select name="' . esc_attr( $this->name ) . '[]" class="rt-select2">'; |
| 349 | |
| 350 | $cropList = Options::imageCropType(); |
| 351 | |
| 352 | foreach ( $cropList as $crop => $cropLabel ) { |
| 353 | $cSl = ( $crop == $cropV ? 'selected' : null ); |
| 354 | $h .= '<option value="' . esc_attr( $crop ) . '" ' . esc_attr( $cSl ) . '>' . esc_html( $cropLabel ) . '</option>'; |
| 355 | } |
| 356 | |
| 357 | $h .= '</select>'; |
| 358 | $h .= '</div>'; |
| 359 | $h .= '</div>'; |
| 360 | |
| 361 | return $h; |
| 362 | } |
| 363 | |
| 364 | private function checkbox() { |
| 365 | $holderClass = explode( ' ', $this->holderClass ); |
| 366 | $this->alignment .= ( in_array( 'pro-field', $holderClass, true ) ) && ! rtTPG()->hasPro() ? ' disabled' : ''; |
| 367 | $h = null; |
| 368 | |
| 369 | if ( $this->multiple ) { |
| 370 | $this->name = $this->name . '[]'; |
| 371 | $this->value = ( is_array( $this->value ) && ! empty( $this->value ) ? $this->value : [] ); |
| 372 | } |
| 373 | |
| 374 | if ( $this->multiple ) { |
| 375 | $h .= '<div class="checkbox-group ' . esc_attr( $this->alignment ) . '" id="' . esc_attr( $this->id ) . '">'; |
| 376 | |
| 377 | if ( is_array( $this->options ) && ! empty( $this->options ) ) { |
| 378 | foreach ( $this->options as $key => $value ) { |
| 379 | $checked = ( in_array( $key, $this->value ) ? 'checked' : null ); |
| 380 | |
| 381 | $h .= '<label for="' . esc_attr( $this->id ) . '-' . esc_attr( $key ) . '"><input type="checkbox" id="' . esc_attr( $this->id ) . '-' . esc_attr( $key ) . '" ' . esc_attr( $checked ) . ' name="' . esc_attr( $this->name ) . '" value="' . esc_attr( $key ) . '">' . esc_html( $value ) . '</label>'; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | $h .= '</div>'; |
| 386 | } else { |
| 387 | $checked = ( $this->value ? 'checked' : null ); |
| 388 | $h .= '<label><input type="checkbox" ' . esc_attr( $checked ) . ' id="' . esc_attr( $this->id ) . '" name="' . esc_attr( $this->name ) . '" value="1" />' . esc_html( $this->option ) . '</label>'; |
| 389 | } |
| 390 | |
| 391 | return $h; |
| 392 | } |
| 393 | |
| 394 | private function switchField() { |
| 395 | $h = null; |
| 396 | $checked = $this->value ? 'checked' : null; |
| 397 | $h .= '<label class="rttm-switch"><input type="checkbox" ' . esc_attr( $checked ) . ' id="' . esc_attr( $this->id ) . '" name="' . esc_attr( $this->name ) . '" value="1" /><span class="rttm-switch-slider round"></span></label>'; |
| 398 | |
| 399 | return $h; |
| 400 | } |
| 401 | |
| 402 | private function checkboxFilter() { |
| 403 | global $post; |
| 404 | |
| 405 | $pt = get_post_meta( $post->ID, 'tpg_post_type', true ); |
| 406 | $advFilters = Options::rtTPAdvanceFilters(); |
| 407 | $holderClass = explode( ' ', $this->holderClass ); |
| 408 | $h = null; |
| 409 | |
| 410 | if ( $this->multiple ) { |
| 411 | $this->name = $this->name . '[]'; |
| 412 | $this->value = ( is_array( $this->value ) && ! empty( $this->value ) ? $this->value : [] ); |
| 413 | } |
| 414 | |
| 415 | if ( $this->multiple ) { |
| 416 | $h .= '<div class="checkbox-group ' . esc_attr( $this->alignment ) . '" id="' . esc_attr( $this->id ) . '">'; |
| 417 | |
| 418 | if ( is_array( $this->options ) && ! empty( $this->options ) ) { |
| 419 | foreach ( $this->options as $key => $value ) { |
| 420 | $checked = ( in_array( $key, $this->value ) ? 'checked' : null ); |
| 421 | |
| 422 | $h .= '<div class="checkbox-filter-field">'; |
| 423 | $h .= '<label for="' . esc_attr( $this->id ) . '-' . esc_attr( $key ) . '"><input type="checkbox" id="' . esc_attr( $this->id ) . '-' . esc_attr( $key ) . '" ' . esc_attr( $checked ) . ' name="' . esc_attr( $this->name ) . '" value="' . esc_attr( $key ) . '">' . esc_html( $value ) . '</label>'; |
| 424 | |
| 425 | if ( $key == 'tpg_taxonomy' ) { |
| 426 | $h .= "<div class='rt-tpg-filter taxonomy tpg_taxonomy tpg-hidden'>"; |
| 427 | |
| 428 | if ( isset( $pt ) && $pt ) { |
| 429 | $taxonomies = Fns::rt_get_all_taxonomy_by_post_type( $pt ); |
| 430 | $taxA = get_post_meta( $post->ID, 'tpg_taxonomy' ); |
| 431 | $post_filter = get_post_meta( $post->ID, 'post_filter' ); |
| 432 | |
| 433 | $h .= "<div class='taxonomy-field'>"; |
| 434 | |
| 435 | if ( is_array( $post_filter ) && ! empty( $post_filter ) && in_array( 'tpg_taxonomy', $post_filter ) && ! empty( $taxonomies ) ) { |
| 436 | $h .= Fns::rtFieldGenerator( |
| 437 | [ |
| 438 | 'tpg_taxonomy' => [ |
| 439 | 'type' => 'checkbox', |
| 440 | 'label' => '', |
| 441 | 'id' => 'post-taxonomy', |
| 442 | 'multiple' => true, |
| 443 | 'options' => $taxonomies, |
| 444 | ], |
| 445 | ] |
| 446 | ); |
| 447 | } else { |
| 448 | $h .= '<div class="field-holder">' . esc_html__( 'No Taxonomy found', 'the-post-grid' ) . '</div>'; |
| 449 | } |
| 450 | |
| 451 | $h .= '</div>'; |
| 452 | $h .= "<div class='rt-tpg-filter-item term-filter-item tpg-hidden'>"; |
| 453 | $h .= '<div class="field-holder">'; |
| 454 | $h .= '<div class="field-label">Terms</div>'; |
| 455 | $h .= '<div class="field term-filter-holder">'; |
| 456 | |
| 457 | if ( is_array( $taxA ) && ! empty( $taxA ) ) { |
| 458 | foreach ( $taxA as $tax ) { |
| 459 | |
| 460 | $h .= '<div class="term-filter-item-container ' . esc_attr( $tax ) . '">'; |
| 461 | $h .= Fns::rtFieldGenerator( |
| 462 | [ |
| 463 | 'term_' . $tax => [ |
| 464 | 'type' => 'select', |
| 465 | 'label' => ucfirst( str_replace( '_', ' ', $tax ) ), |
| 466 | 'class' => 'rt-select2 full', |
| 467 | 'holderClass' => "term-filter-item {$tax}", |
| 468 | 'value' => get_post_meta( $post->ID, 'term_' . $tax ), |
| 469 | 'multiple' => true, |
| 470 | 'options' => Fns::rt_get_all_term_by_taxonomy( $tax ), |
| 471 | ], |
| 472 | ] |
| 473 | ); |
| 474 | $h .= Fns::rtFieldGenerator( |
| 475 | array( |
| 476 | 'term_operator_' . $tax => array( |
| 477 | 'type' => 'select', |
| 478 | 'label' => esc_html__( 'Operator', 'the-post-grid' ), |
| 479 | 'class' => 'rt-select2 full', |
| 480 | 'holderClass' => "term-filter-item-operator {$tax}", |
| 481 | 'value' => get_post_meta( $post->ID, 'term_operator_' . $tax, true ), |
| 482 | 'options' => Options::rtTermOperators(), |
| 483 | ), |
| 484 | ) |
| 485 | ); |
| 486 | $h .= '</div>'; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | $h .= '</div>'; |
| 491 | $h .= '</div>'; |
| 492 | |
| 493 | $h .= Fns::rtFieldGenerator( |
| 494 | array( |
| 495 | 'taxonomy_relation' => array( |
| 496 | 'type' => 'select', |
| 497 | 'label' => esc_html__( 'Relation', 'the-post-grid' ), |
| 498 | 'class' => 'rt-select2', |
| 499 | 'holderClass' => 'term-filter-item-relation ' . ( count( $taxA ) > 1 ? null : 'hidden' ), |
| 500 | 'value' => get_post_meta( $post->ID, 'taxonomy_relation', true ), |
| 501 | 'options' => Options::rtTermRelations(), |
| 502 | ), |
| 503 | ) |
| 504 | ); |
| 505 | |
| 506 | $h .= '</div>'; |
| 507 | } else { |
| 508 | $h .= "<div class='taxonomy-field'>"; |
| 509 | $h .= '</div>'; |
| 510 | $h .= "<div class='rt-tpg-filter-item'>"; |
| 511 | $h .= '<div class="field-holder">'; |
| 512 | $h .= '<div class="field-label">Terms</div>'; |
| 513 | $h .= '<div class="field term-filter-holder">'; |
| 514 | $h .= '</div>'; |
| 515 | $h .= '</div>'; |
| 516 | $h .= '</div>'; |
| 517 | $h .= Fns::rtFieldGenerator( |
| 518 | array( |
| 519 | 'taxonomy_relation' => array( |
| 520 | 'type' => 'select', |
| 521 | 'label' => esc_html__( 'Relation', 'the-post-grid' ), |
| 522 | 'class' => 'rt-select2', |
| 523 | 'holderClass' => 'term-filter-item-relation tpg-hidden', |
| 524 | 'default' => 'OR', |
| 525 | 'options' => Options::rtTermRelations(), |
| 526 | ), |
| 527 | ) |
| 528 | ); |
| 529 | } |
| 530 | |
| 531 | $h .= '</div>'; |
| 532 | } elseif ( $key == 'order' ) { |
| 533 | $h .= '<div class="rt-tpg-filter ' . esc_attr( $key ) . ' tpg-hidden">'; |
| 534 | $h .= "<div class='rt-tpg-filter-item'>"; |
| 535 | $h .= "<div class='field-holder'>"; |
| 536 | $h .= "<div class='field'>"; |
| 537 | $h .= Fns::rtFieldGenerator( |
| 538 | array( |
| 539 | 'order_by' => array( |
| 540 | 'type' => 'select', |
| 541 | 'label' => esc_html__( 'Order by', 'the-post-grid' ), |
| 542 | 'class' => 'rt-select2 filter-item', |
| 543 | 'value' => get_post_meta( $post->ID, 'order_by', true ), |
| 544 | 'options' => Options::rtPostOrderBy( false, true ), |
| 545 | 'description' => esc_html__( 'If "Meta value", "Meta value Number" or "Meta value datetime" is chosen then meta key is required.', 'the-post-grid' ), |
| 546 | ), |
| 547 | ) |
| 548 | ); |
| 549 | $h .= Fns::rtFieldGenerator( |
| 550 | array( |
| 551 | 'tpg_meta_key' => array( |
| 552 | 'type' => 'text', |
| 553 | 'label' => esc_html__( 'Meta key', 'the-post-grid' ), |
| 554 | 'class' => 'rt-select2 filter-item', |
| 555 | 'holderClass' => 'tpg-hidden', |
| 556 | 'value' => get_post_meta( $post->ID, 'tpg_meta_key', true ), |
| 557 | ), |
| 558 | ) |
| 559 | ); |
| 560 | $h .= Fns::rtFieldGenerator( |
| 561 | array( |
| 562 | 'order' => array( |
| 563 | 'type' => 'radio', |
| 564 | 'label' => esc_html__( 'Order', 'the-post-grid' ), |
| 565 | 'class' => 'rt-select2 filter-item', |
| 566 | 'alignment' => 'vertical', |
| 567 | 'default' => 'DESC', |
| 568 | 'value' => get_post_meta( $post->ID, 'order', true ), |
| 569 | 'options' => Options::rtPostOrders(), |
| 570 | ), |
| 571 | ) |
| 572 | ); |
| 573 | $h .= '</div>'; |
| 574 | $h .= '</div>'; |
| 575 | $h .= '</div>'; |
| 576 | $h .= '</div>'; |
| 577 | } elseif ( $key == 'author' ) { |
| 578 | $h .= '<div class="rt-tpg-filter ' . esc_attr( $key ) . ' tpg-hidden">'; |
| 579 | $h .= "<div class='rt-tpg-filter-item'>"; |
| 580 | $h .= Fns::rtFieldGenerator( |
| 581 | array( |
| 582 | $key => array( |
| 583 | 'type' => 'select', |
| 584 | 'label' => '', |
| 585 | 'class' => 'rt-select2 filter-item full', |
| 586 | 'value' => get_post_meta( $post->ID, $key ), |
| 587 | 'multiple' => true, |
| 588 | 'options' => Fns::rt_get_users(), |
| 589 | ), |
| 590 | ) |
| 591 | ); |
| 592 | $h .= '</div>'; |
| 593 | $h .= '</div>'; |
| 594 | } elseif ( $key == 'tpg_post_status' ) { |
| 595 | $h .= '<div class="rt-tpg-filter ' . esc_attr( $key ) . ' tpg-hidden">'; |
| 596 | $h .= "<div class='rt-tpg-filter-item'>"; |
| 597 | $h .= Fns::rtFieldGenerator( |
| 598 | array( |
| 599 | $key => array( |
| 600 | 'type' => 'select', |
| 601 | 'label' => '', |
| 602 | 'class' => 'rt-select2 filter-item full', |
| 603 | 'default' => array( 'publish' ), |
| 604 | 'value' => get_post_meta( $post->ID, $key ), |
| 605 | 'multiple' => true, |
| 606 | 'options' => Options::rtTPGPostStatus(), |
| 607 | ), |
| 608 | ) |
| 609 | ); |
| 610 | $h .= '</div>'; |
| 611 | $h .= '</div>'; |
| 612 | } elseif ( $key == 's' ) { |
| 613 | $h .= '<div class="rt-tpg-filter ' . esc_attr( $key ) . ' tpg-hidden">'; |
| 614 | $h .= "<div class='rt-tpg-filter-item'>"; |
| 615 | $h .= Fns::rtFieldGenerator( |
| 616 | array( |
| 617 | $key => array( |
| 618 | 'type' => 'text', |
| 619 | 'label' => esc_html__( 'Keyword', 'the-post-grid' ), |
| 620 | 'class' => 'filter-item full', |
| 621 | 'value' => get_post_meta( $post->ID, $key, true ), |
| 622 | ), |
| 623 | ) |
| 624 | ); |
| 625 | $h .= '</div>'; |
| 626 | $h .= '</div>'; |
| 627 | } elseif ( $key == 'date_range' ) { |
| 628 | $range_start = get_post_meta( $post->ID, 'date_range_start', true ); |
| 629 | $range_end = get_post_meta( $post->ID, 'date_range_end', true ); |
| 630 | $range_value = array( |
| 631 | 'start' => $range_start, |
| 632 | 'end' => $range_end, |
| 633 | ); |
| 634 | $h .= '<div class="rt-tpg-filter ' . esc_attr( $key ) . ' tpg-hidden">'; |
| 635 | $h .= "<div class='rt-tpg-filter-item'>"; |
| 636 | $h .= Fns::rtFieldGenerator( |
| 637 | array( |
| 638 | $key => array( |
| 639 | 'type' => 'date_range', |
| 640 | 'label' => '', |
| 641 | 'class' => 'filter-item full rt-date-range', |
| 642 | 'value' => $range_value, |
| 643 | 'description' => "Date format should be 'yyyy-mm-dd'", |
| 644 | ), |
| 645 | ) |
| 646 | ); |
| 647 | $h .= '</div>'; |
| 648 | $h .= '</div>'; |
| 649 | } |
| 650 | // } |
| 651 | |
| 652 | $h .= '</div>'; |
| 653 | } |
| 654 | } |
| 655 | $h .= '</div>'; |
| 656 | } else { |
| 657 | $checked = ( $this->value ? 'checked' : null ); |
| 658 | $h .= '<label><input type="checkbox" ' . esc_attr( $checked ) . ' id="' . esc_attr( $this->id ) . '" name="' . esc_attr( $this->name ) . '" value="1" />' . esc_html( $this->option ) . '</label>'; |
| 659 | } |
| 660 | |
| 661 | return $h; |
| 662 | } |
| 663 | |
| 664 | private function radioField() { |
| 665 | $holderClass = explode( ' ', $this->holderClass ); |
| 666 | $this->alignment .= ( in_array( 'pro-field', $holderClass, true ) ) && ! rtTPG()->hasPro() ? ' disabled' : ''; |
| 667 | $h = null; |
| 668 | |
| 669 | $h .= '<div class="radio-group ' . esc_attr( $this->alignment ) . '" id="' . esc_attr( $this->id ) . '">'; |
| 670 | |
| 671 | if ( is_array( $this->options ) && ! empty( $this->options ) ) { |
| 672 | foreach ( $this->options as $key => $value ) { |
| 673 | $checked = ( $key == $this->value ? 'checked' : null ); |
| 674 | |
| 675 | $h .= '<label for="' . esc_attr( $this->name ) . '-' . esc_attr( $key ) . '"><input type="radio" id="' . esc_attr( $this->id ) . '-' . esc_attr( $key ) . '" ' . esc_attr( $checked ) . ' name="' . esc_attr( $this->name ) . '" value="' . esc_attr( $key ) . '">' . esc_html( $value ) . '</label>'; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | $h .= '</div>'; |
| 680 | |
| 681 | return $h; |
| 682 | } |
| 683 | |
| 684 | /** |
| 685 | * Radio Image |
| 686 | * |
| 687 | * @return String |
| 688 | */ |
| 689 | private function radioImage() { |
| 690 | $h = null; |
| 691 | $id = 'rttpg-' . $this->name; |
| 692 | |
| 693 | $h .= sprintf( "<div class='rttpg-radio-image %s' id='%s'>", esc_attr( $this->alignment ), esc_attr( $id ) ); |
| 694 | $selected_value = $this->value; |
| 695 | |
| 696 | if ( is_array( $this->options ) && ! empty( $this->options ) ) { |
| 697 | foreach ( $this->options as $key => $value ) { |
| 698 | $checked = ( $key == $selected_value ? 'checked' : null ); |
| 699 | $title = isset( $value['title'] ) && $value['title'] ? $value['title'] : ''; |
| 700 | $link = isset( $value['layout_link'] ) && $value['layout_link'] ? $value['layout_link'] : ''; |
| 701 | $linkHtml = empty( $link ) ? esc_html( $title ) : '<a href="' . esc_url( $link ) . '" target="_blank">' . esc_html( $title ) . '</a>'; |
| 702 | $layout = isset( $value['layout'] ) ? $value['layout'] : ''; |
| 703 | $taghtml = isset( $value['tag'] ) ? '<div class="rt-tpg-layout-tag"><span>' . $value['tag'] . '</span></div>' : ''; |
| 704 | $h .= sprintf( |
| 705 | '<div class="rt-tpg-radio-layout %7$s"><label data-type="%7$s" class="radio-image %7$s" for="%2$s"> |
| 706 | <input type="radio" id="%2$s" %3$s name="%4$s" value="%2$s"> |
| 707 | <div class="rttpg-radio-image-wrap"> |
| 708 | <img src="%5$s" title="%6$s" alt="%2$s"> |
| 709 | <div class="rttpg-checked"><span class="dashicons dashicons-yes"></span></div> |
| 710 | %9$s |
| 711 | </div> |
| 712 | </label> |
| 713 | <div class="rttpg-demo-name">%8$s</div> |
| 714 | </div>', |
| 715 | '', |
| 716 | esc_attr( $key ), |
| 717 | esc_attr( $checked ), |
| 718 | esc_attr( $this->name ), |
| 719 | esc_url( $value['img'] ), |
| 720 | esc_attr( $title ), |
| 721 | esc_attr( $layout ), |
| 722 | Fns::htmlKses( $linkHtml, 'basic' ), |
| 723 | Fns::htmlKses( $taghtml, 'basic' ) |
| 724 | ); |
| 725 | } |
| 726 | } |
| 727 | $h .= '</div>'; |
| 728 | return $h; |
| 729 | } |
| 730 | |
| 731 | private function dateRange() { |
| 732 | $h = null; |
| 733 | $this->name = ( $this->name ? $this->name : 'date-range-' . wp_rand( 0, 1000 ) ); |
| 734 | $h .= '<div class="date-range-container" id="' . esc_attr( $this->id ) . '">'; |
| 735 | $h .= "<div class='date-range-content start'><span>" . esc_html__( 'Start', 'the-post-grid' ) . "</span><input |
| 736 | type='text' |
| 737 | class='date-range date-range-start {$this->class}' |
| 738 | id='" . esc_attr( $this->id ) . "-start' |
| 739 | value='" . esc_attr( $this->value['start'] ) . "' |
| 740 | name='" . esc_attr( $this->name ) . "_start' |
| 741 | placeholder='" . esc_attr( $this->name ) . "' |
| 742 | " . Fns::htmlKses( $this->attr, 'basic' ) . ' |
| 743 | /></div>'; |
| 744 | $h .= "<div class='date-range-content end'><span>" . esc_html__( 'End', 'the-post-grid' ) . "</span><input |
| 745 | type='text' |
| 746 | class='date-range date-range-end {$this->class}' |
| 747 | id='" . esc_attr( $this->id ) . "-end' |
| 748 | value='" . esc_attr( $this->value['end'] ) . "' |
| 749 | name='" . esc_attr( $this->name ) . "_end' |
| 750 | placeholder='" . esc_attr( $this->name ) . "' |
| 751 | " . Fns::htmlKses( $this->attr, 'basic' ) . ' |
| 752 | /></div>'; |
| 753 | $h .= '</div>'; |
| 754 | |
| 755 | return $h; |
| 756 | } |
| 757 | } |
| 758 |