2017-07-26 14:57:52 +03:00
< ? php
namespace um\core ;
2023-06-26 21:29:41 +03:00
if ( ! defined ( 'ABSPATH' ) ) {
exit ;
}
2017-07-26 14:57:52 +03:00
2018-03-26 01:27:46 +03:00
if ( ! class_exists ( 'um\core\Query' ) ) {
2017-07-26 14:57:52 +03:00
2018-03-20 13:24:38 +02:00
/**
* Class Query
* @package um\core
*/
class Query {
/**
* @var array
*/
public $wp_pages = array ();
/**
* @var array
*/
public $roles = array ();
/**
* Query constructor.
*/
2021-06-29 02:51:54 +03:00
public function __construct () {
2018-03-20 13:24:38 +02:00
}
/**
* Ajax pagination for posts
*/
2021-06-29 02:51:54 +03:00
public function ajax_paginate () {
2018-11-21 14:01:18 +02:00
UM () -> check_ajax_nonce ();
2023-06-22 13:19:09 +03:00
// phpcs:disable WordPress.Security.NonceVerification
2023-06-26 17:16:41 +03:00
if ( ! isset ( $_REQUEST [ 'hook' ] ) ) {
wp_send_json_error ( __ ( 'Invalid hook.' , 'ultimate-member' ) );
2023-06-22 13:19:09 +03:00
}
2023-06-26 17:16:41 +03:00
$hook = sanitize_key ( $_REQUEST [ 'hook' ] );
$args = ! empty ( $_REQUEST [ 'args' ] ) ? $_REQUEST [ 'args' ] : array ();
2023-06-22 13:19:09 +03:00
// phpcs:enable WordPress.Security.NonceVerification
2018-03-20 13:24:38 +02:00
ob_start ();
/**
2023-06-26 17:16:41 +03:00
* Fires on posts loading by AJAX in User Profile tabs.
2018-03-20 13:24:38 +02:00
*
2023-06-26 17:16:41 +03:00
* @since 1.3.x
2023-06-22 13:19:09 +03:00
* @hook um_ajax_load_posts__{$hook}
*
* @param {array} $args Request.
*
2023-06-26 17:16:41 +03:00
* @example <caption>Make any custom action on when posts loading by AJAX in User Profile.</caption>
2018-03-20 13:24:38 +02:00
* function my_ajax_load_posts( $args ) {
* // your code here
* }
2023-06-22 13:19:09 +03:00
* add_action( 'um_ajax_load_posts__{$hook}', 'my_ajax_load_posts', 10, 1 );
2018-03-20 13:24:38 +02:00
*/
do_action ( " um_ajax_load_posts__ { $hook } " , $args );
2018-09-28 17:14:50 +03:00
$output = ob_get_clean ();
2023-06-26 17:16:41 +03:00
// @todo: investigate using WP_KSES
2018-03-20 13:24:38 +02:00
die ( $output );
}
/**
* Get wp pages
*
* @return array|string
*/
2023-06-26 21:29:41 +03:00
public function wp_pages () {
2018-03-20 13:24:38 +02:00
global $wpdb ;
if ( isset ( $this -> wp_pages ) && ! empty ( $this -> wp_pages ) ){
return $this -> wp_pages ;
}
$count_pages = wp_count_posts ( 'page' );
if ( $count_pages -> publish > 300 ){
return 'reached_maximum_limit' ;
}
2019-03-12 21:28:29 +02:00
$pages = $wpdb -> get_results (
2023-06-22 13:19:09 +03:00
" SELECT *
FROM { $wpdb -> posts }
WHERE post_type = 'page' AND
2025-02-03 16:17:37 +02:00
post_status = 'publish' "
2019-03-12 21:28:29 +02:00
);
2018-03-20 13:24:38 +02:00
$array = array ();
2025-02-03 16:17:37 +02:00
if ( $wpdb -> num_rows > 0 ) {
foreach ( $pages as $page_data ) {
2018-03-20 13:24:38 +02:00
$array [ $page_data -> ID ] = $page_data -> post_title ;
}
}
$this -> wp_pages = $array ;
return $array ;
}
/**
* Get all forms
*
* @return mixed
*/
2023-06-26 21:29:41 +03:00
public function forms () {
2018-03-20 13:24:38 +02:00
$results = array ();
$args = array (
'post_type' => 'um_form' ,
'posts_per_page' => 200 ,
'post_status' => array ( 'publish' )
);
$query = new \WP_Query ( $args );
foreach ( $query -> posts as $post ) {
setup_postdata ( $post );
$results [ $post -> ID ] = $post -> post_title ;
}
return $results ;
}
/**
* Do custom queries
*
2023-06-26 21:29:41 +03:00
* @param array $args
2018-03-20 13:24:38 +02:00
*
* @return array|bool|int|\WP_Query
*/
2023-06-22 16:11:32 +03:00
public function make ( $args ) {
2018-03-20 13:24:38 +02:00
$defaults = array (
2023-06-22 16:11:32 +03:00
'post_type' => 'post' ,
'post_status' => array ( 'publish' ),
2018-03-20 13:24:38 +02:00
);
2023-06-22 16:11:32 +03:00
$args = wp_parse_args ( $args , $defaults );
2018-03-20 13:24:38 +02:00
2023-06-22 16:11:32 +03:00
if ( isset ( $args [ 'post__in' ] ) && empty ( $args [ 'post__in' ] ) ) {
2018-03-20 13:24:38 +02:00
return false ;
2023-06-22 16:11:32 +03:00
}
2018-03-20 13:24:38 +02:00
2023-06-26 21:29:41 +03:00
if ( 'comment' === $args [ 'post_type' ] ) {
// Comments query.
2018-03-20 13:24:38 +02:00
unset ( $args [ 'post_type' ] );
/**
2023-06-26 21:29:41 +03:00
* Filters excluded comment types.
2018-03-20 13:24:38 +02:00
*
2023-06-26 21:29:41 +03:00
* @since 1.3.x
2023-06-22 16:11:32 +03:00
* @hook um_excluded_comment_types
*
2023-06-26 21:29:41 +03:00
* @param {array} $types Comment Types.
2023-06-22 16:11:32 +03:00
*
2023-06-26 21:29:41 +03:00
* @return {array} Comment Types.
2023-06-22 16:11:32 +03:00
*
* @example <caption>Extend excluded comment types.</caption>
* function my_excluded_comment_types( $types ) {
2018-03-20 13:24:38 +02:00
* // your code here
* return $types;
* }
2023-06-26 21:29:41 +03:00
* add_filter( 'um_excluded_comment_types', 'my_excluded_comment_types' );
2018-03-20 13:24:38 +02:00
*/
2023-06-22 16:11:32 +03:00
$args [ 'type__not_in' ] = apply_filters ( 'um_excluded_comment_types' , array ( '' ) );
2018-03-20 13:24:38 +02:00
2023-06-26 21:29:41 +03:00
return get_comments ( $args );
}
2018-03-20 13:24:38 +02:00
2023-06-26 21:29:41 +03:00
$custom_posts = new \WP_Query ();
$args [ 'post_status' ] = is_array ( $args [ 'post_status' ] ) ? $args [ 'post_status' ] : explode ( ',' , $args [ 'post_status' ] );
2018-03-20 13:24:38 +02:00
2023-06-26 21:29:41 +03:00
$custom_posts -> query ( $args );
2018-03-20 13:24:38 +02:00
2023-06-26 21:29:41 +03:00
return $custom_posts ;
2018-03-20 13:24:38 +02:00
}
/**
* Get last users
*
* @param int $number
*
* @return array
*/
2023-06-26 21:29:41 +03:00
function get_recent_users ( $number = 5 ) {
2018-03-20 13:24:38 +02:00
$args = array ( 'fields' => 'ID' , 'number' => $number , 'orderby' => 'user_registered' , 'order' => 'desc' );
$users = new \WP_User_Query ( $args );
return $users -> results ;
}
/**
* Count users by status
*
2022-06-20 17:29:31 +03:00
* @since 2.4.2 $status = 'unassigned' is unused. Please use `UM()->setup()->set_default_user_status()` instead. Will be deprecated since 3.0
*
2018-03-20 13:24:38 +02:00
* @param $status
*
* @return int
*/
function count_users_by_status ( $status ) {
2022-06-20 17:29:31 +03:00
if ( 'unassigned' === $status ) {
_deprecated_argument (
__FUNCTION__ ,
'2.4.2' ,
__ ( 'The "unassigned" $status has been removed. Use `UM()->setup()->set_default_user_status()` for setting up default user account status.' , 'ultimate-member' )
);
UM () -> setup () -> set_default_user_status ();
return 0 ;
}
$users_count = get_transient ( " um_count_users_ { $status } " );
if ( false === $users_count ) {
$args = array (
'fields' => 'ids' ,
'number' => 1 ,
'meta_query' => array (
array (
'key' => 'account_status' ,
'value' => $status ,
'compare' => '=' ,
),
),
'um_custom_user_query' => true ,
);
2018-03-20 13:24:38 +02:00
$users = new \WP_User_Query ( $args );
2022-06-20 17:29:31 +03:00
if ( empty ( $users ) || is_wp_error ( $users ) ) {
$users_count = 0 ;
} else {
$users_count = $users -> get_total ();
2018-03-20 13:24:38 +02:00
}
2022-06-20 17:29:31 +03:00
set_transient ( " um_count_users_ { $status } " , $users_count );
2018-03-20 13:24:38 +02:00
}
2022-06-20 17:29:31 +03:00
return $users_count ;
2018-03-20 13:24:38 +02:00
}
/**
2022-06-20 17:29:31 +03:00
* Get pending users (in queue)
2018-03-20 13:24:38 +02:00
*
2022-06-20 17:29:31 +03:00
* @return int
2018-03-20 13:24:38 +02:00
*/
2022-06-20 17:29:31 +03:00
function get_pending_users_count () {
2022-08-12 23:07:16 +03:00
$users_count = get_transient ( 'um_count_users_pending_dot' );
2022-06-20 17:29:31 +03:00
if ( false === $users_count ) {
$args = array (
'fields' => 'ids' ,
'number' => 1 ,
'meta_query' => array (
'relation' => 'OR' ,
array (
'key' => 'account_status' ,
'value' => 'awaiting_email_confirmation' ,
'compare' => '=' ,
),
array (
'key' => 'account_status' ,
'value' => 'awaiting_admin_review' ,
'compare' => '=' ,
),
),
'um_custom_user_query' => true ,
);
2018-03-20 13:24:38 +02:00
2022-06-20 17:29:31 +03:00
/**
* UM hook
*
* @type filter
* @title um_admin_pending_queue_filter
* @description Change user query arguments when get pending users
* @input_vars
* [{"var":"$args","type":"array","desc":"WP_Users query arguments"}]
* @change_log
* ["Since: 2.0"]
* @usage
* <?php add_filter( 'um_admin_pending_queue_filter', 'function_name', 10, 1 ); ?>
* @example
* <?php
* add_filter( 'um_admin_pending_queue_filter', 'my_admin_pending_queue', 10, 1 );
* function my_admin_pending_queue( $args ) {
* // your code here
* return $args;
* }
* ?>
*/
$args = apply_filters ( 'um_admin_pending_queue_filter' , $args );
2018-03-20 13:24:38 +02:00
2022-06-20 17:29:31 +03:00
$users = new \WP_User_Query ( $args );
if ( empty ( $users ) || is_wp_error ( $users ) ) {
$users_count = 0 ;
} else {
$users_count = $users -> get_total ();
}
2022-08-12 23:07:16 +03:00
set_transient ( 'um_count_users_pending_dot' , $users_count );
2022-06-20 17:29:31 +03:00
}
return $users_count ;
2018-03-20 13:24:38 +02:00
}
/**
* Count all users
*
2024-09-24 17:58:55 +03:00
* @param bool $force Avoid transient. Default false.
*
* @return int
2018-03-20 13:24:38 +02:00
*/
2024-09-24 17:58:55 +03:00
public function count_users ( $force = false ) {
$users_count = get_transient ( 'um_count_users_all' );
if ( $force || false === $users_count ) {
$result = count_users ();
2018-03-20 13:24:38 +02:00
2024-09-24 17:58:55 +03:00
$users_count = $result [ 'total_users' ];
set_transient ( 'um_count_users_all' , $users_count , HOUR_IN_SECONDS );
}
return $users_count ;
}
2018-03-20 13:24:38 +02:00
/**
* Using wpdb instead of update_post_meta
*
* @param $key
* @param $post_id
* @param $new_value
*/
2022-06-08 21:24:23 +03:00
function update_attr ( $key , $post_id , $new_value ) {
/**
* Post meta values are passed through the stripslashes() function upon being stored.
* Function wp_slash() is added to compensate for the call to stripslashes().
* @see https://developer.wordpress.org/reference/functions/update_post_meta/
*/
if ( is_array ( $new_value ) ) {
foreach ( $new_value as $k => $val ) {
if ( is_array ( $val ) && array_key_exists ( 'custom_dropdown_options_source' , $val ) ) {
$new_value [ $k ][ 'custom_dropdown_options_source' ] = wp_slash ( $val [ 'custom_dropdown_options_source' ] );
}
}
}
2018-03-20 13:24:38 +02:00
update_post_meta ( $post_id , '_um_' . $key , $new_value );
}
/**
2023-06-22 17:06:01 +03:00
* Get postmeta related to Ultimate Member.
2018-03-20 13:24:38 +02:00
*
2023-06-22 17:06:01 +03:00
* @param string $key
* @param int $post_id
2018-03-20 13:24:38 +02:00
*
* @return mixed
*/
2023-06-22 17:06:01 +03:00
public function get_attr ( $key , $post_id ) {
return get_post_meta ( $post_id , '_um_' . $key , true );
2018-03-20 13:24:38 +02:00
}
/**
* Delete data
*
* @param $key
* @param $post_id
*
* @return bool
*/
function delete_attr ( $key , $post_id ) {
$meta = delete_post_meta ( $post_id , '_um_' . $key );
return $meta ;
}
/**
* Checks if post has a specific meta key
*
* @param $key
* @param null $value
* @param null $post_id
*
* @return bool
*/
function has_post_meta ( $key , $value = null , $post_id = null ) {
if ( ! $post_id ) {
global $post ;
$post_id = $post -> ID ;
}
if ( $value ) {
if ( get_post_meta ( $post_id , $key , true ) == $value ) {
return true ;
}
} else {
if ( get_post_meta ( $post_id , $key , true ) ) {
return true ;
}
}
return false ;
}
/**
* Get posts with specific meta key/value
*
* @param $post_type
* @param $key
* @param $value
*
* @return bool
*/
function find_post_id ( $post_type , $key , $value ) {
$posts = get_posts ( array ( 'post_type' => $post_type , 'meta_key' => $key , 'meta_value' => $value ) );
if ( isset ( $posts [ 0 ] ) && ! empty ( $posts ) )
return $posts [ 0 ] -> ID ;
return false ;
}
/**
* Get post data
*
2023-12-06 17:55:20 +02:00
* @param int $post_id
2018-03-20 13:24:38 +02:00
*
2023-12-06 17:55:20 +02:00
* @return array
2018-03-20 13:24:38 +02:00
*/
2023-12-06 17:55:20 +02:00
public function post_data ( $post_id ) {
2018-03-20 13:24:38 +02:00
$array [ 'form_id' ] = $post_id ;
2023-12-06 17:55:20 +02:00
$mode = $this -> get_attr ( 'mode' , $post_id );
$meta = get_post_custom ( $post_id );
foreach ( $meta as $k => $v ) {
if ( strstr ( $k , '_um_' . $mode . '_' ) ) {
$k = str_replace ( '_um_' . $mode . '_' , '' , $k );
$array [ $k ] = $v [ 0 ];
} elseif ( '_um_mode' === $k ) {
$k = str_replace ( '_um_' , '' , $k );
$array [ $k ] = $v [ 0 ];
} elseif ( strstr ( $k , '_um_' ) ) {
$k = str_replace ( '_um_' , '' , $k );
$array [ $k ] = $v [ 0 ];
2018-03-20 13:24:38 +02:00
}
}
2023-12-06 17:55:20 +02:00
foreach ( $array as $k => $v ) {
if ( strstr ( $k , 'login_' ) || strstr ( $k , 'register_' ) || strstr ( $k , 'profile_' ) ) {
if ( 'directory' !== $mode ) {
unset ( $array [ $k ] );
2018-03-20 13:24:38 +02:00
}
}
}
return $array ;
}
/**
* Capture selected value
*
2019-08-20 16:21:23 +03:00
* @param string $key
* @param string|null $array_key
* @param bool $fallback
2018-03-20 13:24:38 +02:00
* @return int|mixed|null|string
*/
2019-08-09 14:56:27 +03:00
function get_meta_value ( $key , $array_key = null , $fallback = false ) {
2018-03-20 13:24:38 +02:00
$post_id = get_the_ID ();
$try = get_post_meta ( $post_id , $key , true );
2019-08-20 16:21:23 +03:00
//old-old version if ( ! empty( $try ) )
//old version if ( $try !== false )
if ( $try != '' ) {
2018-03-20 13:24:38 +02:00
if ( is_array ( $try ) && in_array ( $array_key , $try ) ) {
return $array_key ;
} else if ( is_array ( $try ) ) {
return '' ;
} else {
return $try ;
}
2019-08-20 16:21:23 +03:00
}
2018-03-20 13:24:38 +02:00
if ( $fallback == 'na' ) {
$fallback = 0 ;
$none = '' ;
} else {
$none = 0 ;
}
return ! empty ( $fallback ) ? $fallback : $none ;
}
/**
* Checks if its a core page of UM
*
* @param $post_id
*
* @return bool|mixed
*/
function is_core ( $post_id ){
$is_core = get_post_meta ( $post_id , '_um_core' , true );
if ( $is_core != '' ) {
return $is_core ;
} else {
return false ;
}
}
2022-06-20 17:29:31 +03:00
/**
* Get users by status
*
* @param $status
* @param int $number
*
* @deprecated 2.4.2
*
* @return array
*/
function get_users_by_status ( $status , $number = 5 ) {
_deprecated_function ( __METHOD__ , '2.4.2' );
$args = array ( 'fields' => 'ID' , 'number' => $number , 'orderby' => 'user_registered' , 'order' => 'desc' );
$args [ 'meta_query' ][] = array (
array (
'key' => 'account_status' ,
'value' => $status ,
'compare' => '='
)
);
$users = new \WP_User_Query ( $args );
return $users -> results ;
}
2018-03-20 13:24:38 +02:00
}
2017-07-26 14:57:52 +03:00
}