|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace JustCoded\WP\Framework\Page_Builder\v25\Fields; |
| 4 | + |
| 5 | +/** |
| 6 | + * Class Just_Field_Post |
| 7 | + */ |
| 8 | +class Field_Select_Posts extends \SiteOrigin_Widget_Field_Base { |
| 9 | + |
| 10 | + /** |
| 11 | + * An array of post types to use in the autocomplete query. Only used for posts. |
| 12 | + * |
| 13 | + * @var array |
| 14 | + */ |
| 15 | + protected $post_types = ''; |
| 16 | + |
| 17 | + /** |
| 18 | + * Render field HTML |
| 19 | + * |
| 20 | + * @param mixed $value Widget values. |
| 21 | + * @param array $instance Widget instance. |
| 22 | + * |
| 23 | + * @return mixed|void |
| 24 | + */ |
| 25 | + protected function render_field( $value, $instance ) { |
| 26 | + $posts = array(); |
| 27 | + if ( ! empty( $value ) ) { |
| 28 | + $posts = get_posts( [ |
| 29 | + 'post__in' => $value, |
| 30 | + 'ignore_sticky_posts' => true, |
| 31 | + ] ); |
| 32 | + } |
| 33 | + ?> |
| 34 | + <select name="<?php echo esc_attr( $this->element_name ) ?>" id="<?php echo esc_attr( $this->element_id ) ?>" |
| 35 | + class="widefat jc-widget-field-select-posts" |
| 36 | + multiple="multiple" |
| 37 | + data-post_types="<?php echo esc_attr( $this->post_types ); ?>" |
| 38 | + > |
| 39 | + <?php if ( ! empty( $posts ) ) : foreach ( $posts as $post ) : ?> |
| 40 | + <option value="<?php echo esc_attr( $post->ID ); ?>" selected="selected"> |
| 41 | + <?php echo esc_html( $this->get_post_caption( $post ) ); ?> |
| 42 | + </option> |
| 43 | + <?php endforeach; endif; ?> |
| 44 | + </select> |
| 45 | + <?php |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Sanitize the input received from their HTML form field. |
| 50 | + * |
| 51 | + * @param mixed $value Value from user input. |
| 52 | + * @param array $instance Widget instance. |
| 53 | + * |
| 54 | + * @return array|mixed |
| 55 | + */ |
| 56 | + protected function sanitize_field_input( $value, $instance ) { |
| 57 | + $values = array(); |
| 58 | + if ( ! is_null( $value ) ) { |
| 59 | + $values = is_array( $value ) ? $value : array( $value ); |
| 60 | + $values = array_map( 'intval', $values ); |
| 61 | + } |
| 62 | + |
| 63 | + return $values; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Enqueue needed js. |
| 68 | + */ |
| 69 | + public function enqueue_scripts() { |
| 70 | + wp_enqueue_script( |
| 71 | + 'pagebuilder-widget-field-select-posts', |
| 72 | + plugin_dir_url( JTF_PLUGIN_FILE ) . 'framework/Page_Builder/v25/Fields/js/select-posts.js', |
| 73 | + array( 'jquery', 'select2' ), |
| 74 | + '1.0' |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Action to handle searching posts |
| 80 | + */ |
| 81 | + public static function ajax_search_posts() { |
| 82 | + if ( empty( $_REQUEST['_widgets_nonce'] ) |
| 83 | + || ! wp_verify_nonce( $_REQUEST['_widgets_nonce'], 'widgets_action' ) |
| 84 | + ) { |
| 85 | + wp_die( __( 'Invalid request.', 'so-widgets-bundle' ), 403 ); |
| 86 | + } |
| 87 | + |
| 88 | + global $wpdb; |
| 89 | + |
| 90 | + $filter = ''; |
| 91 | + $params = array( |
| 92 | + '%' . $wpdb->esc_like( $_REQUEST['term'] ) . '%', |
| 93 | + ); |
| 94 | + if ( ! empty( $_REQUEST['types'] ) ) { |
| 95 | + $registered_cpt = (array) get_post_types( array( |
| 96 | + 'public' => true, |
| 97 | + ) ); |
| 98 | + $post_types = array_intersect( explode( ',', $_REQUEST['types'] ), $registered_cpt ); |
| 99 | + if ( ! empty( $post_types ) ) { |
| 100 | + $filter .= ' AND post_type IN (' . trim( str_repeat( '%s,', count( $post_types ) ), ',' ) . ')'; |
| 101 | + $params = array_merge( $params, $post_types ); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + if ( ! empty( $_REQUEST['selected'] ) ) { |
| 106 | + $filter .= ' AND `ID` NOT IN (' . trim( str_repeat( '%d,', count( $_REQUEST['selected'] ) ), ',' ) . ')'; |
| 107 | + $params = array_merge( $params, $_REQUEST['selected'] ); |
| 108 | + } |
| 109 | + |
| 110 | + $rows = $wpdb->get_results( $wpdb->prepare( " |
| 111 | + SELECT * |
| 112 | + FROM {$wpdb->posts} |
| 113 | + WHERE |
| 114 | + post_status = 'publish' |
| 115 | + AND post_title LIKE %s |
| 116 | + {$filter} |
| 117 | + ORDER BY post_modified DESC |
| 118 | + LIMIT 20 |
| 119 | + ", $params ), OBJECT_K ); |
| 120 | + |
| 121 | + $results = []; |
| 122 | + foreach ( $rows as $row ) { |
| 123 | + $results[] = [ |
| 124 | + 'id' => $row->ID, |
| 125 | + 'text' => self::get_post_caption( $row ), |
| 126 | + ]; |
| 127 | + } |
| 128 | + |
| 129 | + wp_send_json( array( 'results' => $results ) ); |
| 130 | + exit; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Generate caption to display in select box |
| 135 | + * |
| 136 | + * @param \WP_Post $post Post to generate caption for. |
| 137 | + * |
| 138 | + * @return string |
| 139 | + */ |
| 140 | + protected static function get_post_caption( $post ) { |
| 141 | + return "$post->post_title ({$post->post_type})"; |
| 142 | + } |
| 143 | +} |
0 commit comments