_options = apply_filters( 'learn-press/modal-search-items-args', wp_parse_args( $options, array( 'type' => '', 'context' => '', 'context_id' => '', 'exclude' => '', 'term' => '', 'add_button' => __( 'Add', 'learnpress' ), 'close_button' => __( 'Close', 'learnpress' ), 'title' => __( 'Search items', 'learnpress' ), 'limit' => 10, 'paged' => 1, ) ) ); if ( is_string( $this->_options['exclude'] ) ) { $this->_options['exclude'] = explode( ',', $this->_options['exclude'] ); } add_filter( 'learn-press/modal-search-items/exclude', array( $this, 'exclude_items' ), 10, 4 ); add_filter( 'learn-press/modal-search-items/args', array( $this, 'query_args' ), 10, 3 ); } /** * Build query args from object options and get posts. * * @return array */ protected function _get_items(): array { $user_id = get_current_user_id(); $term = $this->_options['term']; $type = $this->_options['type']; $context = $this->_options['context']; $context_id = $this->_options['context_id']; $exclude = array_unique( (array) apply_filters( 'learn-press/modal-search-items/exclude', $this->_options['exclude'], $type, $context, $context_id ) ); if ( is_array( $exclude ) ) { $exclude = array_map( 'intval', $exclude ); } $paged = max( 1, $this->_options['paged'] ); $args = array( 'post_type' => array( $type ), 'post_status' => 'publish', 'order' => 'ASC', 'orderby' => 'parent title', 'exclude' => $exclude, 'posts_per_page' => $this->_options['limit'], 'offset' => ( $paged - 1 ) * $this->_options['limit'], ); if ( 'order-items' === $context ) { $context_id = false; } $context_id = apply_filters( 'learn-press/modal-search-items/context-id', $context_id, $context ); if ( $context_id ) { // Admin can get all items if ( ! user_can( $user_id, 'administrator' ) ) { $args['author'] = get_post_field( 'post_author', $context_id ); } } if ( $term ) { $args['s'] = $term; } $this->_query_args = apply_filters( 'learn-press/modal-search-items/args', $args, $context, $context_id ); $posts = get_posts( $this->_query_args ); if ( $posts ) { $this->_items = wp_list_pluck( $posts, 'ID' ); } return $this->_items; } /** * Get the items * * @return array */ public function get_items() { if ( $this->_changed ) { $this->_get_items(); } return $this->_items; } /** * Get pagination in html. * * @param bool $html * * @return array|string */ function get_pagination( $html = true ) { $pagination = ''; $items = $this->get_items(); if ( $items ) { $args = $this->_query_args; if ( ! empty( $args['exclude'] ) ) { $args['post__not_in'] = $args['exclude']; } $q = new WP_Query( $args ); if ( $this->_options['paged'] && $q->max_num_pages > 1 ) { $pagenum_link = html_entity_decode( get_pagenum_link() ); $query_args = array(); $url_parts = explode( '?', $pagenum_link ); if ( isset( $url_parts[1] ) ) { wp_parse_str( $url_parts[1], $query_args ); } $pagenum_link = esc_url_raw( remove_query_arg( array_keys( $query_args ), $pagenum_link ) ); $pagenum_link = trailingslashit( $pagenum_link ) . '%_%'; $pagination = array( 'base' => $pagenum_link, 'total' => $q->max_num_pages, 'current' => max( 1, $this->_options['paged'] ), 'mid_size' => 1, 'add_args' => array_map( 'urlencode', $query_args ), 'prev_text' => __( '<', 'learnpress' ), 'next_text' => __( '>', 'learnpress' ), 'type' => '', ); if ( $html ) { $pagination = paginate_links( $pagination ); } } $this->_changed = false; } return $pagination; } /** * Return string of list items * * @return string */ public function get_html_items() { ob_start(); $items = $this->get_items(); if ( $items ) { foreach ( $items as $id => $item ) { $type = get_post_type( $item ); $type_object = get_post_type_object( $type ); $type_name = $type_object ? $type_object->labels->singular_name : ''; printf( '