| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace JFB_Modules\Option_Query; |
| 5 |
|
| 6 |
use JFB_Modules\Option_Query\Interfaces\Option_Query_It; |
| 7 |
use JFB_Modules\Option_Query\Traits\Option_Query_Trait; |
| 8 |
|
| 9 |
// If this file is called directly, abort. |
| 10 |
if ( ! defined( 'WPINC' ) ) { |
| 11 |
die; |
| 12 |
} |
| 13 |
|
| 14 |
class Posts_Query implements Option_Query_It { |
| 15 |
|
| 16 |
use Option_Query_Trait; |
| 17 |
|
| 18 |
const POST_PROPS = array( |
| 19 |
'post_title', |
| 20 |
'post_content', |
| 21 |
'post_name', |
| 22 |
'post_excerpt', |
| 23 |
); |
| 24 |
|
| 25 |
public function rep_item_id() { |
| 26 |
return 'posts'; |
| 27 |
} |
| 28 |
|
| 29 |
public function fetch(): \Generator { |
| 30 |
$post_type = $this->get_query( 'post_type' ); |
| 31 |
|
| 32 |
if ( ! $post_type ) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
if ( ! $this->has_query( 'post_status' ) ) { |
| 37 |
$this->set_query( 'post_status', 'publish' ); |
| 38 |
} |
| 39 |
|
| 40 |
if ( ! $this->has_query( 'posts_per_page' ) ) { |
| 41 |
$this->set_query( 'posts_per_page', - 1 ); |
| 42 |
} |
| 43 |
|
| 44 |
// replace 'search' to the 's' |
| 45 |
if ( $this->has_query( 'search' ) && ! $this->has_query( 's' ) ) { |
| 46 |
$this->set_query( 's', $this->get_query( 'search' ) ); |
| 47 |
} |
| 48 |
$this->delete_query( 'search' ); |
| 49 |
|
| 50 |
$posts = get_posts( |
| 51 |
apply_filters_deprecated( |
| 52 |
'jet-form-builder/render-choice/query-options/posts', |
| 53 |
array( $this->get_query_params(), $this->get_settings() ), |
| 54 |
'3.3.1', |
| 55 |
'jet-form-builder/option-query/set-in-block' |
| 56 |
) |
| 57 |
); |
| 58 |
|
| 59 |
if ( empty( $posts ) ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
foreach ( $posts as $post ) { |
| 64 |
|
| 65 |
$item = array( |
| 66 |
'object_id' => $post->ID, |
| 67 |
'value' => $post->ID, |
| 68 |
'label' => apply_filters( 'jet-form-builder/render-choice/label/posts', $post->post_title, $post ), |
| 69 |
); |
| 70 |
|
| 71 |
$value_from = $this->get_setting( 'value_from' ); |
| 72 |
|
| 73 |
if ( $value_from ) { |
| 74 |
if ( in_array( $value_from, self::POST_PROPS, true ) ) { |
| 75 |
$item['value'] = $post->$value_from; |
| 76 |
} else { |
| 77 |
$item['value'] = get_post_meta( $post->ID, $value_from, true ); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
$calc_from = $this->get_setting( 'calc_from' ); |
| 82 |
|
| 83 |
if ( ! empty( $calc_from ) ) { |
| 84 |
if ( in_array( $calc_from, self::POST_PROPS, true ) ) { |
| 85 |
$item['calculate'] = $post->$calc_from; |
| 86 |
} else { |
| 87 |
$item['calculate'] = get_post_meta( $post->ID, $calc_from, true ); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
yield $item; |
| 92 |
|
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
} |
| 97 |
|