class-form.php
<?php
namespace HivePress\Components;
use HivePress\Helpers as hp;
defined( 'ABSPATH' ) || exit;
final class Form extends Component {
public function __construct( $args = [] ) {
add_filter( 'hivepress/v1/fields/field', [ $this, 'set_field_arguments' ] );
add_filter( 'hivepress/v1/fields/field/options', [ $this, 'set_field_options' ], 10, 2 );
if ( $this->is_captcha_enabled() ) {
add_filter( 'hivepress/v1/forms/form/meta', [ $this, 'set_captcha' ] );
add_filter( 'hivepress/v1/forms/form', [ $this, 'add_captcha' ], 10, 2 );
add_filter( 'hivepress/v1/forms/form/errors', [ $this, 'validate_captcha' ], 10, 2 );
}
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
parent::__construct( $args );
}
protected function get_field_options( $type, $args, $value = null ) {
$options = [];
$method = 'get_' . $type;
if ( method_exists( $this, $method ) ) {
$options = call_user_func_array( [ $this, $method ], [ $args, $value ] );
} else {
$options = hivepress()->get_config( $type );
}
return $options;
}
public function set_field_arguments( $args ) {
if ( isset( $args['options'] ) && ! is_array( $args['options'] ) ) {
if ( 'icons' === $args['options'] ) {
$args['attributes']['data-template'] = 'icon';
}
if ( ! isset( $args['source'] ) ) {
$args['options'] = $this->get_field_options( $args['options'], hp\get_array_value( $args, 'option_args', [] ) );
}
}
return $args;
}
public function set_field_options( $options, $field ) {
return $this->get_field_options(
$field->get_arg( 'options' ),
(array) $field->get_arg( 'option_args' ),
$field->get_value()
);
}
protected function get_users( $args, $value ) {
$args = array_merge(
[
'fields' => [ 'ID', 'user_login' ],
],
$args
);
if ( $value ) {
$args['include'] = (array) $value;
}
$users = wp_list_pluck( get_users( $args ), 'user_login', 'ID' );
return $users;
}
protected function get_posts( $args, $value ) {
$args = array_merge(
[
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
],
$args
);
if ( $value ) {
$args['post__in'] = (array) $value;
}
$options = [];
if ( post_type_exists( $args['post_type'] ) ) {
$cache_group = hivepress()->model->get_cache_group( 'post', $args['post_type'] );
$options = null;
if ( strpos( $args['post_type'], 'hp_' ) === 0 ) {
$options = hivepress()->cache->get_cache( array_merge( $args, [ 'fields' => 'titles' ] ), $cache_group );
}
if ( is_null( $options ) ) {
$options = wp_list_pluck( get_posts( $args ), 'post_title', 'ID' );
if ( strpos( $args['post_type'], 'hp_' ) === 0 && count( $options ) <= 1000 ) {
hivepress()->cache->set_cache( array_merge( $args, [ 'fields' => 'titles' ] ), $cache_group, $options );
}
}
}
return $options;
}
protected function get_terms( $args, $value ) {
$args = array_merge(
[
'taxonomy' => 'category',
'hide_empty' => false,
],
$args
);
if ( $value ) {
$args['include'] = (array) $value;
}
$options = [];
if ( taxonomy_exists( $args['taxonomy'] ) ) {
$cache_group = hivepress()->model->get_cache_group( 'term', $args['taxonomy'] );
$options = null;
if ( strpos( $args['taxonomy'], 'hp_' ) === 0 ) {
$options = hivepress()->cache->get_cache( $args, $cache_group );
}
if ( is_null( $options ) ) {
$options = [];
if ( strpos( $args['taxonomy'], 'hp_' ) === 0 && ! isset( $args['orderby'] ) && get_terms(
array_merge(
$args,
[
'number' => 1,
'fields' => 'ids',
'meta_query' => [
[
'key' => 'hp_sort_order',
'value' => 0,
'compare' => '>',
'type' => 'NUMERIC',
],
],
]
)
) ) {
$terms = get_terms(
array_merge(
$args,
[
'orderby' => 'meta_value_num',
'meta_query' => [
'relation' => 'OR',
[
'key' => 'hp_sort_order',
'type' => 'NUMERIC',
'compare' => 'EXISTS',
],
[
'key' => 'hp_sort_order',
'type' => 'NUMERIC',
'compare' => 'NOT EXISTS',
],
],
]
)
);
} else {
$terms = get_terms( $args );
}
foreach ( $terms as $term ) {
$options[ $term->term_id ] = [
'label' => $term->name,
'parent' => $term->parent ? $term->parent : null,
];
}
if ( strpos( $args['taxonomy'], 'hp_' ) === 0 && count( $options ) <= 1000 ) {
hivepress()->cache->set_cache( $args, $cache_group, $options );
}
}
}
return $options;
}
protected function get_forms( $args, $value ) {
$options = [];
foreach ( hivepress()->get_classes( 'forms' ) as $form_name => $form ) {
if ( $form::get_meta( 'label' ) && ! array_diff( $args, array_intersect_key( $form::get_meta(), $args ) ) ) {
$options[ $form_name ] = $form::get_meta( 'label' );
}
}
asort( $options );
return $options;
}
protected function get_fields( $args, $value ) {
$options = [];
foreach ( hivepress()->get_classes( 'fields' ) as $field_name => $field ) {
if ( $field::get_meta( 'label' ) && ! array_diff( $args, array_intersect_key( $field::get_meta(), $args ) ) ) {
$options[ $field_name ] = $field::get_meta( 'label' );
}
}
asort( $options );
return $options;
}
protected function get_mime_types( $args, $value ) {
return get_allowed_mime_types();
}
protected function get_templates( $args, $value ) {
$options = [];
foreach ( hivepress()->get_classes( 'templates' ) as $template_name => $template ) {
if ( $template::get_meta( 'label' ) ) {
$options[ $template_name ] = $template::get_meta( 'label' );
}
}
asort( $options );
return $options;
}
protected function get_days( $args, $value ) {
return array_map(
function( $day ) {
return date_i18n( 'D', strtotime( $day ) );
},
[
1 => 'monday',
2 => 'tuesday',
3 => 'wednesday',
4 => 'thursday',
5 => 'friday',
6 => 'saturday',
0 => 'sunday',
]
);
}
protected function get_timezones() {
if ( ! is_textdomain_loaded( 'continents-cities' ) ) {
load_textdomain( 'continents-cities', WP_LANG_DIR . '/continents-cities-' . get_locale() . '.mo' );
}
$timezones = [];
foreach ( timezone_identifiers_list() as $name ) {
$labels = [];
foreach ( explode( '/', str_replace( '_', ' ', $name ) ) as $label ) {
$labels[] = translate( $label, 'continents-cities' );
}
if ( count( $labels ) > 1 ) {
array_shift( $labels );
}
$timezones[ $name ] = implode( ' - ', $labels );
}
asort( $timezones );
return $timezones;
}
protected function get_emails( $args, $value ) {
$options = [];
foreach ( hivepress()->get_classes( 'emails' ) as $email_name => $email ) {
if ( $email::get_meta( 'label' ) ) {
$options[ $email_name ] = $email::get_meta( 'label' );
}
}
asort( $options );
return $options;
}
protected function is_captcha_enabled() {
return get_option( 'hp_recaptcha_site_key' ) && get_option( 'hp_recaptcha_secret_key' );
}
public function set_captcha( $meta ) {
if ( isset( $meta['captcha'] ) && in_array( $meta['name'], (array) get_option( 'hp_recaptcha_forms' ), true ) ) {
$meta['captcha'] = true;
}
return $meta;
}
public function add_captcha( $args, $form ) {
if ( $form::get_meta( 'captcha' ) ) {
$args['fields']['_captcha'] = [
'type' => 'captcha',
'_separate' => true,
'_order' => 10000,
];
}
return $args;
}
public function validate_captcha( $errors, $form ) {
if ( $form::get_meta( 'captcha' ) ) {
$response = json_decode(
wp_remote_retrieve_body(
wp_remote_get(
'https://www.google.com/recaptcha/api/siteverify?' . http_build_query(
[
'secret' => get_option( 'hp_recaptcha_secret_key' ),
'response' => sanitize_text_field( hp\get_array_value( $_POST, 'g-recaptcha-response' ) ),
]
)
)
),
true
);
if ( ! hp\get_array_value( $response, 'success', false ) ) {
$errors[] = esc_html__( 'Captcha is invalid.', 'hivepress' );
}
}
return $errors;
}
public function enqueue_scripts() {
$language = hivepress()->translator->get_language();
$filepath = '/node_modules/flatpickr/dist/l10n/' . ( 'el' === $language ? 'gr' : $language ) . '.js';
if ( file_exists( hivepress()->get_path() . $filepath ) ) {
wp_enqueue_script(
'flatpickr-' . $language,
hivepress()->get_url() . $filepath,
[ 'flatpickr' ],
hivepress()->get_version(),
true
);
}
if ( ! is_admin() ) {
$filepath = '/node_modules/select2/dist/js/i18n/' . $language . '.js';
if ( file_exists( hivepress()->get_path() . $filepath ) ) {
wp_enqueue_script(
'select2-' . $language,
hivepress()->get_url() . $filepath,
[ 'select2-full' ],
hivepress()->get_version(),
true
);
}
if ( $this->is_captcha_enabled() ) {
wp_enqueue_script(
'recaptcha',
'https://www.google.com/recaptcha/api.js',
[],
null,
false
);
wp_script_add_data( 'recaptcha', 'async', true );
wp_script_add_data( 'recaptcha', 'defer', true );
}
}
}
}