base-source.php
4 years ago
preset-source-post.php
4 years ago
preset-source-query-var.php
4 years ago
preset-source-user.php
4 years ago
preset-source-post.php
170 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Presets\Sources; |
| 5 | |
| 6 | |
| 7 | class Preset_Source_Post extends Base_Source { |
| 8 | |
| 9 | private $array_allowed; |
| 10 | |
| 11 | public function get_id() { |
| 12 | return 'post'; |
| 13 | } |
| 14 | |
| 15 | public function after_init() { |
| 16 | $this->set_array_allowed(); |
| 17 | } |
| 18 | |
| 19 | private function set_array_allowed() { |
| 20 | if ( isset( $this->field_args['type'] ) ) { |
| 21 | $this->array_allowed = 'checkbox-field' === $this->field_args['type']; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | public function on_sanitize() { |
| 26 | if ( ! is_user_logged_in() ) { |
| 27 | return false; |
| 28 | } |
| 29 | |
| 30 | if ( absint( $this->src()->post_author ) !== get_current_user_id() && ! current_user_can( 'edit_others_posts' ) ) { |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | return true; |
| 35 | } |
| 36 | |
| 37 | public function query_source() { |
| 38 | $post_from = ! empty( $this->preset_data['post_from'] ) ? $this->preset_data['post_from'] : 'current_post'; |
| 39 | |
| 40 | if ( 'current_post' === $post_from ) { |
| 41 | $post_id = get_the_ID(); |
| 42 | } else { |
| 43 | $var = ! empty( $this->preset_data['query_var'] ) ? $this->preset_data['query_var'] : ''; |
| 44 | $post_id = ( $var && isset( $_REQUEST[ $var ] ) ) ? $_REQUEST[ $var ] : false; |
| 45 | } |
| 46 | |
| 47 | if ( $post_id ) { |
| 48 | return get_post( $post_id ); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | |
| 53 | protected function can_get_preset() { |
| 54 | return ( parent::can_get_preset() && |
| 55 | ( |
| 56 | absint( $this->src()->post_author ) === get_current_user_id() |
| 57 | || current_user_can( 'edit_others_posts' ) |
| 58 | ) |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | public function _source__post_meta() { |
| 63 | if ( empty( $this->field_data['key'] ) ) { |
| 64 | return self::EMPTY; |
| 65 | } |
| 66 | |
| 67 | $value = get_post_meta( $this->src()->ID, $this->field_data['key'], true ); |
| 68 | |
| 69 | if ( $this->is_repeater_val( $value ) ) { |
| 70 | |
| 71 | $prepared_value = array(); |
| 72 | |
| 73 | foreach ( $value as $index => $row ) { |
| 74 | |
| 75 | $prepared_row = array(); |
| 76 | |
| 77 | foreach ( $row as $item_key => $item_value ) { |
| 78 | |
| 79 | $item_key = $this->get_key_from_map( $item_key ); |
| 80 | |
| 81 | $prepared_row[ $item_key ] = $item_value; |
| 82 | } |
| 83 | |
| 84 | $prepared_value[] = $prepared_row; |
| 85 | |
| 86 | } |
| 87 | $value = $prepared_value; |
| 88 | } else if ( function_exists( 'jet_engine' ) |
| 89 | && jet_engine()->relations |
| 90 | && jet_engine()->relations->is_relation_key( $this->field_data['key'] ) ) { |
| 91 | |
| 92 | $info = jet_engine()->relations->get_relation_info( $this->field_data['key'] ); |
| 93 | |
| 94 | if ( ! $info ) { |
| 95 | return self::EMPTY; |
| 96 | } |
| 97 | |
| 98 | $args = array( |
| 99 | 'post_id' => $this->src()->ID, |
| 100 | 'post_type_1' => $info['post_type_1'], |
| 101 | 'post_type_2' => $info['post_type_2'], |
| 102 | ); |
| 103 | |
| 104 | if ( $this->src()->post_type === $info['post_type_1'] ) { |
| 105 | $args['from'] = $info['post_type_2']; |
| 106 | } else { |
| 107 | $args['from'] = $info['post_type_1']; |
| 108 | } |
| 109 | |
| 110 | $value = jet_engine()->relations->get_related_posts( $args ); |
| 111 | |
| 112 | } |
| 113 | |
| 114 | return $value; |
| 115 | } |
| 116 | |
| 117 | public function _source__post_terms() { |
| 118 | if ( empty( $this->field_data['key'] ) ) { |
| 119 | return self::EMPTY; |
| 120 | } |
| 121 | |
| 122 | $value = wp_get_post_terms( $this->src()->ID, $this->field_data['key'] ); |
| 123 | |
| 124 | if ( empty( $value ) || is_wp_error( $value ) ) { |
| 125 | return ''; |
| 126 | } else { |
| 127 | if ( $this->array_allowed ) { |
| 128 | $value = array_map( function ( $term ) { |
| 129 | return strval( $term->term_id ); |
| 130 | }, $value ); |
| 131 | } else { |
| 132 | $value = $value[0]; |
| 133 | $value = $value->term_id; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return $value; |
| 138 | } |
| 139 | |
| 140 | public function _source__post_thumb() { |
| 141 | return get_post_thumbnail_id( $this->src()->ID ); |
| 142 | } |
| 143 | |
| 144 | public function is_repeater_val( $value ) { |
| 145 | if ( is_array( $value ) && ! empty( $value ) ) { |
| 146 | $value = array_values( $value ); |
| 147 | |
| 148 | return is_array( $value[0] ); |
| 149 | } else { |
| 150 | return false; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | public function get_key_from_map( $repeater_key ) { |
| 155 | |
| 156 | foreach ( $this->fields_map as $field => $data ) { |
| 157 | |
| 158 | if ( 'post_meta' === $this->prop |
| 159 | && ! empty( $this->preset_data['key'] ) |
| 160 | && $repeater_key == $this->preset_data['key'] ) { |
| 161 | return $field; |
| 162 | } |
| 163 | |
| 164 | } |
| 165 | |
| 166 | return $repeater_key; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | } |