initial commit
This commit is contained in:
@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'ACF_Ajax_Check_Screen' ) ) :
|
||||
|
||||
class ACF_Ajax_Check_Screen extends ACF_Ajax {
|
||||
|
||||
/** @var string The AJAX action name. */
|
||||
var $action = 'acf/ajax/check_screen';
|
||||
|
||||
/** @var bool Prevents access for non-logged in users. */
|
||||
var $public = false;
|
||||
|
||||
/**
|
||||
* get_response
|
||||
*
|
||||
* Returns the response data to sent back.
|
||||
*
|
||||
* @date 31/7/18
|
||||
* @since 5.7.2
|
||||
*
|
||||
* @param array $request The request args.
|
||||
* @return mixed The response data or WP_Error.
|
||||
*/
|
||||
function get_response( $request ) {
|
||||
|
||||
// vars
|
||||
$args = wp_parse_args(
|
||||
$this->request,
|
||||
array(
|
||||
'screen' => '',
|
||||
'post_id' => 0,
|
||||
'ajax' => true,
|
||||
'exists' => array(),
|
||||
)
|
||||
);
|
||||
|
||||
// vars
|
||||
$response = array(
|
||||
'results' => array(),
|
||||
'style' => '',
|
||||
);
|
||||
|
||||
// get field groups
|
||||
$field_groups = acf_get_field_groups( $args );
|
||||
|
||||
// loop through field groups
|
||||
if ( $field_groups ) {
|
||||
foreach ( $field_groups as $i => $field_group ) {
|
||||
|
||||
// vars
|
||||
$item = array(
|
||||
'id' => 'acf-' . $field_group['key'],
|
||||
'key' => $field_group['key'],
|
||||
'title' => $field_group['title'],
|
||||
'position' => $field_group['position'],
|
||||
'style' => $field_group['style'],
|
||||
'label' => $field_group['label_placement'],
|
||||
'edit' => acf_get_field_group_edit_link( $field_group['ID'] ),
|
||||
'html' => '',
|
||||
);
|
||||
|
||||
// append html if doesnt already exist on page
|
||||
if ( ! in_array( $field_group['key'], $args['exists'] ) ) {
|
||||
|
||||
// load fields
|
||||
$fields = acf_get_fields( $field_group );
|
||||
|
||||
// get field HTML
|
||||
ob_start();
|
||||
|
||||
// render
|
||||
acf_render_fields( $fields, $args['post_id'], 'div', $field_group['instruction_placement'] );
|
||||
|
||||
$item['html'] = ob_get_clean();
|
||||
}
|
||||
|
||||
// append
|
||||
$response['results'][] = $item;
|
||||
}
|
||||
|
||||
// Get style from first field group.
|
||||
$response['style'] = acf_get_field_group_style( $field_groups[0] );
|
||||
}
|
||||
|
||||
// Custom metabox order.
|
||||
if ( $this->get( 'screen' ) == 'post' ) {
|
||||
$response['sorted'] = get_user_option( 'meta-box-order_' . $this->get( 'post_type' ) );
|
||||
}
|
||||
|
||||
// return
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
acf_new_instance( 'ACF_Ajax_Check_Screen' );
|
||||
|
||||
endif; // class_exists check
|
||||
|
||||
|
@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'ACF_Ajax_Local_JSON_Diff' ) ) :
|
||||
|
||||
class ACF_Ajax_Local_JSON_Diff extends ACF_Ajax {
|
||||
|
||||
/** @var string The AJAX action name. */
|
||||
var $action = 'acf/ajax/local_json_diff';
|
||||
|
||||
/** @var bool Prevents access for non-logged in users. */
|
||||
var $public = false;
|
||||
|
||||
/**
|
||||
* get_response
|
||||
*
|
||||
* Returns the response data to sent back.
|
||||
*
|
||||
* @date 31/7/18
|
||||
* @since 5.7.2
|
||||
*
|
||||
* @param array $request The request args.
|
||||
* @return mixed The response data or WP_Error.
|
||||
*/
|
||||
function get_response( $request ) {
|
||||
$json = array();
|
||||
|
||||
// Extract props.
|
||||
$id = isset( $request['id'] ) ? intval( $request['id'] ) : 0;
|
||||
|
||||
// Bail ealry if missing props.
|
||||
if ( ! $id ) {
|
||||
return new WP_Error( 'acf_invalid_param', __( 'Invalid field group parameter(s).', 'acf' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
// Disable filters and load field group directly from database.
|
||||
acf_disable_filters();
|
||||
$field_group = acf_get_field_group( $id );
|
||||
if ( ! $field_group ) {
|
||||
return new WP_Error( 'acf_invalid_id', __( 'Invalid field group ID.', 'acf' ), array( 'status' => 404 ) );
|
||||
}
|
||||
$field_group['fields'] = acf_get_fields( $field_group );
|
||||
$field_group['modified'] = get_post_modified_time( 'U', true, $field_group['ID'] );
|
||||
$field_group = acf_prepare_field_group_for_export( $field_group );
|
||||
|
||||
// Load local field group file.
|
||||
$files = acf_get_local_json_files();
|
||||
$key = $field_group['key'];
|
||||
if ( ! isset( $files[ $key ] ) ) {
|
||||
return new WP_Error( 'acf_cannot_compare', __( 'Sorry, this field group is unavailable for diff comparison.', 'acf' ), array( 'status' => 404 ) );
|
||||
}
|
||||
$local_field_group = json_decode( file_get_contents( $files[ $key ] ), true );
|
||||
|
||||
// Render diff HTML.
|
||||
$date_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' );
|
||||
$date_template = __( 'Last updated: %s', 'acf' );
|
||||
$json['html'] = '
|
||||
<div class="acf-diff">
|
||||
<div class="acf-diff-title">
|
||||
<div class="acf-diff-title-left">
|
||||
<strong>' . __( 'Original field group', 'acf' ) . '</strong>
|
||||
<span>' . sprintf( $date_template, wp_date( $date_format, $field_group['modified'] ) ) . '</span>
|
||||
</div>
|
||||
<div class="acf-diff-title-right">
|
||||
<strong>' . __( 'JSON field group (newer)', 'acf' ) . '</strong>
|
||||
<span>' . sprintf( $date_template, wp_date( $date_format, $local_field_group['modified'] ) ) . '</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="acf-diff-content">
|
||||
' . wp_text_diff( acf_json_encode( $field_group ), acf_json_encode( $local_field_group ) ) . '
|
||||
</div>
|
||||
</div>';
|
||||
return $json;
|
||||
}
|
||||
}
|
||||
|
||||
acf_new_instance( 'ACF_Ajax_Local_JSON_Diff' );
|
||||
|
||||
endif; // class_exists check
|
@ -0,0 +1,275 @@
|
||||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'ACF_Ajax_Query_Users' ) ) :
|
||||
|
||||
class ACF_Ajax_Query_Users extends ACF_Ajax_Query {
|
||||
|
||||
/** @var string The AJAX action name. */
|
||||
var $action = 'acf/ajax/query_users';
|
||||
|
||||
/**
|
||||
* init_request
|
||||
*
|
||||
* Called at the beginning of a request to setup properties.
|
||||
*
|
||||
* @date 23/5/19
|
||||
* @since 5.8.1
|
||||
*
|
||||
* @param array $request The request args.
|
||||
* @return void
|
||||
*/
|
||||
function init_request( $request ) {
|
||||
parent::init_request( $request );
|
||||
|
||||
// Customize query.
|
||||
add_filter( 'user_search_columns', array( $this, 'filter_search_columns' ), 10, 3 );
|
||||
|
||||
/**
|
||||
* Fires when a request is made.
|
||||
*
|
||||
* @date 21/5/19
|
||||
* @since 5.8.1
|
||||
*
|
||||
* @param array $request The query request.
|
||||
* @param ACF_Ajax_Query $query The query object.
|
||||
*/
|
||||
do_action( 'acf/ajax/query_users/init', $request, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* get_args
|
||||
*
|
||||
* Returns an array of args for this query.
|
||||
*
|
||||
* @date 31/7/18
|
||||
* @since 5.7.2
|
||||
*
|
||||
* @param array $request The request args.
|
||||
* @return array
|
||||
*/
|
||||
function get_args( $request ) {
|
||||
$args = parent::get_args( $request );
|
||||
$args['number'] = $this->per_page;
|
||||
$args['paged'] = $this->page;
|
||||
if ( $this->is_search ) {
|
||||
$args['search'] = "*{$this->search}*";
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the query args.
|
||||
*
|
||||
* @date 21/5/19
|
||||
* @since 5.8.1
|
||||
*
|
||||
* @param array $args The query args.
|
||||
* @param array $request The query request.
|
||||
* @param ACF_Ajax_Query $query The query object.
|
||||
*/
|
||||
return apply_filters( 'acf/ajax/query_users/args', $args, $request, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares args for the get_results() method.
|
||||
*
|
||||
* @date 23/3/20
|
||||
* @since 5.8.9
|
||||
*
|
||||
* @param array args The query args.
|
||||
* @return array
|
||||
*/
|
||||
function prepare_args( $args ) {
|
||||
|
||||
// Parse pagination args that may have been modified.
|
||||
if ( isset( $args['users_per_page'] ) ) {
|
||||
$this->per_page = intval( $args['users_per_page'] );
|
||||
unset( $args['users_per_page'] );
|
||||
|
||||
} elseif ( isset( $args['number'] ) ) {
|
||||
$this->per_page = intval( $args['number'] );
|
||||
}
|
||||
|
||||
if ( isset( $args['paged'] ) ) {
|
||||
$this->page = intval( $args['paged'] );
|
||||
unset( $args['paged'] );
|
||||
}
|
||||
|
||||
// Set pagination args for fine control.
|
||||
$args['number'] = $this->per_page;
|
||||
$args['offset'] = $this->per_page * ( $this->page - 1 );
|
||||
$args['count_total'] = true;
|
||||
return $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* get_results
|
||||
*
|
||||
* Returns an array of results for the given args.
|
||||
*
|
||||
* @date 31/7/18
|
||||
* @since 5.7.2
|
||||
*
|
||||
* @param array args The query args.
|
||||
* @return array
|
||||
*/
|
||||
function get_results( $args ) {
|
||||
$results = array();
|
||||
|
||||
// Prepare args for quey.
|
||||
$args = $this->prepare_args( $args );
|
||||
|
||||
// Get result groups.
|
||||
if ( ! empty( $args['role__in'] ) ) {
|
||||
$roles = acf_get_user_role_labels( $args['role__in'] );
|
||||
} else {
|
||||
$roles = acf_get_user_role_labels();
|
||||
}
|
||||
|
||||
// Return a flat array of results when searching or when queriying one group only.
|
||||
if ( $this->is_search || count( $roles ) === 1 ) {
|
||||
|
||||
// Query users and append to results.
|
||||
$wp_user_query = new WP_User_Query( $args );
|
||||
$users = (array) $wp_user_query->get_results();
|
||||
$total_users = $wp_user_query->get_total();
|
||||
foreach ( $users as $user ) {
|
||||
$results[] = $this->get_result( $user );
|
||||
}
|
||||
|
||||
// Determine if more results exist.
|
||||
// As this query does not return grouped results, the calculation can be exact (">").
|
||||
$this->more = ( $total_users > count( $users ) + $args['offset'] );
|
||||
|
||||
// Otherwise, group results via role.
|
||||
} else {
|
||||
|
||||
// Unset args that will interfer with query results.
|
||||
unset( $args['role__in'], $args['role__not_in'] );
|
||||
|
||||
// Loop over each role.
|
||||
foreach ( $roles as $role => $role_label ) {
|
||||
|
||||
// Query users (for this role only).
|
||||
$args['role'] = $role;
|
||||
$wp_user_query = new WP_User_Query( $args );
|
||||
$users = (array) $wp_user_query->get_results();
|
||||
$total_users = $wp_user_query->get_total();
|
||||
|
||||
// acf_log( $args );
|
||||
// acf_log( '- ', count($users) );
|
||||
// acf_log( '- ', $total_users );
|
||||
|
||||
// If users were found for this query...
|
||||
if ( $users ) {
|
||||
|
||||
// Append optgroup of results.
|
||||
$role_results = array();
|
||||
foreach ( $users as $user ) {
|
||||
$role_results[] = $this->get_result( $user );
|
||||
}
|
||||
$results[] = array(
|
||||
'text' => $role_label,
|
||||
'children' => $role_results,
|
||||
);
|
||||
|
||||
// End loop when enough results have been found.
|
||||
if ( count( $users ) === $args['number'] ) {
|
||||
|
||||
// Determine if more results exist.
|
||||
// As this query does return grouped results, the calculation is best left fuzzy to avoid querying the next group (">=").
|
||||
$this->more = ( $total_users >= count( $users ) + $args['offset'] );
|
||||
break;
|
||||
|
||||
// Otherwise, modify the args so that the next query can continue on correctly.
|
||||
} else {
|
||||
$args['offset'] = 0;
|
||||
$args['number'] -= count( $users );
|
||||
}
|
||||
|
||||
// If no users were found (for the current pagination args), but there were users found for previous pages...
|
||||
// Modify the args so that the next query is offset slightly less (the number of total users) and can continue on correctly.
|
||||
} elseif ( $total_users ) {
|
||||
$args['offset'] -= $total_users;
|
||||
continue;
|
||||
|
||||
// Ignore roles that will never return a result.
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the query results.
|
||||
*
|
||||
* @date 21/5/19
|
||||
* @since 5.8.1
|
||||
*
|
||||
* @param array $results The query results.
|
||||
* @param array $args The query args.
|
||||
* @param ACF_Ajax_Query $query The query object.
|
||||
*/
|
||||
return apply_filters( 'acf/ajax/query_users/results', $results, $args, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* get_result
|
||||
*
|
||||
* Returns a single result for the given item object.
|
||||
*
|
||||
* @date 31/7/18
|
||||
* @since 5.7.2
|
||||
*
|
||||
* @param mixed $item A single item from the queried results.
|
||||
* @return string
|
||||
*/
|
||||
function get_result( $user ) {
|
||||
$item = acf_get_user_result( $user );
|
||||
|
||||
/**
|
||||
* Filters the result item.
|
||||
*
|
||||
* @date 21/5/19
|
||||
* @since 5.8.1
|
||||
*
|
||||
* @param array $item The choice id and text.
|
||||
* @param ACF_User $user The user object.
|
||||
* @param ACF_Ajax_Query $query The query object.
|
||||
*/
|
||||
return apply_filters( 'acf/ajax/query_users/result', $item, $user, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the WP_User_Query search columns.
|
||||
*
|
||||
* @date 9/3/20
|
||||
* @since 5.8.8
|
||||
*
|
||||
* @param array $columns An array of column names to be searched.
|
||||
* @param string $search The search term.
|
||||
* @param WP_User_Query $WP_User_Query The WP_User_Query instance.
|
||||
* @return array
|
||||
*/
|
||||
function filter_search_columns( $columns, $search, $WP_User_Query ) {
|
||||
|
||||
/**
|
||||
* Filters the column names to be searched.
|
||||
*
|
||||
* @date 21/5/19
|
||||
* @since 5.8.1
|
||||
*
|
||||
* @param array $columns An array of column names to be searched.
|
||||
* @param string $search The search term.
|
||||
* @param WP_User_Query $WP_User_Query The WP_User_Query instance.
|
||||
* @param ACF_Ajax_Query $query The query object.
|
||||
*/
|
||||
return apply_filters( 'acf/ajax/query_users/search_columns', $columns, $search, $WP_User_Query, $this );
|
||||
}
|
||||
}
|
||||
|
||||
acf_new_instance( 'ACF_Ajax_Query_Users' );
|
||||
|
||||
endif; // class_exists check
|
@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'ACF_Ajax_Query' ) ) :
|
||||
|
||||
class ACF_Ajax_Query extends ACF_Ajax {
|
||||
|
||||
/** @var bool Prevents access for non-logged in users. */
|
||||
var $public = true;
|
||||
|
||||
/** @var int The page of results to return. */
|
||||
var $page = 1;
|
||||
|
||||
/** @var int The number of results per page. */
|
||||
var $per_page = 20;
|
||||
|
||||
/** @var bool Signifies whether or not this AJAX query has more pages to load. */
|
||||
var $more = false;
|
||||
|
||||
/** @var string The searched term. */
|
||||
var $search = '';
|
||||
|
||||
/** @var bool Signifies whether the current query is a search. */
|
||||
var $is_search = false;
|
||||
|
||||
/** @var (int|string) The post_id being edited. */
|
||||
var $post_id = 0;
|
||||
|
||||
/** @var array The ACF field related to this query. */
|
||||
var $field = false;
|
||||
|
||||
/**
|
||||
* get_response
|
||||
*
|
||||
* Returns the response data to sent back.
|
||||
*
|
||||
* @date 31/7/18
|
||||
* @since 5.7.2
|
||||
*
|
||||
* @param array $request The request args.
|
||||
* @return (array|WP_Error) The response data or WP_Error.
|
||||
*/
|
||||
function get_response( $request ) {
|
||||
|
||||
// Init request.
|
||||
$this->init_request( $request );
|
||||
|
||||
// Get query args.
|
||||
$args = $this->get_args( $request );
|
||||
|
||||
// Get query results.
|
||||
$results = $this->get_results( $args );
|
||||
if ( is_wp_error( $results ) ) {
|
||||
return $results;
|
||||
}
|
||||
|
||||
// Return response.
|
||||
return array(
|
||||
'results' => $results,
|
||||
'more' => $this->more,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* init_request
|
||||
*
|
||||
* Called at the beginning of a request to setup properties.
|
||||
*
|
||||
* @date 23/5/19
|
||||
* @since 5.8.1
|
||||
*
|
||||
* @param array $request The request args.
|
||||
* @return void
|
||||
*/
|
||||
function init_request( $request ) {
|
||||
|
||||
// Get field for this query.
|
||||
if ( isset( $request['field_key'] ) ) {
|
||||
$this->field = acf_get_field( $request['field_key'] );
|
||||
}
|
||||
|
||||
// Update query properties.
|
||||
if ( isset( $request['page'] ) ) {
|
||||
$this->page = intval( $request['page'] );
|
||||
}
|
||||
if ( isset( $request['per_page'] ) ) {
|
||||
$this->per_page = intval( $request['per_page'] );
|
||||
}
|
||||
if ( isset( $request['search'] ) && acf_not_empty( $request['search'] ) ) {
|
||||
$this->search = sanitize_text_field( $request['search'] );
|
||||
$this->is_search = true;
|
||||
}
|
||||
if ( isset( $request['post_id'] ) ) {
|
||||
$this->post_id = $request['post_id'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* get_args
|
||||
*
|
||||
* Returns an array of args for this query.
|
||||
*
|
||||
* @date 31/7/18
|
||||
* @since 5.7.2
|
||||
*
|
||||
* @param array $request The request args.
|
||||
* @return array
|
||||
*/
|
||||
function get_args( $request ) {
|
||||
|
||||
// Allow for custom "query" arg.
|
||||
if ( isset( $request['query'] ) ) {
|
||||
return (array) $request['query'];
|
||||
}
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* get_items
|
||||
*
|
||||
* Returns an array of results for the given args.
|
||||
*
|
||||
* @date 31/7/18
|
||||
* @since 5.7.2
|
||||
*
|
||||
* @param array args The query args.
|
||||
* @return array
|
||||
*/
|
||||
function get_results( $args ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* get_item
|
||||
*
|
||||
* Returns a single result for the given item object.
|
||||
*
|
||||
* @date 31/7/18
|
||||
* @since 5.7.2
|
||||
*
|
||||
* @param mixed $item A single item from the queried results.
|
||||
* @return array An array containing "id" and "text".
|
||||
*/
|
||||
function get_result( $item ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
endif; // class_exists check
|
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'ACF_Ajax_Upgrade' ) ) :
|
||||
|
||||
class ACF_Ajax_Upgrade extends ACF_Ajax {
|
||||
|
||||
/** @var string The AJAX action name */
|
||||
var $action = 'acf/ajax/upgrade';
|
||||
|
||||
/**
|
||||
* get_response
|
||||
*
|
||||
* Returns the response data to sent back.
|
||||
*
|
||||
* @date 31/7/18
|
||||
* @since 5.7.2
|
||||
*
|
||||
* @param array $request The request args.
|
||||
* @return mixed The response data or WP_Error.
|
||||
*/
|
||||
function get_response( $request ) {
|
||||
|
||||
// Switch blog.
|
||||
if ( isset( $request['blog_id'] ) ) {
|
||||
switch_to_blog( $request['blog_id'] );
|
||||
}
|
||||
|
||||
// Bail early if no upgrade avaiable.
|
||||
if ( ! acf_has_upgrade() ) {
|
||||
return new WP_Error( 'upgrade_error', __( 'No updates available.', 'acf' ) );
|
||||
}
|
||||
|
||||
// Listen for output.
|
||||
ob_start();
|
||||
|
||||
// Run upgrades.
|
||||
acf_upgrade_all();
|
||||
|
||||
// Store output.
|
||||
$error = ob_get_clean();
|
||||
|
||||
// Return error or success.
|
||||
if ( $error ) {
|
||||
return new WP_Error( 'upgrade_error', $error );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
acf_new_instance( 'ACF_Ajax_Upgrade' );
|
||||
|
||||
endif; // class_exists check
|
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'ACF_Ajax_User_Setting' ) ) :
|
||||
|
||||
class ACF_Ajax_User_Setting extends ACF_Ajax {
|
||||
|
||||
/** @var string The AJAX action name. */
|
||||
var $action = 'acf/ajax/user_setting';
|
||||
|
||||
/** @var bool Prevents access for non-logged in users. */
|
||||
var $public = true;
|
||||
|
||||
/**
|
||||
* get_response
|
||||
*
|
||||
* Returns the response data to sent back.
|
||||
*
|
||||
* @date 31/7/18
|
||||
* @since 5.7.2
|
||||
*
|
||||
* @param array $request The request args.
|
||||
* @return mixed The response data or WP_Error.
|
||||
*/
|
||||
function get_response( $request ) {
|
||||
|
||||
// update
|
||||
if ( $this->has( 'value' ) ) {
|
||||
return acf_update_user_setting( $this->get( 'name' ), $this->get( 'value' ) );
|
||||
|
||||
// get
|
||||
} else {
|
||||
return acf_get_user_setting( $this->get( 'name' ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
acf_new_instance( 'ACF_Ajax_User_Setting' );
|
||||
|
||||
endif; // class_exists check
|
@ -0,0 +1,232 @@
|
||||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'ACF_Ajax' ) ) :
|
||||
|
||||
class ACF_Ajax {
|
||||
|
||||
/** @var string The AJAX action name. */
|
||||
var $action = '';
|
||||
|
||||
/** @var array The $_REQUEST data. */
|
||||
var $request;
|
||||
|
||||
/** @var bool Prevents access for non-logged in users. */
|
||||
var $public = false;
|
||||
|
||||
/**
|
||||
* __construct
|
||||
*
|
||||
* Sets up the class functionality.
|
||||
*
|
||||
* @date 31/7/18
|
||||
* @since 5.7.2
|
||||
*
|
||||
* @param void
|
||||
* @return void
|
||||
*/
|
||||
function __construct() {
|
||||
$this->initialize();
|
||||
$this->add_actions();
|
||||
}
|
||||
|
||||
/**
|
||||
* has
|
||||
*
|
||||
* Returns true if the request has data for the given key.
|
||||
*
|
||||
* @date 31/7/18
|
||||
* @since 5.7.2
|
||||
*
|
||||
* @param string $key The data key.
|
||||
* @return boolean
|
||||
*/
|
||||
function has( $key = '' ) {
|
||||
return isset( $this->request[ $key ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* get
|
||||
*
|
||||
* Returns request data for the given key.
|
||||
*
|
||||
* @date 31/7/18
|
||||
* @since 5.7.2
|
||||
*
|
||||
* @param string $key The data key.
|
||||
* @return mixed
|
||||
*/
|
||||
function get( $key = '' ) {
|
||||
return isset( $this->request[ $key ] ) ? $this->request[ $key ] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets request data for the given key.
|
||||
*
|
||||
* @date 31/7/18
|
||||
* @since 5.7.2
|
||||
*
|
||||
* @param string $key The data key.
|
||||
* @param mixed $value The data value.
|
||||
* @return ACF_Ajax
|
||||
*/
|
||||
function set( $key = '', $value = null ) {
|
||||
$this->request[ $key ] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* initialize
|
||||
*
|
||||
* Allows easy access to modifying properties without changing constructor.
|
||||
*
|
||||
* @date 31/7/18
|
||||
* @since 5.7.2
|
||||
*
|
||||
* @param void
|
||||
* @return void
|
||||
*/
|
||||
function initialize() {
|
||||
/* do nothing */
|
||||
}
|
||||
|
||||
/**
|
||||
* add_actions
|
||||
*
|
||||
* Adds the ajax actions for this response.
|
||||
*
|
||||
* @date 31/7/18
|
||||
* @since 5.7.2
|
||||
*
|
||||
* @param void
|
||||
* @return void
|
||||
*/
|
||||
function add_actions() {
|
||||
|
||||
// add action for logged-in users
|
||||
add_action( "wp_ajax_{$this->action}", array( $this, 'request' ) );
|
||||
|
||||
// add action for non logged-in users
|
||||
if ( $this->public ) {
|
||||
add_action( "wp_ajax_nopriv_{$this->action}", array( $this, 'request' ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* request
|
||||
*
|
||||
* Callback for ajax action. Sets up properties and calls the get_response() function.
|
||||
*
|
||||
* @date 1/8/18
|
||||
* @since 5.7.2
|
||||
*
|
||||
* @param void
|
||||
* @return void
|
||||
*/
|
||||
function request() {
|
||||
|
||||
// Store data for has() and get() functions.
|
||||
$this->request = wp_unslash( $_REQUEST );
|
||||
|
||||
// Verify request and handle error.
|
||||
$error = $this->verify_request( $this->request );
|
||||
if ( is_wp_error( $error ) ) {
|
||||
$this->send( $error );
|
||||
}
|
||||
|
||||
// Send response.
|
||||
$this->send( $this->get_response( $this->request ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies the request.
|
||||
*
|
||||
* @date 9/3/20
|
||||
* @since 5.8.8
|
||||
*
|
||||
* @param array $request The request args.
|
||||
* @return (bool|WP_Error) True on success, WP_Error on fail.
|
||||
*/
|
||||
function verify_request( $request ) {
|
||||
|
||||
// Verify nonce.
|
||||
if ( ! acf_verify_ajax() ) {
|
||||
return new WP_Error( 'acf_invalid_nonce', __( 'Invalid nonce.', 'acf' ), array( 'status' => 404 ) );
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* get_response
|
||||
*
|
||||
* Returns the response data to sent back.
|
||||
*
|
||||
* @date 31/7/18
|
||||
* @since 5.7.2
|
||||
*
|
||||
* @param array $request The request args.
|
||||
* @return mixed The response data or WP_Error.
|
||||
*/
|
||||
function get_response( $request ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* send
|
||||
*
|
||||
* Sends back JSON based on the $response as either success or failure.
|
||||
*
|
||||
* @date 31/7/18
|
||||
* @since 5.7.2
|
||||
*
|
||||
* @param mixed $response The response to send back.
|
||||
* @return void
|
||||
*/
|
||||
function send( $response ) {
|
||||
|
||||
// Return error.
|
||||
if ( is_wp_error( $response ) ) {
|
||||
$this->send_error( $response );
|
||||
|
||||
// Return success.
|
||||
} else {
|
||||
wp_send_json( $response );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a JSON response for the given WP_Error object.
|
||||
*
|
||||
* @date 8/3/20
|
||||
* @since 5.8.8
|
||||
*
|
||||
* @param WP_Error error The error object.
|
||||
* @return void
|
||||
*/
|
||||
function send_error( $error ) {
|
||||
|
||||
// Get error status
|
||||
$error_data = $error->get_error_data();
|
||||
if ( is_array( $error_data ) && isset( $error_data['status'] ) ) {
|
||||
$status_code = $error_data['status'];
|
||||
} else {
|
||||
$status_code = 500;
|
||||
}
|
||||
|
||||
wp_send_json(
|
||||
array(
|
||||
'code' => $error->get_error_code(),
|
||||
'message' => $error->get_error_message(),
|
||||
'data' => $error->get_error_data(),
|
||||
),
|
||||
$status_code
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
endif; // class_exists check
|
||||
|
||||
|
Reference in New Issue
Block a user