| 1 |
<?php |
| 2 |
/** |
| 3 |
* Defines the functions which called by hooks |
| 4 |
*/ |
| 5 |
|
| 6 |
/***************************************/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Filter post types the user can access in admin |
| 10 |
* |
| 11 |
* @param $query |
| 12 |
* @editor tungnx |
| 13 |
* @modify 4.1.4 - comment - not use make redundancy |
| 14 |
*/ |
| 15 |
/*function _learn_press_set_user_items( $query ) { |
| 16 |
global $post_type, $pagenow, $wpdb; |
| 17 |
|
| 18 |
if ( ! function_exists( 'wp_get_current_user' ) ) { |
| 19 |
include( ABSPATH . 'wp-includes/pluggable.php' ); |
| 20 |
} |
| 21 |
|
| 22 |
if ( ! did_action( 'plugin_loaded' ) || current_user_can( 'manage_options' ) || ! current_user_can( LP_TEACHER_ROLE ) || ! is_admin() || ( $pagenow != 'edit.php' ) ) { |
| 23 |
return $query; |
| 24 |
} |
| 25 |
|
| 26 |
if ( ! in_array( |
| 27 |
$post_type, |
| 28 |
apply_filters( |
| 29 |
'learn-press/filter-user-access-types', |
| 30 |
array( |
| 31 |
LP_COURSE_CPT, |
| 32 |
LP_LESSON_CPT, |
| 33 |
LP_QUIZ_CPT, |
| 34 |
LP_QUESTION_CPT, |
| 35 |
) |
| 36 |
) |
| 37 |
) ) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
$items = $wpdb->get_col( |
| 42 |
$wpdb->prepare( |
| 43 |
"SELECT ID FROM $wpdb->posts |
| 44 |
WHERE post_type = %s |
| 45 |
AND post_author = %d", |
| 46 |
$post_type, |
| 47 |
get_current_user_id() |
| 48 |
) |
| 49 |
); |
| 50 |
|
| 51 |
if ( count( $items ) == 0 ) { |
| 52 |
$query->set( 'post_type', 'no-item-access' ); |
| 53 |
} else { |
| 54 |
$query->set( 'post__in', $items ); |
| 55 |
} |
| 56 |
add_filter( 'views_edit-' . $post_type . '', '_learn_press_restrict_view_items', 10 ); |
| 57 |
} |
| 58 |
|
| 59 |
add_action( 'pre_get_posts', '_learn_press_set_user_items', 10 );*/ |
| 60 |
|
| 61 |
/** |
| 62 |
* Restrict user views |
| 63 |
* |
| 64 |
* @param $views |
| 65 |
* |
| 66 |
* @return mixed |
| 67 |
* @editor tungnx |
| 68 |
* @modify 4.1.4 - comment - not see use anywhere |
| 69 |
*/ |
| 70 |
/*function _learn_press_restrict_view_items( $views ) { |
| 71 |
$post_type = get_query_var( 'post_type' ); |
| 72 |
$new_views = array( |
| 73 |
'all' => __( 'All', 'learnpress' ), |
| 74 |
'publish' => __( 'Published', 'learnpress' ), |
| 75 |
'private' => __( 'Private', 'learnpress' ), |
| 76 |
'pending' => __( 'Pending Review', 'learnpress' ), |
| 77 |
'future' => __( 'Scheduled', 'learnpress' ), |
| 78 |
'draft' => __( 'Draft', 'learnpress' ), |
| 79 |
'trash' => __( 'Trash', 'learnpress' ), |
| 80 |
); |
| 81 |
|
| 82 |
$url = 'edit.php'; |
| 83 |
|
| 84 |
foreach ( $new_views as $view => $name ) { |
| 85 |
$query = array( |
| 86 |
'post_type' => $post_type, |
| 87 |
); |
| 88 |
if ( $view == 'all' ) { |
| 89 |
$query['all_posts'] = 1; |
| 90 |
$class = ( get_query_var( 'all_posts' ) == 1 || ( get_query_var( 'post_status' ) == '' && get_query_var( 'author' ) == '' ) ) ? ' class="current"' : ''; |
| 91 |
} else { |
| 92 |
$query['post_status'] = $view; |
| 93 |
$class = ( get_query_var( 'post_status' ) == $view ) ? ' class="current"' : ''; |
| 94 |
} |
| 95 |
$result = new WP_Query( $query ); |
| 96 |
|
| 97 |
if ( $result->found_posts > 0 ) { |
| 98 |
$views[ $view ] = sprintf( |
| 99 |
'<a href="%s"' . $class . '>' . $name . ' <span class="count">(%d)</span></a>', |
| 100 |
esc_url_raw( add_query_arg( $query, $url ) ), |
| 101 |
$result->found_posts |
| 102 |
); |
| 103 |
} else { |
| 104 |
unset( $views[ $view ] ); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
unset( $views['mine'] ); |
| 109 |
|
| 110 |
return $views; |
| 111 |
}*/ |
| 112 |
|
| 113 |
/** |
| 114 |
* Update permalink structure for course |
| 115 |
*/ |
| 116 |
// function learn_press_update_permalink_structure() { |
| 117 |
// global $pagenow; |
| 118 |
|
| 119 |
// if ( $pagenow != 'options-permalink.php' ) { |
| 120 |
// return; |
| 121 |
// } |
| 122 |
|
| 123 |
// if ( strtolower( $_SERVER['REQUEST_METHOD'] ) != 'post' ) { |
| 124 |
// return; |
| 125 |
// } |
| 126 |
|
| 127 |
// $rewrite_prefix = ''; |
| 128 |
// $permalink_structure = ! empty( $_REQUEST['permalink_structure'] ) ? $_REQUEST['permalink_structure'] : ''; |
| 129 |
|
| 130 |
// if ( $permalink_structure ) { |
| 131 |
// $rewrite_prefix = array(); |
| 132 |
// $segs = explode( '/', $permalink_structure ); |
| 133 |
|
| 134 |
// if ( sizeof( $segs ) ) { |
| 135 |
// foreach ( $segs as $seg ) { |
| 136 |
// if ( strpos( $seg, '%' ) !== false || $seg == 'archives' ) { |
| 137 |
// break; |
| 138 |
// } |
| 139 |
// $rewrite_prefix[] = $seg; |
| 140 |
// } |
| 141 |
// } |
| 142 |
|
| 143 |
// $rewrite_prefix = array_filter( $rewrite_prefix ); |
| 144 |
|
| 145 |
// if ( sizeof( $rewrite_prefix ) ) { |
| 146 |
// $rewrite_prefix = join( '/', $rewrite_prefix ) . '/'; |
| 147 |
// } else { |
| 148 |
// $rewrite_prefix = ''; |
| 149 |
// } |
| 150 |
// } |
| 151 |
|
| 152 |
// update_option( 'learn_press_permalink_structure', $rewrite_prefix ); |
| 153 |
// } |
| 154 |
// add_action( 'init', 'learn_press_update_permalink_structure' ); |
| 155 |
|
| 156 |
/** |
| 157 |
* Active Courses menu under LearnPress |
| 158 |
* when user is editing course and course |
| 159 |
* category. |
| 160 |
*/ |
| 161 |
function learn_press_active_course_menu() { |
| 162 |
$post_type = LP_Request::get( 'post_type' ); |
| 163 |
|
| 164 |
if ( ! $post_type ) { |
| 165 |
return; |
| 166 |
} |
| 167 |
?> |
| 168 |
|
| 169 |
<script> |
| 170 |
jQuery( function( $ ) { |
| 171 |
var $lpMainMenu = $( '#toplevel_page_learn_press' ), |
| 172 |
href = 'edit.php?post_type=<?php echo esc_js( $_GET['post_type'] ); ?>', |
| 173 |
$current = $( 'a[href="' + href + '"]', $lpMainMenu ); |
| 174 |
|
| 175 |
if ( $current.length ) { |
| 176 |
$current.addClass( 'current' ); |
| 177 |
$current.parent( 'li' ).addClass( 'current' ); |
| 178 |
} |
| 179 |
|
| 180 |
<?php if ( $post_type === LP_COURSE_CPT && LP_Request::get( 'taxonomy' ) === 'course_category' ) { ?> |
| 181 |
$( 'body' ).removeClass( 'sticky-menu' ); |
| 182 |
$lpMainMenu.addClass( 'wp-has-current-submenu wp-menu-open' ).removeClass( 'wp-not-current-submenu' ); |
| 183 |
$lpMainMenu.children( 'a' ).addClass( 'wp-has-current-submenu wp-menu-open' ).removeClass( 'wp-not-current-submenu' ); |
| 184 |
<?php |
| 185 |
} |
| 186 |
?> |
| 187 |
}); |
| 188 |
</script> |
| 189 |
|
| 190 |
<?php |
| 191 |
} |
| 192 |
add_action( 'admin_footer', 'learn_press_active_course_menu' ); |
| 193 |
|
| 194 |
/* |
| 195 |
* Display tabs related to course in admin when user |
| 196 |
* viewing/editing course/category/tags. |
| 197 |
*/ |
| 198 |
function learn_press_admin_course_tabs() { |
| 199 |
if ( ! is_admin() ) { |
| 200 |
return; |
| 201 |
} |
| 202 |
|
| 203 |
$wp_screen = get_current_screen(); |
| 204 |
|
| 205 |
if ( ! $wp_screen || 'edit-lp_course' !== $wp_screen->id ) { |
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
$admin_tabs = apply_filters( |
| 210 |
'learn_press_admin_tabs_info', |
| 211 |
array( |
| 212 |
10 => array( |
| 213 |
'link' => 'edit.php?post_type=lp_course', |
| 214 |
'name' => esc_html__( 'Courses', 'learnpress' ), |
| 215 |
'id' => 'edit-lp_course', |
| 216 |
), |
| 217 |
20 => array( |
| 218 |
'link' => 'edit-tags.php?taxonomy=course_category', |
| 219 |
'name' => esc_html__( 'Categories', 'learnpress' ), |
| 220 |
'id' => 'edit-course_category', |
| 221 |
), |
| 222 |
30 => array( |
| 223 |
'link' => 'edit-tags.php?taxonomy=course_tag', |
| 224 |
'name' => esc_html__( 'Tags', 'learnpress' ), |
| 225 |
'id' => 'edit-course_tag', |
| 226 |
), |
| 227 |
|
| 228 |
) |
| 229 |
); |
| 230 |
|
| 231 |
ksort( $admin_tabs ); |
| 232 |
$tabs = array(); |
| 233 |
|
| 234 |
foreach ( $admin_tabs as $key => $value ) { |
| 235 |
array_push( $tabs, $key ); |
| 236 |
} |
| 237 |
|
| 238 |
$pages = apply_filters( |
| 239 |
'learn_press_admin_tabs_on_pages', |
| 240 |
array( 'edit-lp_course', 'edit-course_category', 'edit-course_tag', 'lp_course' ) |
| 241 |
); |
| 242 |
|
| 243 |
$admin_tabs_on_page = array(); |
| 244 |
|
| 245 |
foreach ( $pages as $page ) { |
| 246 |
$admin_tabs_on_page[ $page ] = $tabs; |
| 247 |
} |
| 248 |
|
| 249 |
$current_page_id = get_current_screen()->id; |
| 250 |
$current_user = wp_get_current_user(); |
| 251 |
|
| 252 |
if ( ! in_array( 'administrator', $current_user->roles ) ) { |
| 253 |
return; |
| 254 |
} |
| 255 |
|
| 256 |
if ( ! empty( $admin_tabs_on_page[ $current_page_id ] ) && count( $admin_tabs_on_page[ $current_page_id ] ) ) { |
| 257 |
|
| 258 |
echo '<h2 class="nav-tab-wrapper lp-nav-tab-wrapper">'; |
| 259 |
|
| 260 |
foreach ( $admin_tabs_on_page[ $current_page_id ] as $admin_tab_id ) { |
| 261 |
$class = ( $admin_tabs[ $admin_tab_id ]['id'] == $current_page_id ) ? 'nav-tab nav-tab-active' : 'nav-tab'; |
| 262 |
// echo '<a href="' . admin_url( $admin_tabs[ $admin_tab_id ]['link'] ) . '" class="' . $class . ' nav-tab-' . $admin_tabs[ $admin_tab_id ]['id'] . '">' . $admin_tabs[ $admin_tab_id ]['name'] . '</a>'; |
| 263 |
echo wp_kses( |
| 264 |
sprintf( |
| 265 |
'<a href="%s" class="%s">%s</a>', |
| 266 |
admin_url( $admin_tabs[ $admin_tab_id ]['link'] ), |
| 267 |
$class . ' nav-tab-' . $admin_tabs[ $admin_tab_id ]['id'], |
| 268 |
$admin_tabs[ $admin_tab_id ]['name'] |
| 269 |
), |
| 270 |
true |
| 271 |
); |
| 272 |
} |
| 273 |
|
| 274 |
echo '</h2>'; |
| 275 |
} |
| 276 |
} |
| 277 |
add_action( 'all_admin_notices', 'learn_press_admin_course_tabs' ); |
| 278 |
|
| 279 |
/** |
| 280 |
* Create some warning messages: |
| 281 |
* + LP Profile page is not setup |
| 282 |
* + LP Checkout page is not setup |
| 283 |
*/ |
| 284 |
if ( ! function_exists( 'lp_remove_admin_warning' ) ) { |
| 285 |
function lp_remove_admin_warning() { |
| 286 |
if ( isset( $_POST['action'] ) && $_POST['action'] === 'lp_remove_admin_warning' && isset( $_POST['name'] ) ) { |
| 287 |
if ( empty( $transient_profile ) ) { |
| 288 |
set_transient( $_POST['name'], true, 60 * 60 * 12 ); |
| 289 |
} |
| 290 |
|
| 291 |
echo 'success'; |
| 292 |
wp_die(); |
| 293 |
|
| 294 |
} |
| 295 |
|
| 296 |
echo 'error'; |
| 297 |
wp_die(); |
| 298 |
} |
| 299 |
} |
| 300 |
//add_action( 'wp_ajax_lp_remove_admin_warning', 'lp_remove_admin_warning' ); |
| 301 |
|
| 302 |
|