/wp-content/plugins/wp-job-manager/wp-job-manager-functions.php

https://github.com/TomZhangUT/ECEClubWordpress · PHP · 402 lines · 258 code · 62 blank · 82 comment · 38 complexity · 4f0054c917508df097eea39a8efc2013 MD5 · raw file

  1. <?php
  2. if ( ! function_exists( 'get_job_listings' ) ) :
  3. /**
  4. * Queries job listings with certain criteria and returns them
  5. *
  6. * @access public
  7. * @return void
  8. */
  9. function get_job_listings( $args = array() ) {
  10. global $wpdb;
  11. $args = wp_parse_args( $args, array(
  12. 'search_location' => '',
  13. 'search_keywords' => '',
  14. 'search_categories' => array(),
  15. 'job_types' => array(),
  16. 'offset' => '',
  17. 'posts_per_page' => '-1',
  18. 'orderby' => 'date',
  19. 'order' => 'DESC',
  20. 'featured' => null
  21. ) );
  22. $query_args = array(
  23. 'post_type' => 'job_listing',
  24. 'post_status' => 'publish',
  25. 'ignore_sticky_posts' => 1,
  26. 'offset' => absint( $args['offset'] ),
  27. 'posts_per_page' => intval( $args['posts_per_page'] ),
  28. 'orderby' => $args['orderby'],
  29. 'order' => $args['order'],
  30. 'tax_query' => array(),
  31. 'meta_query' => array()
  32. );
  33. if ( ! empty( $args['job_types'] ) ) {
  34. $query_args['tax_query'][] = array(
  35. 'taxonomy' => 'job_listing_type',
  36. 'field' => 'slug',
  37. 'terms' => $args['job_types']
  38. );
  39. }
  40. if ( ! empty( $args['search_categories'] ) ) {
  41. $field = is_numeric( $args['search_categories'][0] ) ? 'term_id' : 'slug';
  42. $query_args['tax_query'][] = array(
  43. 'taxonomy' => 'job_listing_category',
  44. 'field' => $field,
  45. 'terms' => $args['search_categories']
  46. );
  47. }
  48. if ( get_option( 'job_manager_hide_filled_positions' ) == 1 ) {
  49. $query_args['meta_query'][] = array(
  50. 'key' => '_filled',
  51. 'value' => '1',
  52. 'compare' => '!='
  53. );
  54. }
  55. if ( ! is_null( $args['featured'] ) ) {
  56. $query_args['meta_query'][] = array(
  57. 'key' => '_featured',
  58. 'value' => '1',
  59. 'compare' => $args['featured'] ? '=' : '!='
  60. );
  61. }
  62. // Location search - search geolocation data and location meta
  63. if ( $args['search_location'] ) {
  64. $location_post_ids = array_merge( $wpdb->get_col( $wpdb->prepare( "
  65. SELECT DISTINCT post_id FROM {$wpdb->postmeta}
  66. WHERE meta_key IN ( 'geolocation_city', 'geolocation_country_long', 'geolocation_country_short', 'geolocation_formatted_address', 'geolocation_state_long', 'geolocation_state_short', 'geolocation_street', 'geolocation_zipcode', '_job_location' )
  67. AND meta_value LIKE '%%%s%%'
  68. ", $args['search_location'] ) ), array( 0 ) );
  69. } else {
  70. $location_post_ids = array();
  71. }
  72. // Keyword search - search meta as well as post content
  73. if ( $args['search_keywords'] ) {
  74. $search_keywords = array_map( 'trim', explode( ',', $args['search_keywords'] ) );
  75. $posts_search_keywords_sql = array();
  76. $postmeta_search_keywords_sql = array();
  77. foreach ( $search_keywords as $keyword ) {
  78. $postmeta_search_keywords_sql[] = " meta_value LIKE '%" . esc_sql( $keyword ) . "%' ";
  79. $posts_search_keywords_sql[] = "
  80. post_title LIKE '%" . esc_sql( $keyword ) . "%'
  81. OR post_content LIKE '%" . esc_sql( $keyword ) . "%'
  82. ";
  83. }
  84. $keyword_post_ids = $wpdb->get_col( "
  85. SELECT DISTINCT post_id FROM {$wpdb->postmeta}
  86. WHERE " . implode( ' OR ', $postmeta_search_keywords_sql ) . "
  87. " );
  88. $keyword_post_ids = array_merge( $keyword_post_ids, $wpdb->get_col( "
  89. SELECT ID FROM {$wpdb->posts}
  90. WHERE ( " . implode( ' OR ', $posts_search_keywords_sql ) . " )
  91. AND post_type = 'job_listing'
  92. AND post_status = 'publish'
  93. " ), array( 0 ) );
  94. } else {
  95. $keyword_post_ids = array();
  96. }
  97. // Merge post ids
  98. if ( ! empty( $location_post_ids ) && ! empty( $keyword_post_ids ) ) {
  99. $query_args['post__in'] = array_intersect( $location_post_ids, $keyword_post_ids );
  100. } elseif ( ! empty( $location_post_ids ) || ! empty( $keyword_post_ids ) ) {
  101. $query_args['post__in'] = array_merge( $location_post_ids, $keyword_post_ids );
  102. }
  103. $query_args = apply_filters( 'job_manager_get_listings', $query_args, $args );
  104. if ( empty( $query_args['meta_query'] ) )
  105. unset( $query_args['meta_query'] );
  106. if ( empty( $query_args['tax_query'] ) )
  107. unset( $query_args['tax_query'] );
  108. if ( $args['orderby'] == 'featured' ) {
  109. $query_args['orderby'] = 'meta_key';
  110. $query_args['meta_key'] = '_featured';
  111. add_filter( 'posts_clauses', 'order_featured_job_listing' );
  112. }
  113. // Filter args
  114. $query_args = apply_filters( 'get_job_listings_query_args', $query_args, $args );
  115. do_action( 'before_get_job_listings', $query_args, $args );
  116. $result = new WP_Query( $query_args );
  117. do_action( 'after_get_job_listings', $query_args, $args );
  118. remove_filter( 'posts_clauses', 'order_featured_job_listing' );
  119. return $result;
  120. }
  121. endif;
  122. if ( ! function_exists( 'get_job_listing_post_statuses' ) ) :
  123. /**
  124. * Get post statuses used for jobs
  125. *
  126. * @access public
  127. * @return array
  128. */
  129. function get_job_listing_post_statuses() {
  130. return apply_filters( 'job_listing_post_statuses', array(
  131. 'draft' => _x( 'Draft', 'post status', 'wp-job-manager' ),
  132. 'expired' => _x( 'Expired', 'post status', 'wp-job-manager' ),
  133. 'preview' => _x( 'Preview', 'post status', 'wp-job-manager' ),
  134. 'pending' => _x( 'Pending approval', 'post status', 'wp-job-manager' ),
  135. 'pending_payment' => _x( 'Pending payment', 'post status', 'wp-job-manager' ),
  136. 'publish' => _x( 'Active', 'post status', 'wp-job-manager' ),
  137. ) );
  138. }
  139. endif;
  140. if ( ! function_exists( 'order_featured_job_listing' ) ) :
  141. /**
  142. * WP Core doens't let us change the sort direction for invidual orderby params - http://core.trac.wordpress.org/ticket/17065
  143. *
  144. * @access public
  145. * @param array $args
  146. * @return array
  147. */
  148. function order_featured_job_listing( $args ) {
  149. global $wpdb;
  150. $args['orderby'] = "$wpdb->postmeta.meta_value+0 DESC, $wpdb->posts.post_date DESC";
  151. return $args;
  152. }
  153. endif;
  154. if ( ! function_exists( 'get_featured_job_ids' ) ) :
  155. /**
  156. * Gets the ids of featured jobs.
  157. *
  158. * @access public
  159. * @return array
  160. */
  161. function get_featured_job_ids() {
  162. return get_posts( array(
  163. 'posts_per_page' => -1,
  164. 'post_type' => 'job_listing',
  165. 'post_status' => 'publish',
  166. 'meta_key' => '_featured',
  167. 'meta_value' => '1',
  168. 'fields' => 'ids'
  169. ) );
  170. }
  171. endif;
  172. if ( ! function_exists( 'get_job_listing_types' ) ) :
  173. /**
  174. * Get job listing types
  175. *
  176. * @access public
  177. * @return array
  178. */
  179. function get_job_listing_types( $fields = 'all' ) {
  180. return get_terms( "job_listing_type", array(
  181. 'orderby' => 'name',
  182. 'order' => 'ASC',
  183. 'hide_empty' => false,
  184. 'fields' => $fields
  185. ) );
  186. }
  187. endif;
  188. if ( ! function_exists( 'get_job_listing_categories' ) ) :
  189. /**
  190. * Get job categories
  191. *
  192. * @access public
  193. * @return array
  194. */
  195. function get_job_listing_categories() {
  196. if ( ! get_option( 'job_manager_enable_categories' ) ) {
  197. return array();
  198. }
  199. return get_terms( "job_listing_category", array(
  200. 'orderby' => 'name',
  201. 'order' => 'ASC',
  202. 'hide_empty' => false,
  203. ) );
  204. }
  205. endif;
  206. if ( ! function_exists( 'job_manager_get_filtered_links' ) ) :
  207. /**
  208. * Shows links after filtering jobs
  209. */
  210. function job_manager_get_filtered_links( $args = array() ) {
  211. $links = apply_filters( 'job_manager_job_filters_showing_jobs_links', array(
  212. 'reset' => array(
  213. 'name' => __( 'Reset', 'wp-job-manager' ),
  214. 'url' => '#'
  215. ),
  216. 'rss_link' => array(
  217. 'name' => __( 'RSS', 'wp-job-manager' ),
  218. 'url' => get_job_listing_rss_link( apply_filters( 'job_manager_get_listings_custom_filter_rss_args', array(
  219. 'type' => isset( $args['filter_job_types'] ) ? implode( ',', $args['filter_job_types'] ) : '',
  220. 'location' => $args['search_location'],
  221. 'job_categories' => implode( ',', $args['search_categories'] ),
  222. 's' => $args['search_keywords'],
  223. ) ) )
  224. )
  225. ), $args );
  226. $return = '';
  227. foreach ( $links as $key => $link ) {
  228. $return .= '<a href="' . esc_url( $link['url'] ) . '" class="' . esc_attr( $key ) . '">' . $link['name'] . '</a>';
  229. }
  230. return $return;
  231. }
  232. endif;
  233. if ( ! function_exists( 'get_job_listing_rss_link' ) ) :
  234. /**
  235. * Get the Job Listing RSS link
  236. *
  237. * @return string
  238. */
  239. function get_job_listing_rss_link( $args = array() ) {
  240. $rss_link = add_query_arg( array_merge( array( 'feed' => 'job_feed' ), $args ), home_url() );
  241. return $rss_link;
  242. }
  243. endif;
  244. if ( ! function_exists( 'job_manager_create_account' ) ) :
  245. /**
  246. * Handle account creation.
  247. *
  248. * @param string $account_email
  249. * @param string $role
  250. * @return WP_error | bool was an account created?
  251. */
  252. function wp_job_manager_create_account( $account_email, $role = '' ) {
  253. global $current_user;
  254. $user_email = apply_filters( 'user_registration_email', sanitize_email( $account_email ) );
  255. if ( empty( $user_email ) )
  256. return false;
  257. if ( ! is_email( $user_email ) )
  258. return new WP_Error( 'validation-error', __( 'Your email address isn&#8217;t correct.', 'wp-job-manager' ) );
  259. if ( email_exists( $user_email ) )
  260. return new WP_Error( 'validation-error', __( 'This email is already registered, please choose another one.', 'wp-job-manager' ) );
  261. // Email is good to go - use it to create a user name
  262. $username = sanitize_user( current( explode( '@', $user_email ) ) );
  263. $password = wp_generate_password();
  264. // Ensure username is unique
  265. $append = 1;
  266. $o_username = $username;
  267. while( username_exists( $username ) ) {
  268. $username = $o_username . $append;
  269. $append ++;
  270. }
  271. // Final error check
  272. $reg_errors = new WP_Error();
  273. do_action( 'job_manager_register_post', $username, $user_email, $reg_errors );
  274. $reg_errors = apply_filters( 'job_manager_registration_errors', $reg_errors, $username, $user_email );
  275. if ( $reg_errors->get_error_code() ) {
  276. return $reg_errors;
  277. }
  278. // Create account
  279. $new_user = array(
  280. 'user_login' => $username,
  281. 'user_pass' => $password,
  282. 'user_email' => $user_email,
  283. 'role' => $role ? $role : get_option( 'default_role' )
  284. );
  285. $user_id = wp_insert_user( apply_filters( 'job_manager_create_account_data', $new_user ) );
  286. if ( is_wp_error( $user_id ) )
  287. return $user_id;
  288. // Notify
  289. wp_new_user_notification( $user_id, $password );
  290. // Login
  291. wp_set_auth_cookie( $user_id, true, is_ssl() );
  292. $current_user = get_user_by( 'id', $user_id );
  293. return true;
  294. }
  295. endif;
  296. /**
  297. * True if an the user can post a job. If accounts are required, and reg is enabled, users can post (they signup at the same time).
  298. *
  299. * @return bool
  300. */
  301. function job_manager_user_can_post_job() {
  302. $can_post = true;
  303. if ( ! is_user_logged_in() ) {
  304. if ( job_manager_user_requires_account() && ! job_manager_enable_registration() ) {
  305. $can_post = false;
  306. }
  307. }
  308. return apply_filters( 'job_manager_user_can_post_job', $can_post );
  309. }
  310. /**
  311. * True if an the user can edit a job.
  312. *
  313. * @return bool
  314. */
  315. function job_manager_user_can_edit_job( $job_id ) {
  316. $can_edit = true;
  317. $job = get_post( $job_id );
  318. if ( ! is_user_logged_in() ) {
  319. $can_edit = false;
  320. } elseif ( $job->post_author != get_current_user_id() ) {
  321. $can_edit = false;
  322. }
  323. return apply_filters( 'job_manager_user_can_edit_job', $can_edit, $job_id );
  324. }
  325. /**
  326. * True if registration is enabled.
  327. *
  328. * @return bool
  329. */
  330. function job_manager_enable_registration() {
  331. return apply_filters( 'job_manager_enable_registration', get_option( 'job_manager_enable_registration' ) == 1 ? true : false );
  332. }
  333. /**
  334. * True if an account is required to post a job.
  335. *
  336. * @return bool
  337. */
  338. function job_manager_user_requires_account() {
  339. return apply_filters( 'job_manager_user_requires_account', get_option( 'job_manager_user_requires_account' ) == 1 ? true : false );
  340. }