initial
This commit is contained in:
+92
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Post_Custom_Field_Select;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Config\GF_Config;
|
||||
use Gravity_Forms\Gravity_Forms\Config\GF_Config_Data_Parser;
|
||||
use GFFormsModel;
|
||||
|
||||
defined( 'ABSPATH' ) || die();
|
||||
|
||||
/**
|
||||
* Config items for the Post Custom Field Select dropdown
|
||||
*
|
||||
* @since 2.9.20
|
||||
*/
|
||||
class GF_Post_Custom_Field_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 post custom field select dropdown
|
||||
*
|
||||
* @since 2.9.20
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function data() {
|
||||
return array(
|
||||
'components' => array(
|
||||
'post_custom_select' => array(
|
||||
'endpoints' => array(
|
||||
'get' => array(
|
||||
'action' => 'gf_get_custom_fields',
|
||||
'nonce' => wp_create_nonce( 'gf_get_custom_fields' ),
|
||||
),
|
||||
),
|
||||
'data' => $this->get_initial_custom_fields(),
|
||||
'strings' => array(
|
||||
'search_placeholder' => esc_html__( 'Search custom field names...', 'gravityforms' ),
|
||||
'trigger_placeholder' => esc_html__( 'Select a custom field name', 'gravityforms' ),
|
||||
'trigger_aria_text' => esc_html__( 'Default custom field name', 'gravityforms' ),
|
||||
'search_aria_text' => esc_html__( 'Search for custom field names', 'gravityforms' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the initial list of custom fields for the dropdown
|
||||
*
|
||||
* @since 2.9.20
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_initial_custom_fields() {
|
||||
|
||||
$results = GFFormsModel::get_custom_field_names( 10 );
|
||||
|
||||
foreach( $results as &$result ) {
|
||||
$result = array(
|
||||
'value' => $result,
|
||||
'label' => $result,
|
||||
);
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Post_Custom_Field_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_Post_Custom_Field_Select_Service_Provider
|
||||
*
|
||||
* Service provider for the Post Custom Field Select functionality
|
||||
*
|
||||
* @since 2.9.20
|
||||
*/
|
||||
class GF_Post_Custom_Field_Select_Service_Provider extends GF_Service_Provider {
|
||||
|
||||
const POST_CUSTOM_SELECT = 'post_custom_select';
|
||||
const POST_CUSTOM_SELECT_CONFIG = 'post_custom_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-post-custom-field-select.php' );
|
||||
require_once( plugin_dir_path( __FILE__ ) . 'class-gf-post-custom-field-select-config.php' );
|
||||
|
||||
$container->add( self::POST_CUSTOM_SELECT, function() {
|
||||
return new GF_Post_Custom_Field_Select();
|
||||
} );
|
||||
|
||||
$container->add( self::POST_CUSTOM_SELECT_CONFIG, function() use ( $container ) {
|
||||
$data_parser = $container->get( GF_Config_Service_Provider::DATA_PARSER );
|
||||
return new GF_Post_Custom_Field_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::POST_CUSTOM_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::POST_CUSTOM_SELECT_CONFIG ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Post_Custom_Field_Select;
|
||||
|
||||
defined( 'ABSPATH' ) || die();
|
||||
|
||||
use GFCommon;
|
||||
|
||||
/**
|
||||
* Class GF_Post_Custom_Field_Select
|
||||
*
|
||||
* Handles AJAX requests for the post custom field name select dropdown in the form editor
|
||||
*
|
||||
* @since 2.9.20
|
||||
*/
|
||||
class GF_Post_Custom_Field_Select {
|
||||
|
||||
/**
|
||||
* Initialize the post custom field select functionality
|
||||
*
|
||||
* @since 2.9.20
|
||||
*/
|
||||
public function init() {
|
||||
add_action( 'wp_ajax_gf_get_custom_fields', array( $this, 'ajax_custom_fields' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle AJAX request to get custom field names
|
||||
*
|
||||
* @since 2.9.20
|
||||
*/
|
||||
public function ajax_custom_fields() {
|
||||
if ( ! GFCommon::current_user_can_any( 'gravityforms_edit_forms' ) ) {
|
||||
wp_send_json_error( array( 'message' => __( 'Access denied', 'gravityforms' ) ) );
|
||||
}
|
||||
|
||||
check_ajax_referer( 'gf_get_custom_fields', 'nonce' );
|
||||
|
||||
$search = isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : '';
|
||||
|
||||
$custom_fields = $this->get_custom_field_names( $search );
|
||||
|
||||
$response = array();
|
||||
foreach ( $custom_fields as $field ) {
|
||||
$response[] = array(
|
||||
'value' => $field,
|
||||
'label' => $field,
|
||||
);
|
||||
}
|
||||
|
||||
wp_send_json_success( $response );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all unique custom field names from the postmeta table, optionally filtered by search.
|
||||
*
|
||||
* @since 2.9.20
|
||||
*
|
||||
* @param string $search custom field name search term
|
||||
* @return array
|
||||
*/
|
||||
private function get_custom_field_names( $search = '' ) {
|
||||
global $wpdb;
|
||||
|
||||
$not_like = '\_%';
|
||||
|
||||
if ( $search ) {
|
||||
$like = '%' . $wpdb->esc_like( $search ) . '%';
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
|
||||
$results = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT meta_key FROM {$wpdb->postmeta} WHERE meta_key NOT LIKE %s AND meta_key LIKE %s ORDER BY meta_key ASC LIMIT 10", $not_like, $like ) );
|
||||
} else {
|
||||
$results = wp_cache_get( 'gf_custom_fields_all', 'gf_custom_fields' );
|
||||
if ( false === $results ) {
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
|
||||
$results = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT meta_key FROM {$wpdb->postmeta} WHERE meta_key NOT LIKE %s ORDER BY meta_key ASC LIMIT 10", $not_like ) );
|
||||
wp_cache_set( 'gf_custom_fields_all', $results, 'gf_custom_fields', 300 );
|
||||
}
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user