File: /var/www/html/insiders/wp-load/wp-content/plugins/gutenmate/lib/custom-fields.php
<?php
function gtm_register_custom_fields( $slug, $args ) {
GTM_Custom_Fields::getInstance()->register_fields( $slug, $args );
}
add_filter( 'gtm_config', 'gtm_add_custom_fields_config' );
function gtm_add_custom_fields_config( $config ) {
$fields = GTM_Custom_Fields::getInstance()->get_fields();
if ( ! empty( $fields ) ) {
$config['custom_fields'] = $fields;
}
return $config;
}
if ( ! class_exists( 'GTM_Custom_Fields' ) ) {
class GTM_Custom_Fields {
private static $instance;
private $fields = [];
public static function getInstance() {
if ( is_null( static::$instance ) ) {
static::$instance = new self();
}
return static::$instance;
}
public function register_fields( $slug, $args ) {
$slug = sanitize_key( $slug );
$args = wp_parse_args( $args, [
'title' => '',
'description' => '',
'post_type' => 'post', // Use empty value for all post types
'fields' => [],
] );
// Store the fields
$this->fields[$slug] = $this->sanitize_fields( $args );
// Register post meta
foreach ( $args['fields'] as $fieldSlug => $field ) {
$restType = 'string';
register_post_meta( $args['post_type'], $fieldSlug, [
'show_in_rest' => true,
'single' => true,
'type' => $restType,
] );
}
}
/* Enfore structure and sanitize values */
private function sanitize_fields( $args ) {
$args['title'] = sanitize_text_field( $args['title'] );
$args['description'] = sanitize_text_field( $args['description'] );
$args['post_type'] = sanitize_text_field( $args['post_type'] );
foreach ( $args['fields'] as $fieldSlug => $field ) {
$args['fields'][$fieldSlug]['type'] = sanitize_key( $args['fields'][$fieldSlug]['type'] ?? '' );
$args['fields'][$fieldSlug]['title'] = sanitize_text_field( $args['fields'][$fieldSlug]['title'] ?? '' );
$args['fields'][$fieldSlug]['description'] = sanitize_text_field( $args['fields'][$fieldSlug]['description'] ?? '' );
}
return $args;
}
public function get_fields() {
return $this->fields;
}
}
}
function gtm_has_custom_field_set( $slug, $post_id = null ) {
$post_id = $post_id ?: get_the_ID();
$fields = GTM_Custom_Fields::getInstance()->get_fields();
$has = false;
if ( ! empty( $fields[$slug]['fields'] ) ) {
foreach ( $fields[$slug]['fields'] as $field_slug => $field ) {
$post_meta = get_post_meta( $post_id, $field_slug, true );
$has |= ! empty( $post_meta );
}
}
return $has;
}
/*
Custom field preset: Recipe summary
*/
add_action( 'init', 'gtm_register_recipe_custom_fields', 11 );
function gtm_register_recipe_custom_fields() {
if ( get_theme_support( 'gutenmate-recipe-summary' ) ) {
gtm_register_custom_fields( 'gtm_recipe_summary', [
'title' => esc_html__( 'Recipe summary', 'gutenmate' ),
'description' => esc_html__( 'Enter an additional summary for recipe post. These fields will be shown with post if the selected post layout is supported.', 'gutenmate' ),
'post_type' => 'post',
'fields' => [
'gtm_recipe_cook_time' => [
'title' => esc_html__( 'Cook time', 'gutenmate' ),
'description' => esc_html__( 'Enter time for cooking. Example, 10 mins.', 'gutenmate' ),
'type' => 'text',
],
'gtm_recipe_serving_size' => [
'title' => esc_html__( 'Serves', 'gutenmate' ),
'description' => esc_html__( 'Enter serving size. Example, 4-6.', 'gutenmate' ),
'type' => 'text',
],
'gtm_recipe_skill_level' => [
'title' => esc_html__( 'Skill level', 'gutenmate' ),
'description' => esc_html__( 'Enter skill level for this recipe. Example, Easy, Medium, Hard.', 'gutenmate' ),
'type' => 'text',
],
],
] );
}
}
/*
Custom field preset: Place summary
*/
add_action( 'init', 'gtm_register_place_custom_fields', 11 );
function gtm_register_place_custom_fields() {
if ( get_theme_support( 'gutenmate-place-summary' ) ) {
gtm_register_custom_fields( 'gtm_place_summary', [
'title' => esc_html__( 'Place summary', 'gutenmate' ),
'description' => esc_html__( 'Enter an additional summary for place post. These fields will be shown with post if the selected post layout is supported.', 'gutenmate' ),
'post_type' => 'post',
'fields' => [
'gtm_place_location' => [
'title' => esc_html__( 'Location', 'gutenmate' ),
'description' => esc_html__( 'Enter location for the place.', 'gutenmate' ),
'type' => 'text',
],
'gtm_place_price' => [
'title' => esc_html__( 'Price', 'gutenmate' ),
'description' => esc_html__( 'Enter price for the place.', 'gutenmate' ),
'type' => 'text',
],
'gtm_place_rating' => [
'title' => esc_html__( 'Rating', 'gutenmate' ),
'description' => esc_html__( 'Enter place rating.', 'gutenmate' ),
'type' => 'text',
],
],
] );
}
}
/*
Custom field preset: Event summary
*/
add_action( 'init', 'gtm_register_event_custom_fields', 11 );
function gtm_register_event_custom_fields() {
if ( get_theme_support( 'gutenmate-event-summary' ) ) {
gtm_register_custom_fields( 'gtm_event_summary', [
'title' => esc_html__( 'Event summary', 'gutenmate' ),
'description' => esc_html__( 'Enter an additional summary for event post. These fields will be shown with post if the selected post layout is supported.', 'gutenmate' ),
'post_type' => 'post',
'fields' => [
'gtm_event_location' => [
'title' => esc_html__( 'Location', 'gutenmate' ),
'description' => esc_html__( 'Enter location for the event.', 'gutenmate' ),
'type' => 'text',
],
'gtm_event_date' => [
'title' => esc_html__( 'Date', 'gutenmate' ),
'description' => esc_html__( 'Enter date for the event.', 'gutenmate' ),
'type' => 'text',
],
'gtm_event_price' => [
'title' => esc_html__( 'Price', 'gutenmate' ),
'description' => esc_html__( 'Enter price for the event.', 'gutenmate' ),
'type' => 'text',
],
],
] );
}
}