mirror of
https://github.com/10h30/display-featured-image-genesis.git
synced 2026-06-05 15:08:20 +09:00
Move field builder to separate class
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
class DisplayFeaturedImageGenesisWidgetsBlocksFields {
|
||||
|
||||
/**
|
||||
* Get the fields for the block.
|
||||
*
|
||||
* @param $block
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function fields( $block ) {
|
||||
$output = array();
|
||||
foreach ( $this->get_all_fields( $block ) as $key => $value ) {
|
||||
if ( ! empty( $value['args']['id'] ) ) {
|
||||
$key = $value['args']['id'];
|
||||
}
|
||||
$output[ $key ] = $this->get_individual_field_attributes( $value, $block );
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $block
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_all_fields( $block ) {
|
||||
$fields = "{$block}_fields";
|
||||
$attributes = array_merge(
|
||||
include 'fields/blocks-attributes.php',
|
||||
$this->$fields()
|
||||
);
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function cpt_fields() {
|
||||
$form = new DisplayFeaturedImageGenesisWidgetsForm( $this, array() );
|
||||
|
||||
return array_merge(
|
||||
include 'fields/cpt-post_type.php',
|
||||
include 'fields/text.php',
|
||||
include 'fields/image.php',
|
||||
include 'fields/archive.php'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function author_fields() {
|
||||
$form = new DisplayFeaturedImageGenesisWidgetsForm( $this, array() );
|
||||
$user = array(
|
||||
array(
|
||||
'method' => 'select',
|
||||
'args' => include 'fields/author-user.php',
|
||||
),
|
||||
);
|
||||
|
||||
return array_merge(
|
||||
$user,
|
||||
include 'fields/author-image.php',
|
||||
include 'fields/author-gravatar.php',
|
||||
include 'fields/author-description.php',
|
||||
include 'fields/author-archive.php'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array of attributes for an individual field.
|
||||
*
|
||||
* @param $field
|
||||
* @param $block
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_individual_field_attributes( $field, $block ) {
|
||||
$method = empty( $field['method'] ) ? 'text' : $field['method'];
|
||||
$field_type = $this->get_field_type( $method );
|
||||
if ( empty( $field['args']['label'] ) ) {
|
||||
return $field;
|
||||
}
|
||||
$defaults = include "fields/{$block}-defaults.php";
|
||||
$attributes = array(
|
||||
'type' => $field_type,
|
||||
'default' => $defaults[ $field['args']['id'] ],
|
||||
'label' => $field['args']['label'],
|
||||
'method' => $method,
|
||||
);
|
||||
if ( in_array( 'number', array( $field_type, $method ), true ) ) {
|
||||
$attributes['min'] = $field['args']['min'];
|
||||
$attributes['max'] = $field['args']['max'];
|
||||
} elseif ( 'select' === $method ) {
|
||||
$attributes['options'] = $this->convert_choices_for_select( $field['args']['choices'] );
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the type of field for our script.
|
||||
*
|
||||
* @param $method
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function get_field_type( $method ) {
|
||||
$type = 'string';
|
||||
if ( 'number' === $method ) {
|
||||
return $method;
|
||||
}
|
||||
if ( 'checkbox' === $method ) {
|
||||
return 'boolean';
|
||||
}
|
||||
|
||||
return $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a standard PHP array to what the block editor needs.
|
||||
*
|
||||
* @param $options
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function convert_choices_for_select( $options ) {
|
||||
$output = array();
|
||||
foreach ( $options as $value => $label ) {
|
||||
$output[] = array(
|
||||
'value' => $value,
|
||||
'label' => $label,
|
||||
);
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user