PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/opentickets-community-edition/inc/core/my-account-takeover.class.php

https://gitlab.com/leobelizquierdo/cabotsubmitter-wordpress
PHP | 376 lines | 308 code | 55 blank | 13 comment | 52 complexity | 62996ed8b69b90ddfb22f6a740ced56f MD5 | raw file
  1. <?php if ( __FILE__ == $_SERVER['SCRIPT_FILENAME'] ) die( header( 'Location: /') );
  2. class qsot_my_account_takeover {
  3. protected static $options = array();
  4. protected static $o = array();
  5. public static function pre_init() {
  6. $settings_class_name = apply_filters('qsot-settings-class-name', '');
  7. if (!empty($settings_class_name)) {
  8. self::$o = call_user_func_array(array($settings_class_name, "instance"), array());
  9. // load all the options, and share them with all other parts of the plugin
  10. $options_class_name = apply_filters('qsot-options-class-name', '');
  11. if (!empty($options_class_name)) {
  12. self::$options = call_user_func_array(array($options_class_name, "instance"), array());
  13. self::_setup_admin_options();
  14. }
  15. add_action('woocommerce_before_my_account', array(__CLASS__, 'draw_upcoming_event_tickets_list'), 10);
  16. add_action('edit_user_profile', array(__CLASS__, 'add_my_account_to_user_profile'), 4, 1);
  17. add_action('show_user_profile', array(__CLASS__, 'add_my_account_to_user_profile'), 4, 1);
  18. add_action('woocommerce_init', array(__CLASS__, 'override_shortcodes'), 10001);
  19. add_action('woocommerce_my_account_my_orders_values', array(__CLASS__, 'my_orders_values'), 10, 2);
  20. add_action('woocommerce_my_account_my_orders_headers', array(__CLASS__, 'my_orders_headers'), 10, 2);
  21. // allow users to be logged in indefinitely, more or less
  22. if (self::$options->{'qsot-infinite-login'} == 'yes') {
  23. //add_action('login_init', array(__CLASS__, 'long_test_cookie'), PHP_INT_MAX);
  24. add_filter('auth_cookie_expiration', array(__CLASS__, 'long_login_expire'), PHP_INT_MAX, 3);
  25. add_filter('auth_cookie_expire_time', array(__CLASS__, 'long_login_expire'), PHP_INT_MAX, 4);
  26. add_filter('wc_session_expiring', array(__CLASS__, 'long_login_expiring'), PHP_INT_MAX, 3);
  27. add_filter('wc_session_expiration', array(__CLASS__, 'long_login_expire'), PHP_INT_MAX, 3);
  28. add_filter('init', array(__CLASS__, 'extend_login_expiration'), -1);
  29. }
  30. }
  31. }
  32. public static function debug($name) { die(__log($name)); }
  33. public static function my_orders_headers($user, $orders) {
  34. if (!is_admin()) return;
  35. echo '<th>'.__('Shows','opentickets-community-edition').'</th>';
  36. }
  37. public static function my_orders_values($user, $order) {
  38. if (!is_admin()) return;
  39. $shows = array();
  40. foreach ($order->get_items() as $item) {
  41. unset($item['item_meta']);
  42. if (is_array($item) && isset($item['event_id'])) {
  43. $event = apply_filters('qsot-get-event', false, $item['event_id']);
  44. if (is_object($event)) {
  45. $shows[] = $event->post_title;
  46. }
  47. }
  48. }
  49. $shows = array_unique($shows);
  50. ?>
  51. <td>
  52. <?php if (count($shows)): ?>
  53. <?php echo implode('<br/>', $shows) ?>
  54. <?php else: ?>
  55. <?php echo '&nbsp;'.__('(none)','opentickets-community-edition'); ?>
  56. <?php endif; ?>
  57. </td>
  58. <?php
  59. }
  60. public static function long_login_expire($length, $user_id=0, $remember='', $from_expiration=0) {
  61. return $from_expiration ? $from_expiration : 31536000;
  62. }
  63. public static function long_login_expiring($length, $user_id=0, $remember='') {
  64. return 31449600;
  65. }
  66. public static function long_test_cookie() {
  67. setcookie(TEST_COOKIE, 'WP Cookie check', apply_filters('auth_cookie_expiration', 0), COOKIEPATH, COOKIE_DOMAIN);
  68. if ( SITECOOKIEPATH != COOKIEPATH )
  69. setcookie(TEST_COOKIE, 'WP Cookie check', apply_filters('auth_cookie_expiration', 0), SITECOOKIEPATH, COOKIE_DOMAIN);
  70. }
  71. public static function extend_login_expiration() {
  72. $user = wp_get_current_user();
  73. if (!empty($user->ID)) {
  74. wp_set_auth_cookie($user->ID);
  75. self::long_test_cookie();
  76. }
  77. }
  78. public static function override_shortcodes() {
  79. remove_shortcode('woocommerce_view_order');
  80. add_shortcode( 'woocommerce_view_order', array( __CLASS__, 'view_order_shortcode' ) );
  81. }
  82. public static function view_order_shortcode($atts) {
  83. return WC()->shortcode_wrapper( array( __CLASS__, 'view_order_shortcode_output' ), $atts );
  84. }
  85. public static function view_order_shortcode_output($atts) {
  86. if ( ! is_user_logged_in() ) return;
  87. extract( shortcode_atts( array(
  88. 'order_count' => 10
  89. ), $atts ) );
  90. $user_id = get_current_user_id();
  91. $order_id = ( isset( $_GET['order'] ) ) ? $_GET['order'] : 0;
  92. $order = new WC_Order( $order_id );
  93. if ( $order_id == 0 ) {
  94. wc_get_template( 'myaccount/my-orders.php', array( 'order_count' => 'all' == $order_count ? -1 : $order_count ) );
  95. return;
  96. }
  97. if ( !current_user_can('delete_users') && $order->user_id != $user_id ) {
  98. echo '<div class="woocommerce-error">' . __( 'Invalid order.', 'woocommerce' ) . ' <a href="'.get_permalink( wc_get_page_id('myaccount') ).'">'. __( 'My Account &rarr;','opentickets-community-edition') .'</a>' . '</div>';
  99. return;
  100. }
  101. if (is_callable(array(&$order, 'get_status'))) {
  102. $status = $order->get_status();
  103. } else {
  104. $status = get_term_by('slug', $order->status, 'shop_order_status');
  105. }
  106. echo '<p class="order-info">'
  107. . sprintf( __('Order <mark class="order-number">%s</mark> made on <mark class="order-date">%s</mark>','opentickets-community-edition'), $order->get_order_number(), date_i18n( get_option( 'date_format' ), strtotime( $order->order_date ) ) )
  108. . '. ' . sprintf( __('Order status: <mark class="order-status">%s</mark>','opentickets-community-edition'), __($status->name,'opentickets-community-edition') )
  109. . '.</p>';
  110. $notes = $order->get_customer_order_notes();
  111. if ($notes) :
  112. ?>
  113. <h2><?php _e('Order Updates','opentickets-community-edition'); ?></h2>
  114. <ol class="commentlist notes">
  115. <?php foreach ($notes as $note) : ?>
  116. <li class="comment note">
  117. <div class="comment_container">
  118. <div class="comment-text">
  119. <p class="meta"><?php echo date_i18n(__( 'l jS \of F Y, h:ia','opentickets-community-edition'), strtotime($note->comment_date)); ?></p>
  120. <div class="description">
  121. <?php echo wpautop(wptexturize($note->comment_content)); ?>
  122. </div>
  123. <div class="clear"></div>
  124. </div>
  125. <div class="clear"></div>
  126. </div>
  127. </li>
  128. <?php endforeach; ?>
  129. </ol>
  130. <?php
  131. endif;
  132. do_action( 'woocommerce_view_order', $order_id );
  133. }
  134. // add the upcoming events section to the user profile, both on the frontend and backend
  135. public static function add_my_account_to_user_profile( $userprofile ) {
  136. // grab a WC instance
  137. $woocommerce = WC();
  138. // first make sure to load all the required files are included
  139. $woocommerce->frontend_includes();
  140. $pp = $woocommerce->plugin_path();
  141. include_once( $pp . '/includes/abstracts/abstract-wc-session.php' );
  142. include_once( $pp . '/includes/class-wc-session-handler.php' );
  143. // next, setup the session, if it is not arlready setup (mainly for the backend profile pages)
  144. $session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' );
  145. $woocommerce->session = isset( $woocommerce->session ) && $woocommerce->session instanceof $session_class ? $woocommerce->session : new $session_class();
  146. // setup the customer information for the profile page
  147. if ( ! is_object( $woocommerce->customer ) )
  148. $woocommerce->customer = new WC_Customer();
  149. // if the user is not logged in, then force them to before we continue
  150. if ( ! is_user_logged_in() ) {
  151. wc_get_template( 'myaccount/form-login.php' );
  152. } else {
  153. // find all the completed orders for that user
  154. query_posts( array(
  155. 'numberposts' => -1,
  156. 'meta_key' => '_customer_user',
  157. 'meta_value' => $userprofile->ID,
  158. 'post_type' => wc_get_order_types( 'view-orders' ),
  159. 'post_status' => array_keys( wc_get_order_statuses() )
  160. ) );
  161. // and if there are no posts, then bail, because there will definitely be nothing to display
  162. if ( have_posts() )
  163. the_post();
  164. // hack it up here.
  165. // basically, because this part of the template is not designed to show in the admin, we have to fool core WC into thinking that the displayed user is possibly someone other than the current user
  166. $cu = wp_get_current_user();
  167. $GLOBALS['qsot_my_acct'] = array(
  168. 'current_user' => $cu,
  169. 'can_edit_orders' => current_user_can('edit_shop_orders'),
  170. );
  171. $GLOBALS['current_user'] = $userprofile;
  172. $cu2 = wp_get_current_user();
  173. $GLOBALS['qsot_my_acct']['swapin_user'] = $cu2;
  174. ?><div class="my-account"><?php
  175. wc_get_template( 'myaccount/my-account.php', array(
  176. 'current_user' => $cu2,
  177. 'order_count' => -1,
  178. ) );
  179. ?></div><?php
  180. $GLOBALS['current_user'] = $cu;
  181. wp_get_current_user();
  182. }
  183. }
  184. public static function draw_upcoming_event_tickets_list($current_user) {
  185. global $wpdb;
  186. $orders = get_posts(array(
  187. 'posts_per_page' => -1,
  188. 'meta_key' => '_customer_user',
  189. 'meta_value' => is_object($current_user) && isset($current_user->ID) ? $current_user->ID : get_current_user_id(),
  190. 'post_type' => 'shop_order',
  191. 'post_status' => 'any',
  192. 'fields' => 'ids',
  193. ));
  194. if (!is_array($orders) || empty($orders)) return;
  195. $orders = array_map('absint', $orders);
  196. $q = 'select distinct order_item_id from '.$wpdb->base_prefix.'woocommerce_order_items where order_id in ('.implode(',', $orders).')';
  197. $order_item_ids = $wpdb->get_col($q);
  198. if (!is_array($order_item_ids) || empty($order_item_ids)) return;
  199. $order_item_ids = array_map('absint', $order_item_ids);
  200. $q = $wpdb->prepare(
  201. 'select order_item_id, meta_value from '.$wpdb->base_prefix.'woocommerce_order_itemmeta where order_item_id in ('.implode(',', $order_item_ids).') and meta_key = %s',
  202. '_event_id'
  203. );
  204. $pairs = $wpdb->get_results($q);
  205. if (!is_array($pairs) || empty($pairs)) return;
  206. $groups = array();
  207. foreach ($pairs as $pair) {
  208. $event_id = $pair->meta_value;
  209. $oiid = $pair->order_item_id;
  210. if (!isset($groups["{$event_id}"]) || !is_array($groups["{$event_id}"])) $groups["{$event_id}"] = array();
  211. $groups["{$event_id}"][] = $oiid;
  212. }
  213. $events = get_posts(array(
  214. 'posts_per_page' => -1,
  215. 'fields' => 'ids',
  216. 'suppress_filters' => false,
  217. 'post_status' => current_user_can( 'read_private_posts' ) ? array('publish', 'hidden', 'private') : array( 'publish' ),
  218. 'post_type' => self::$o->core_post_type,
  219. 'post__in' => array_keys($groups),
  220. 'meta_query' => array(
  221. array(
  222. 'key' => self::$o->{'meta_key.start'},
  223. 'value' => date('Y-m-d H:i:s'),
  224. 'type' => 'DATETIME',
  225. 'compare' => '>=',
  226. ),
  227. ),
  228. 'meta_key' => self::$o->{'meta_key.start'},
  229. 'orderby' => 'meta_value_date',
  230. 'order' => 'asc',
  231. ));
  232. if (!is_array($events) || empty($events)) return;
  233. $events = array_map('absint', $events);
  234. $ticket_ids = array();
  235. foreach ($events as $eid)
  236. if (isset($groups["{$eid}"]))
  237. $ticket_ids = array_merge($ticket_ids, $groups["{$eid}"]);
  238. $ticket_ids = array_unique($ticket_ids);
  239. $q = 'select * from '.$wpdb->base_prefix.'woocommerce_order_itemmeta where order_item_id in ('.implode(',', $ticket_ids).')';
  240. $raw_data = $wpdb->get_results($q);
  241. $q = 'select order_id, order_item_id from '.$wpdb->base_prefix.'woocommerce_order_items where order_item_id in ('.implode(',', $ticket_ids).')';
  242. $raw_pairs = $wpdb->get_results($q);
  243. $pairs = array();
  244. foreach ($raw_pairs as $raw_row) $pairs[$raw_row->order_item_id.''] = $raw_row->order_id;
  245. $e_data = $event_data = $ticket_data = array();
  246. foreach ($raw_data as $row) {
  247. if (!isset($ticket_data["{$row->order_item_id}"]) || !is_array($ticket_data["{$row->order_item_id}"]))
  248. $ticket_data["{$row->order_item_id}"] = array('__order_item_id' => $row->order_item_id, '__order_id' => isset($pairs[$row->order_item_id]) ? $pairs[$row->order_item_id] : 0);
  249. $ticket_data["{$row->order_item_id}"][$row->meta_key] = $row->meta_value;
  250. }
  251. foreach ($ticket_data as $ind => $ticket) {
  252. $ticket = (object)wp_parse_args($ticket, array(
  253. '_ticket_code' => '',
  254. '_ticket_link' => '',
  255. '_product_id' => 0,
  256. '_event_id' => 0,
  257. '__order_id' => 0,
  258. ));
  259. $ticket->permalink = apply_filters('qsot-get-ticket-link', '', $ticket->__order_item_id);
  260. $ticket->product = get_product($ticket->_product_id);
  261. $ticket->event = apply_filters('qsot-event-add-meta', get_post($ticket->_event_id));
  262. $ticket_data[$ind] = $ticket;
  263. if (is_object($ticket->event) && (!isset($e_data["{$ticket->_event_id}"]) || !is_object($e_data["{$ticket->_event_id}"])))
  264. $e_data["{$ticket->_event_id}"] = $ticket->event;
  265. if (!isset($e_data["{$ticket->_event_id}"]->tickets) || !is_array($e_data["{$ticket->_event_id}"]->tickets))
  266. $e_data["{$ticket->_event_id}"]->tickets = array();
  267. $e_data["{$ticket->_event_id}"]->tickets[] = $ticket;
  268. }
  269. foreach ($events as $eid) if (isset($e_data[$eid.''])) $event_data[$eid.''] = $e_data[$eid.''];
  270. wc_get_template('myaccount/my-upcoming-tickets.php', array(
  271. 'user' => $current_user,
  272. 'tickets' => $ticket_data,
  273. 'by_event' => $event_data,
  274. 'display_format' => self::$options->{'qsot-my-account-display-upcoming-tickets'},
  275. ));
  276. }
  277. protected static function _setup_admin_options() {
  278. self::$options->def('qsot-my-account-display-upcoming-tickets', 'by_event');
  279. self::$options->def('qsot-infinite-login', 'yes');
  280. self::$options->add(array(
  281. 'order' => 1000,
  282. 'type' => 'title',
  283. 'title' => __('My Account Page','opentickets-community-edition'),
  284. 'id' => 'heading-frontend-my-account-1',
  285. 'page' => 'frontend',
  286. 'section' => 'my-account',
  287. ));
  288. self::$options->add(array(
  289. 'order' => 1010,
  290. 'id' => 'qsot-my-account-display-upcoming-tickets',
  291. 'type' => 'radio',
  292. 'title' => __('Display Upcoming Tickets','opentickets-community-edition'),
  293. 'desc_tip' => __('Format to display the upcoming tickets list in. The list appears on the end user\'s "My Account" page.','opentickets-community-edition'),
  294. 'options' => array(
  295. 'by_event' => __('By Event','opentickets-community-edition'),
  296. 'as_list' => __('As Line Item List','opentickets-community-edition'),
  297. ),
  298. 'default' => 'by_event',
  299. 'page' => 'frontend',
  300. 'section' => 'my-account',
  301. ));
  302. self::$options->add(array(
  303. 'order' => 1030,
  304. 'type' => 'sectionend',
  305. 'id' => 'heading-frontend-my-account-1',
  306. 'page' => 'frontend',
  307. 'section' => 'my-account',
  308. ));
  309. self::$options->add(array(
  310. 'order' => 115,
  311. 'id' => 'qsot-infinite-login',
  312. 'type' => 'checkbox',
  313. 'title' => __('Infinite Login','opentickets-community-edition'),
  314. 'desc' => __('Once a user logs in, they stay logged in, forever.','opentickets-community-edition'),
  315. 'default' => 'yes',
  316. ));
  317. }
  318. }
  319. if (defined('ABSPATH') && function_exists('add_action')) {
  320. qsot_my_account_takeover::pre_init();
  321. }