LLMS_Admin_Post_Table_Instructors
Contents
Source Source
File: includes/admin/post-types/post-tables/class.llms.admin.post.table.instructors.php
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | class LLMS_Admin_Post_Table_Instructors { private $post_types = array ( 'course' , 'llms_membership' , ); /** * Constructor * * @return void * @since 3.3.0 * @version 3.13.0 */ public function __construct() { foreach ( $this ->post_types as $post_type ) { add_filter( 'manage_' . $post_type . '_posts_columns' , array ( $this , 'add_columns' ), 10, 1 ); add_action( 'manage_' . $post_type . '_posts_custom_column' , array ( $this , 'manage_columns' ), 10, 2 ); add_filter( 'views_edit-' . $post_type , array ( $this , 'get_views' ), 777, 1 ); } add_action( 'pre_get_posts' , array ( $this , 'pre_get_posts' ) ); } /** * Add Custom Columns * * @param array $columns array of default columns * @return array * @since 3.13.0 * @version 3.13.0 */ public function add_columns( $columns ) { $offset = array_search ( 'title' , array_keys ( $columns ) ); $add = array ( 'llms-instructors' => __( 'Instructors' , 'lifterlms' ), ); return array_slice ( $columns , 0, $offset + 1 ) + $add + array_slice ( $columns , $offset ); } /** * Create a string that can be used in a LIKE query for finding a student's id in the llms_instructors * meta field on the usermeta table * * @param int $user_id WP User ID * @return string * @since 3.13.0 * @version 3.13.0 */ private function get_serialized_id( $user_id ) { $val = serialize( array ( 'id' => absint( $user_id ), ) ); return str_replace ( array ( 'a:1:{' , '}' ), '' , $val ); } /** * Ensure that the "Mine" view quick link at the top of the table displays the correct number * Most of this is based on WordPress core functions found in wp-admin/includes/class-wp-posts-list-table.php * * @since 3.13.0 * @since 3.24.0 Unknown. * @since 3.35.0 Verify nonces and sanitize `$_POST` data. * @since 4.5.1 Use `$_GET` data instead of `$_POST`. * @since 5.9.0 Stop using deprecated `FILTER_SANITIZE_STRING`. * * @param array $views array of view link HTML string * @return array */ public function get_views( $views ) { $post_type = llms_filter_input_sanitize_string( INPUT_GET, 'post_type' ); $current_user_id = get_current_user_id(); $exclude_states = get_post_stati( array ( 'show_in_admin_all_list' => false, ) ); global $wpdb ; // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared -- Statuses are sanitized. $count = intval ( $wpdb ->get_var( $wpdb ->prepare( " SELECT COUNT ( 1 ) FROM $wpdb ->posts AS p JOIN $wpdb ->postmeta AS m ON p.ID = m.post_id AND m.meta_key = '_llms_instructors' AND m.meta_value LIKE %s WHERE p.post_type = %s AND p.post_status NOT IN ( '" . implode( "' , '", $exclude_states ) . "' ) ", '%' . $this ->get_serialized_id( $current_user_id ) . '%' , $post_type ) ) ); // phpcs:enable WordPress.DB.PreparedSQL.NotPrepared $label = sprintf( _nx( 'Mine <span class="count">(%s)</span>' , 'Mine <span class="count">(%s)</span>' , $count , 'posts' , 'lifterlms' ), number_format_i18n( $count ) ); $url = add_query_arg( array ( 'post_type' => $post_type , 'author' => $current_user_id , ), 'edit.php' ); $class = '' ; if ( isset( $_GET [ 'author' ] ) && ( $_GET [ 'author' ] == $current_user_id ) ) { $class = 'class="current"' ; } /** * If mine doesn't already exist in views, we need to add it after "All" manually * to preserve the user experience. */ if ( ! isset( $views [ 'mine' ] ) ) { $offset = array_search ( 'all' , array_keys ( $views ) ); $add = array ( 'mine' => '' , ); $views = array_slice ( $views , 0, $offset + 1 ) + $add + array_slice ( $views , $offset + 1 ); } $views [ 'mine' ] = sprintf( '<a href="%1$s"%2$s>%3$s</a>' , esc_url( $url ), $class , $label ); return $views ; } /** * Manage content of custom columns * * @param string $column column key/name * @param int $post_id WP Post ID of the coupon for the row * @return void * @since 3.13.0 * @version 3.23.0 */ public function manage_columns( $column , $post_id ) { $post = llms_get_post( $post_id ); switch ( $column ) { case 'llms-instructors' : $instructors = $post ->get_instructors(); $htmls = array (); foreach ( $instructors as $user ) { $url = add_query_arg( array ( 'post_type' => $post ->get( 'type' ), 'author' => $user [ 'id' ], ), 'edit.php' ); $instructor = llms_get_instructor( $user [ 'id' ] ); if ( $instructor ) { $htmls [] = sprintf( '<a href="%1$s">%2$s</a>' , esc_url( $url ), $instructor ->get( 'display_name' ) ); } } echo implode( ', ' , $htmls ); break ; } } /** * Handle course & membership queries for searching by llms_instructors rather than author * * @param obj $query WP_Query * @return void * @since 3.13.0 * @version 3.13.0 */ public function pre_get_posts( $query ) { if ( ! is_admin() ) { return ; } if ( ! $query ->is_main_query() ) { return ; } // Don't run duplicates. if ( $query ->get( 'llms_instructor_query' ) ) { return ; } // phpcs:ignore -- commented out code // var_dump( $query->query_vars ); if ( isset( $query ->query_vars[ 'post_type' ] ) && in_array( $query ->query_vars[ 'post_type' ], $this ->post_types ) && ! empty ( $query ->query_vars[ 'author' ] ) ) { // Get the query or a default to work with. $meta_query = $query ->get( 'meta_query' ); if ( ! $meta_query ) { $meta_query = array (); } /** * Set an and relation for our filters * if other filters already exist, we'll ensure we obey them as well this way. */ $meta_query [ 'relation' ] = 'AND' ; $meta_query [] = array ( 'compare' => 'LIKE' , 'key' => '_llms_instructors' , 'value' => $this ->get_serialized_id( $query ->query_vars[ 'author' ] ), ); $query ->set( 'meta_query' , $meta_query ); $query ->set( 'llms_instructor_query' , true ); $query ->set( 'author' , '' ); } } } |
Expand full source code Collapse full source code View on GitHub
Methods Methods
- __construct — Constructor
- add_columns — Add Custom Columns
- get_serialized_id — Create a string that can be used in a LIKE query for finding a student's id in the llms_instructors meta field on the usermeta table
- get_views — Ensure that the "Mine" view quick link at the top of the table displays the correct number Most of this is based on WordPress core functions found in wp-admin/includes/class-wp-posts-list-table.php
- manage_columns — Manage content of custom columns
- pre_get_posts — Handle course & membership queries for searching by llms_instructors rather than author
Changelog Changelog
Version | Description |
---|---|
3.35.0 | Verify nonces and sanitize $_POST data. |
3.13.0 | Introduced. |