PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/learnpress/inc/class-lp-shortcodes.php

https://gitlab.com/gregtyka/lfmawordpress
PHP | 324 lines | 263 code | 33 blank | 28 comment | 55 complexity | 99bbaa581b348d44644e55a3ae9f7552 MD5 | raw file
  1. <?php
  2. /**
  3. * @author ThimPress
  4. * @package LearnPress/Shortcodes
  5. * @version 1.0
  6. */
  7. if ( !defined( 'ABSPATH' ) ) {
  8. exit;
  9. }
  10. /**
  11. * LP_Shortcodes class
  12. */
  13. class LP_Shortcodes {
  14. /**
  15. * Init shortcodes
  16. */
  17. static function init() {
  18. $shortcodes = array(
  19. 'learn_press_confirm_order' => __CLASS__ . '::confirm_order',
  20. 'learn_press_profile' => __CLASS__ . '::profile',
  21. 'learn_press_become_teacher_form' => __CLASS__ . '::become_teacher_form',
  22. 'learn_press_cart' => __CLASS__ . '::cart',
  23. 'learn_press_checkout' => __CLASS__ . '::checkout',
  24. );
  25. foreach ( $shortcodes as $shortcode => $function ) {
  26. add_shortcode( apply_filters( "{$shortcode}_shortcode_tag", $shortcode ), $function );
  27. }
  28. add_action( 'template_redirect', array( __CLASS__, 'auto_shortcode' ) );
  29. }
  30. static function auto_shortcode( $template ) {
  31. if ( is_page() ) {
  32. global $post, $wp_query, $wp;
  33. $page_id = !empty( $wp_query->queried_object_id ) ?
  34. $wp_query->queried_object_id :
  35. ( !empty( $wp_query->query_vars['page_id'] ) ? $wp_query->query_vars['page_id'] : - 1 );
  36. if ( $page_id == learn_press_get_page_id( 'checkout' ) ) {
  37. if ( !preg_match( '/\[learn_press_checkout\s?(.*)\]/', $post->post_content ) ) {
  38. $post->post_content .= '[learn_press_checkout]';
  39. }
  40. } elseif ( $page_id == learn_press_get_page_id( 'profile' ) ) {
  41. if ( empty( $wp->query_vars['user'] ) ) {
  42. $current_user = wp_get_current_user();
  43. if ( !empty( $current_user->user_login ) ) {
  44. wp_redirect( learn_press_get_endpoint_url( '', $current_user->user_login, learn_press_get_page_link( 'profile' ) ) );
  45. die();
  46. } else {
  47. ob_start();
  48. wp_login_form(
  49. array(
  50. 'form_id' => 'learn-press-form-login'
  51. )
  52. );
  53. $content = ob_get_clean();
  54. $post->post_content = $content;
  55. }
  56. } else {
  57. $query = array();
  58. parse_str( $wp->matched_query, $query );
  59. if ( $query ) {
  60. $profile_endpoints = (array) LP()->settings->get( 'profile_endpoints' );
  61. $endpoints = array_keys( $profile_endpoints );
  62. foreach ( $query as $k => $v ) {
  63. if ( ( $k == 'view' ) ) {
  64. if ( !$v ) {
  65. $v = reset( $profile_endpoints );
  66. }
  67. if ( !in_array( $v, apply_filters( 'learn_press_profile_tab_endpoints', $profile_endpoints ) ) ) {
  68. learn_press_404_page();
  69. }
  70. }
  71. if ( !empty( $v ) ) {
  72. $wp->query_vars[$k] = $v;
  73. }
  74. }
  75. }
  76. }
  77. if ( !preg_match( '/\[learn_press_profile\s?(.*)\]/', $post->post_content ) ) {
  78. $post->post_content .= '[learn_press_profile]';
  79. }
  80. } elseif ( $page_id == learn_press_get_page_id( 'cart' ) ) {
  81. if ( !preg_match( '/\[learn_press_cart\s?(.*)\]/', $post->post_content ) ) {
  82. $post->post_content .= '[learn_press_cart]';
  83. }
  84. } elseif ( $page_id == learn_press_get_page_id( 'become_a_teacher' ) ) {
  85. if ( !preg_match( '/\[learn_press_become_teacher_form\s?(.*)\]/', $post->post_content ) ) {
  86. $post->post_content .= '[learn_press_become_teacher_form]';
  87. }
  88. }
  89. }
  90. return $template;
  91. }
  92. /**
  93. * Checkout form
  94. *
  95. * @return string
  96. */
  97. static function checkout() {
  98. global $wp;
  99. ob_start();
  100. if ( isset( $wp->query_vars['lp-order-received'] ) ) {
  101. self::order_received( $wp->query_vars['lp-order-received'] );
  102. } else {
  103. // Check cart has contents
  104. if ( LP()->cart->is_empty() ) {
  105. learn_press_get_template( 'cart/empty-cart.php', array( 'checkout' => LP()->checkout() ) );
  106. } else {
  107. learn_press_get_template( 'checkout/form.php', array( 'checkout' => LP()->checkout() ) );
  108. }
  109. }
  110. return ob_get_clean();
  111. }
  112. private static function order_received( $order_id = 0 ) {
  113. learn_press_print_notices();
  114. $order = false;
  115. // Get the order
  116. $order_id = absint( $order_id );
  117. $order_key = !empty( $_GET['key'] ) ? $_GET['key'] : '';
  118. if ( $order_id > 0 && ( $order = learn_press_get_order( $order_id ) ) && $order->post->post_status != 'trash' ) {
  119. if ( $order->order_key != $order_key )
  120. unset( $order );
  121. } else {
  122. learn_press_display_message( __( 'Invalid order!', 'learnpress' ), 'error' );
  123. return;
  124. }
  125. LP()->session->order_awaiting_payment = null;
  126. learn_press_get_template( 'checkout/order-received.php', array( 'order' => $order ) );
  127. }
  128. static function cart() {
  129. ob_start();
  130. // Check cart has contents
  131. if ( LP()->cart->is_empty() ) {
  132. learn_press_get_template( 'cart/empty-cart.php', array( 'cart' => LP()->cart ) );
  133. } else {
  134. learn_press_get_template( 'cart/form.php', array( 'cart' => LP()->cart ) );
  135. }
  136. return ob_get_clean();
  137. }
  138. static function confirm_order( $atts = null ) {
  139. $atts = shortcode_atts(
  140. array(
  141. 'order_id' => !empty( $_REQUEST['order_id'] ) ? intval( $_REQUEST['order_id'] ) : 0
  142. ),
  143. $atts
  144. );
  145. $order_id = null;
  146. extract( $atts );
  147. ob_start();
  148. $order = learn_press_get_order( $order_id );
  149. if ( $order ) {
  150. learn_press_get_template( 'order/confirm.php', array( 'order' => $order ) );
  151. }
  152. return ob_get_clean();
  153. }
  154. /**
  155. * Display a form let the user can be join as a teacher
  156. */
  157. static function become_teacher_form( $atts ) {
  158. $user = learn_press_get_current_user();
  159. $message = '';
  160. $code = 0;
  161. if ( !is_user_logged_in() ) {
  162. $message = __( "Please login to fill out this form", 'learnpress' );
  163. $code = 1;
  164. } elseif ( in_array( LP()->teacher_role, $user->user->roles ) ) {
  165. $message = __( "You are a teacher now", 'learnpress' );
  166. $code = 2;
  167. } elseif ( get_transient( 'learn_press_become_teacher_sent_' . $user->id ) == 'yes' ) {
  168. $message = __( 'Your request has been sent! We will get in touch with you soon!', 'learnpress' );
  169. $code = 3;
  170. } elseif ( learn_press_user_maybe_is_a_teacher() ) {
  171. $message = __( 'Your role is allowed to create a course', 'learnpress' );
  172. $code = 4;
  173. }
  174. if ( !apply_filters( 'learn_press_become_a_teacher_display_form', true, $code, $message ) ) {
  175. return;
  176. }
  177. $atts = shortcode_atts(
  178. array(
  179. 'method' => 'post',
  180. 'action' => '',
  181. 'title' => __( 'Become a Teacher', 'learnpress' ),
  182. 'description' => __( 'Fill out your information and send to us to become a teacher', 'learnpress' ),
  183. 'submit_button_text' => __( 'Submit', 'learnpress' ),
  184. 'submit_button_process_text' => __( 'Processing', 'learnpress' )
  185. ),
  186. $atts
  187. );
  188. $fields = array(
  189. 'bat_name' => array(
  190. 'title' => __( 'Name', 'learnpress' ),
  191. 'type' => 'text',
  192. 'placeholder' => __( 'Your name', 'learnpress' ),
  193. 'def' => $user->display_name
  194. ),
  195. 'bat_email' => array(
  196. 'title' => __( 'Email', 'learnpress' ),
  197. 'type' => 'email',
  198. 'placeholder' => __( 'Your email address', 'learnpress' ),
  199. 'def' => $user->user_email
  200. ),
  201. 'bat_phone' => array(
  202. 'title' => __( 'Phone', 'learnpress' ),
  203. 'type' => 'text',
  204. 'placeholder' => __( 'Your phone number', 'learnpress' )
  205. )
  206. );
  207. $fields = apply_filters( 'learn_press_become_teacher_form_fields', $fields );
  208. ob_start();
  209. $args = array_merge(
  210. array(
  211. 'fields' => $fields,
  212. 'code' => $code,
  213. 'message' => $message
  214. ),
  215. $atts
  216. );
  217. learn_press_get_template( 'global/become-teacher-form.php', $args );
  218. $html = ob_get_clean();
  219. LP_Assets::enqueue_script( 'become-teacher' );
  220. return $html;
  221. }
  222. static function profile() {
  223. global $wp_query, $wp;
  224. if ( isset( $wp_query->query['user'] ) ) {
  225. $user = get_user_by( 'login', urldecode( $wp_query->query['user'] ) );
  226. } else {
  227. $user = get_user_by( 'id', get_current_user_id() );
  228. }
  229. $output = '';
  230. ob_start();
  231. if ( !$user ) {
  232. if ( empty( $wp_query->query['user'] ) ) {
  233. learn_press_get_template( 'profile/private-area.php' );
  234. } else {
  235. learn_press_display_message( sprintf( __( 'The user %s is not available!', 'learnpress' ), $wp_query->query['user'] ), 'error' );
  236. }
  237. } else {
  238. $user = LP_User::get_user( $user->ID );
  239. $tabs = learn_press_user_profile_tabs( $user );
  240. if ( !empty( $wp->query_vars['view'] ) ) {
  241. $current = $wp->query_vars['view'];
  242. } else {
  243. $tab_keys = array_keys( $tabs );
  244. $current = reset( $tab_keys );
  245. }
  246. $_REQUEST['tab'] = $current;
  247. $_POST['tab'] = $current;
  248. $_GET['tab'] = $current;
  249. if ( !learn_press_current_user_can_view_profile_section( $current, $user ) ) {
  250. learn_press_get_template( 'profile/private-area.php' );
  251. } else {
  252. if ( !empty( $tabs ) && !empty( $tabs[$current] ) ) :
  253. learn_press_get_template( 'profile/index.php',
  254. array(
  255. 'user' => $user,
  256. 'tabs' => $tabs,
  257. 'current' => $current
  258. )
  259. );
  260. else:
  261. if ( $wp->query_vars['view'] == LP()->settings->get( 'profile_endpoints.profile-order-details' ) ) {
  262. /*
  263. $current_user = wp_get_current_user();
  264. if ( $wp_query->query_vars['user'] != $current_user->user_login ) {
  265. learn_press_get_template( 'profile/private-area.php' );
  266. return;
  267. }*/
  268. $order_id = 0;
  269. if ( !empty( $wp->query_vars['id'] ) ) {
  270. $order_id = $wp->query_vars['id'];
  271. }
  272. $order = learn_press_get_order( $order_id );
  273. if ( !$order ) {
  274. learn_press_display_message( __( 'Invalid order!', 'learnpress' ), 'error' );
  275. } else {
  276. learn_press_get_template( 'profile/order-details.php',
  277. array(
  278. 'user' => $user,
  279. 'order' => $order
  280. )
  281. );
  282. }
  283. }
  284. endif;
  285. }
  286. }
  287. $output .= ob_get_clean();
  288. return $output;
  289. }
  290. }
  291. LP_Shortcodes::init();