'pfbc-select pfbc-post-select'); /** * Constructor * * @param string $label The field label. * @param string $name The field name attribute. * @param string $post_type Post type to query (default: 'page'). * @param string $extra_args Extra query arguments in query string format. * @param array|null $properties Optional element properties. */ public function __construct($label, $name, $post_type = 'page', $extra_args = '', ?array $properties = null) { // Initialize with empty options - will be loaded via AJAX parent::__construct($label, $name, array(), $properties); $this->post_type = $post_type; $this->extra_args = $extra_args; $this->ajax_url = admin_url('admin-ajax.php'); $this->nonce = wp_create_nonce('accua_forms_get_posts'); // Add data attributes for JavaScript $this->attributes['data-post-type'] = $this->post_type; $this->attributes['data-ajax-url'] = $this->ajax_url; $this->attributes['data-nonce'] = $this->nonce; if (!empty($this->extra_args)) { $this->attributes['data-extra-args'] = $this->extra_args; } } /** * Render the post select element * * Outputs a native select that will be enhanced by JavaScript for search/AJAX. * Falls back to a working select if JavaScript is disabled (with initial options). */ public function render() { $this->applyAriaAttributes(); // Get current value $value = ''; if (isset($this->attributes['value'])) { $value = is_array($this->attributes['value']) ? reset($this->attributes['value']) : $this->attributes['value']; } // Store selected value for AJAX to include in first page if (!empty($value)) { $this->attributes['data-selected'] = $value; } // Generate unique ID for this element (access attributes array directly) $id = isset($this->attributes['id']) ? $this->attributes['id'] : ''; if (empty($id)) { $id = 'pfbc-post-select-' . uniqid(); $this->attributes['id'] = $id; } // Start rendering echo '
'; // Native select element (will be hidden when JS enhances it) echo 'getAttributes(array('value', 'selected', 'data-post-type', 'data-ajax-url', 'data-nonce', 'data-extra-args', 'data-selected')), '>'; // Empty option first - empty text like Country field does (floating label shows the field name) echo ''; // If we have a selected value, fetch and render that post if (!empty($value) && is_numeric($value)) { $selected_post = get_post(absint($value)); if ($selected_post) { echo ''; } } echo ''; // Hidden data attributes for JS echo 'extra_args)) { echo ' data-extra-args="', esc_attr($this->extra_args), '"'; } if (!empty($value)) { echo ' data-selected="', esc_attr($value), '"'; } echo ' />'; echo '
'; } /** * Get the post type for this select * * @return string */ public function getPostType() { return $this->post_type; } /** * Get the extra query arguments * * @return string */ public function getExtraArgs() { return $this->extra_args; } } // phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped