initial
This commit is contained in:
+120
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Author_Select;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Config\GF_Config;
|
||||
use Gravity_Forms\Gravity_Forms\Config\GF_Config_Data_Parser;
|
||||
|
||||
defined( 'ABSPATH' ) || die();
|
||||
|
||||
/**
|
||||
* Config items for the Author Select dropdown
|
||||
*
|
||||
* @since 2.9.20
|
||||
*/
|
||||
class GF_Author_Select_Config extends GF_Config {
|
||||
|
||||
protected $name = 'gform_admin_config';
|
||||
protected $script_to_localize = 'gform_gravityforms_admin_vendors';
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @since 2.9.20
|
||||
*
|
||||
* @param GF_Config_Data_Parser $data_parser
|
||||
*/
|
||||
public function __construct( GF_Config_Data_Parser $data_parser ) {
|
||||
parent::__construct( $data_parser );
|
||||
}
|
||||
|
||||
/**
|
||||
* Only enqueue in the form editor
|
||||
*
|
||||
* @since 2.9.20
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function should_enqueue() {
|
||||
return \GFForms::get_page() === 'form_editor';
|
||||
}
|
||||
|
||||
/**
|
||||
* Config data for the author select dropdown
|
||||
*
|
||||
* @since 2.9.20
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function data() {
|
||||
$form_id = isset( $_GET['id'] ) ? intval( $_GET['id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
||||
|
||||
$form = \GFFormsModel::get_form_meta( $form_id );
|
||||
$current_author = rgar( $form, 'postAuthor' );
|
||||
$selected_label = '';
|
||||
|
||||
if ( $current_author ) {
|
||||
$current_user = get_user_by( 'id', $current_author );
|
||||
if ( $current_user ) {
|
||||
$selected_label = esc_html( $current_user->display_name );
|
||||
}
|
||||
}
|
||||
|
||||
return array(
|
||||
'components' => array(
|
||||
'author_select' => array(
|
||||
'endpoints' => array(
|
||||
'get' => array(
|
||||
'action' => 'gf_get_users',
|
||||
'nonce' => wp_create_nonce( 'gf_get_users' ),
|
||||
),
|
||||
),
|
||||
'data' => $this->get_initial_users( $form_id ),
|
||||
'form_id' => $form_id,
|
||||
'selected_value' => $current_author,
|
||||
'selected_label' => $selected_label,
|
||||
'strings' => array(
|
||||
'search_placeholder' => esc_html__( 'Search users...', 'gravityforms' ),
|
||||
'trigger_placeholder' => esc_html__( 'Select an author', 'gravityforms' ),
|
||||
'trigger_aria_text' => esc_html__( 'Default Post Author', 'gravityforms' ),
|
||||
'search_aria_text' => esc_html__( 'Search for users', 'gravityforms' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the initial list of users for the dropdown
|
||||
*
|
||||
* @since 2.9.20
|
||||
*
|
||||
* @param int $form_id The form ID
|
||||
* @return array
|
||||
*/
|
||||
private function get_initial_users( $form_id ) {
|
||||
$args = array(
|
||||
'number' => 10,
|
||||
'fields' => array( 'ID', 'display_name' ),
|
||||
);
|
||||
|
||||
// Apply existing filter for backward compatibility
|
||||
$args = gf_apply_filters( array( 'gform_author_dropdown_args', $form_id ), $args );
|
||||
|
||||
$users = get_users( $args );
|
||||
|
||||
if ( ! is_array( $users ) || empty( $users ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$initial_users = array();
|
||||
foreach ( $users as $user ) {
|
||||
$initial_users[] = array(
|
||||
'value' => $user->ID,
|
||||
'label' => esc_html( $user->display_name ),
|
||||
);
|
||||
}
|
||||
|
||||
return $initial_users;
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Author_Select;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Config\GF_Config_Service_Provider;
|
||||
use Gravity_Forms\Gravity_Forms\GF_Service_Container;
|
||||
use Gravity_Forms\Gravity_Forms\GF_Service_Provider;
|
||||
|
||||
defined( 'ABSPATH' ) || die();
|
||||
|
||||
/**
|
||||
* Class GF_Author_Select_Service_Provider
|
||||
*
|
||||
* Service provider for the Author Select functionality
|
||||
*
|
||||
* @since 2.9.20
|
||||
*/
|
||||
class GF_Author_Select_Service_Provider extends GF_Service_Provider {
|
||||
|
||||
const AUTHOR_SELECT = 'author_select';
|
||||
const AUTHOR_SELECT_CONFIG = 'author_select_config';
|
||||
|
||||
/**
|
||||
* Register services to the container
|
||||
*
|
||||
* @since 2.9.20
|
||||
*
|
||||
* @param GF_Service_Container $container
|
||||
*/
|
||||
public function register( GF_Service_Container $container ) {
|
||||
require_once( plugin_dir_path( __FILE__ ) . 'class-gf-author-select.php' );
|
||||
require_once( plugin_dir_path( __FILE__ ) . 'class-gf-author-select-config.php' );
|
||||
|
||||
$container->add( self::AUTHOR_SELECT, function() {
|
||||
return new GF_Author_Select();
|
||||
} );
|
||||
|
||||
$container->add( self::AUTHOR_SELECT_CONFIG, function() use ( $container ) {
|
||||
$data_parser = $container->get( GF_Config_Service_Provider::DATA_PARSER );
|
||||
return new GF_Author_Select_Config( $data_parser );
|
||||
} );
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize any actions or hooks
|
||||
*
|
||||
* @since 2.9.20
|
||||
*
|
||||
* @param GF_Service_Container $container
|
||||
*/
|
||||
public function init( GF_Service_Container $container ) {
|
||||
$container->get( self::AUTHOR_SELECT )->init();
|
||||
|
||||
if ( \GFForms::get_page() === 'form_editor' ) {
|
||||
$config_collection = $container->get( GF_Config_Service_Provider::CONFIG_COLLECTION );
|
||||
$config_collection->add_config( $container->get( self::AUTHOR_SELECT_CONFIG ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Author_Select;
|
||||
|
||||
defined( 'ABSPATH' ) || die();
|
||||
|
||||
use GFCommon;
|
||||
|
||||
/**
|
||||
* Class GF_Author_Select
|
||||
*
|
||||
* Handles AJAX requests for the author select dropdown in the form editor
|
||||
*
|
||||
* @since 2.9.20
|
||||
*/
|
||||
class GF_Author_Select {
|
||||
|
||||
/**
|
||||
* Initialize the author select functionality
|
||||
*
|
||||
* @since 2.9.20
|
||||
*/
|
||||
public function init() {
|
||||
add_action( 'wp_ajax_gf_get_users', array( $this, 'ajax_get_users' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle AJAX request to get users
|
||||
*
|
||||
* @since 2.9.20
|
||||
*/
|
||||
public function ajax_get_users() {
|
||||
if ( ! GFCommon::current_user_can_any( 'gravityforms_edit_forms' ) ) {
|
||||
wp_send_json_error( array( 'message' => __( 'Access denied', 'gravityforms' ) ) );
|
||||
}
|
||||
|
||||
check_ajax_referer( 'gf_get_users', 'nonce' );
|
||||
|
||||
$search = isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : '';
|
||||
$form_id = isset( $_POST['form_id'] ) ? intval( $_POST['form_id'] ) : 0;
|
||||
|
||||
$args = array(
|
||||
'number' => 10,
|
||||
'fields' => array( 'ID', 'display_name' ),
|
||||
);
|
||||
|
||||
/**
|
||||
* Filter the arguments used to query users for the author dropdown.
|
||||
* Originally added to limit users for performance.
|
||||
* Now with AJAX and 10-result limit by default, useful for role restrictions or custom filtering.
|
||||
*
|
||||
* @since 1.3.10
|
||||
*
|
||||
* @param array $args WP_User_Query arguments for get_users()
|
||||
*/
|
||||
$args = gf_apply_filters( array( 'gform_author_dropdown_args', $form_id ), $args );
|
||||
|
||||
if ( ! empty( $search ) ) {
|
||||
$args['search'] = '*' . $search . '*';
|
||||
$args['search_columns'] = array( 'user_login', 'user_email', 'display_name' );
|
||||
}
|
||||
|
||||
$users = get_users( $args );
|
||||
|
||||
$response = array();
|
||||
foreach ( $users as $user ) {
|
||||
$response[] = array(
|
||||
'value' => $user->ID,
|
||||
'label' => esc_html( $user->display_name ),
|
||||
);
|
||||
}
|
||||
|
||||
wp_send_json_success( $response );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user