| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class LP_Modal_Search_Users |
| 4 |
* |
| 5 |
* @deprecated 4.2.6.9.3 |
| 6 |
*/ |
| 7 |
class LP_Modal_Search_Users { |
| 8 |
|
| 9 |
/** |
| 10 |
* @var array |
| 11 |
*/ |
| 12 |
protected $_options = array(); |
| 13 |
|
| 14 |
/** |
| 15 |
* @var array |
| 16 |
*/ |
| 17 |
protected $_query_args = array(); |
| 18 |
|
| 19 |
/** |
| 20 |
* @var WP_User_Query |
| 21 |
*/ |
| 22 |
protected $_query = null; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var array |
| 26 |
*/ |
| 27 |
protected $_items = array(); |
| 28 |
|
| 29 |
/** |
| 30 |
* @var bool |
| 31 |
*/ |
| 32 |
protected $_changed = true; |
| 33 |
|
| 34 |
/** |
| 35 |
* LP_Modal_Search_Users constructor. |
| 36 |
* |
| 37 |
* @param string $options |
| 38 |
*/ |
| 39 |
public function __construct( $options = '' ) { |
| 40 |
add_action( 'admin_print_footer_scripts', array( $this, 'js_template' ) ); |
| 41 |
$this->_options = wp_parse_args( |
| 42 |
$options, |
| 43 |
array( |
| 44 |
'type' => '', |
| 45 |
'context' => '', |
| 46 |
'context_id' => '', |
| 47 |
'exclude' => '', |
| 48 |
'term' => '', |
| 49 |
'text_format' => '{{display_name}} ({{email}})', |
| 50 |
'add_button' => __( 'Add', 'learnpress' ), |
| 51 |
'close_button' => __( 'Close', 'learnpress' ), |
| 52 |
'title' => __( 'Search users', 'learnpress' ), |
| 53 |
'number' => 10, |
| 54 |
'paged' => 1, |
| 55 |
) |
| 56 |
); |
| 57 |
|
| 58 |
if ( is_string( $this->_options['exclude'] ) ) { |
| 59 |
$this->_options['exclude'] = explode( ',', $this->_options['exclude'] ); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
protected function _get_items() { |
| 64 |
$term = $this->_options['term']; |
| 65 |
$type = $this->_options['type']; |
| 66 |
$context = $this->_options['context']; |
| 67 |
$context_id = $this->_options['context_id']; |
| 68 |
|
| 69 |
$exclude = array_unique( (array) apply_filters( 'learn-press/modal-search-user/exclude', $this->_options['exclude'], $type, $context, $context_id ) ); |
| 70 |
|
| 71 |
if ( ! empty( $exclude ) ) { |
| 72 |
$exclude = array_map( 'intval', $exclude ); |
| 73 |
} |
| 74 |
|
| 75 |
$args = array( |
| 76 |
'number' => $this->_options['number'], |
| 77 |
'offset' => ( $this->_options['paged'] - 1 ) * $this->_options['number'], |
| 78 |
'exclude' => $exclude, |
| 79 |
); |
| 80 |
if ( $term ) { |
| 81 |
$args['search'] = sprintf( '*%s*', esc_attr( $term ) ); |
| 82 |
$args['search_columns'] = array( 'user_login', 'user_email' ); |
| 83 |
} |
| 84 |
$this->_query_args = apply_filters( 'learn-press/modal-search-users/args', $args, $context, $context_id ); |
| 85 |
|
| 86 |
// The Query |
| 87 |
$this->_query = new WP_User_Query( $args ); |
| 88 |
$this->_items = array(); |
| 89 |
|
| 90 |
$results = $this->_query->get_results(); |
| 91 |
if ( $results ) { |
| 92 |
foreach ( $results as $user ) { |
| 93 |
$this->_items[ $user->ID ] = $user->user_login; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
return $this->_items; |
| 98 |
} |
| 99 |
|
| 100 |
public function get_items() { |
| 101 |
if ( $this->_changed ) { |
| 102 |
$this->_get_items(); |
| 103 |
} |
| 104 |
|
| 105 |
return $this->_items; |
| 106 |
} |
| 107 |
|
| 108 |
function get_pagination() { |
| 109 |
|
| 110 |
$pagination = ''; |
| 111 |
$items = $this->get_items(); |
| 112 |
|
| 113 |
if ( $items && $this->_options['number'] > 0 ) { |
| 114 |
|
| 115 |
$number = $this->_options['number']; |
| 116 |
$total = $this->_query->get_total(); |
| 117 |
$max_num_pages = intval( $total / $number ); |
| 118 |
|
| 119 |
if ( $total % $number ) { |
| 120 |
$max_num_pages ++; |
| 121 |
} |
| 122 |
|
| 123 |
if ( $this->_options['paged'] && $max_num_pages > 1 ) { |
| 124 |
$pagenum_link = html_entity_decode( get_pagenum_link() ); |
| 125 |
|
| 126 |
$query_args = array(); |
| 127 |
$url_parts = explode( '?', $pagenum_link ); |
| 128 |
|
| 129 |
if ( isset( $url_parts[1] ) ) { |
| 130 |
wp_parse_str( $url_parts[1], $query_args ); |
| 131 |
} |
| 132 |
|
| 133 |
$pagenum_link = esc_url_raw( remove_query_arg( array_keys( $query_args ), $pagenum_link ) ); |
| 134 |
$pagenum_link = trailingslashit( $pagenum_link ) . '%_%'; |
| 135 |
|
| 136 |
$pagination = paginate_links( |
| 137 |
array( |
| 138 |
'base' => $pagenum_link, |
| 139 |
'total' => $max_num_pages, |
| 140 |
'current' => max( 1, $this->_options['paged'] ), |
| 141 |
'mid_size' => 1, |
| 142 |
'add_args' => array_map( 'urlencode', $query_args ), |
| 143 |
'prev_text' => __( '<', 'learnpress' ), |
| 144 |
'next_text' => __( '>', 'learnpress' ), |
| 145 |
'type' => '', |
| 146 |
) |
| 147 |
); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
return $pagination; |
| 152 |
} |
| 153 |
|
| 154 |
public function get_html_items() { |
| 155 |
|
| 156 |
$variables = array( |
| 157 |
'id', |
| 158 |
'email', |
| 159 |
'user_login', |
| 160 |
'description', |
| 161 |
'first_name', |
| 162 |
'last_name', |
| 163 |
'nickname', |
| 164 |
'display_name', |
| 165 |
); |
| 166 |
|
| 167 |
ob_start(); |
| 168 |
|
| 169 |
$items = $this->get_items(); |
| 170 |
|
| 171 |
if ( $items ) { |
| 172 |
foreach ( $items as $id => $item ) { |
| 173 |
$the_user = learn_press_get_user( $id ); |
| 174 |
$text = str_replace( '{{id}}', $the_user->get_id(), $this->_options['text_format'] ); |
| 175 |
$data = array(); |
| 176 |
foreach ( $variables as $variable ) { |
| 177 |
$text = str_replace( '{{' . $variable . '}}', $the_user->get_data( $variable ), $text ); |
| 178 |
$data[ $variable ] = $the_user->get_data( $variable ); |
| 179 |
} |
| 180 |
$data['id'] = $id; |
| 181 |
printf( '<li class="%s" data-id="%d" data-data="%s"><label>', 'lp-result-item user-' . $id, $id, esc_attr( wp_json_encode( $data ) ) ); |
| 182 |
if ( $this->_options['multiple'] ) { |
| 183 |
printf( |
| 184 |
' |
| 185 |
<input type="checkbox" value="%d" name="selectedItems[]"> |
| 186 |
<span class="lp-item-text">%s</span> |
| 187 |
', |
| 188 |
$id, |
| 189 |
esc_attr( $text ) |
| 190 |
); |
| 191 |
} else { |
| 192 |
printf( |
| 193 |
' |
| 194 |
<a href=""><span class="lp-item-text">%s</span></a> |
| 195 |
', |
| 196 |
esc_attr( $text ) |
| 197 |
); |
| 198 |
} |
| 199 |
|
| 200 |
echo '</li>'; |
| 201 |
} |
| 202 |
} else { |
| 203 |
echo '<li>' . apply_filters( 'learn-press/modal-search-users/not-found', __( 'No item found', 'learnpress' ), $this->_options['type'] ) . '</li>'; |
| 204 |
} |
| 205 |
|
| 206 |
return ob_get_clean(); |
| 207 |
} |
| 208 |
|
| 209 |
public function js_template() { |
| 210 |
$view = learn_press_get_admin_view( 'modal-search-users' ); |
| 211 |
include $view; |
| 212 |
} |
| 213 |
|
| 214 |
public static function instance() { |
| 215 |
static $instance; |
| 216 |
if ( is_null( $instance ) ) { |
| 217 |
$instance = new self(); |
| 218 |
} |
| 219 |
|
| 220 |
return $instance; |
| 221 |
} |
| 222 |
} |
| 223 |
|