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;
|
||||
}
|
||||
}
|
||||
@@ -18,10 +18,9 @@ class DisplayFeaturedImageGenesisWidgetsBlocks {
|
||||
protected $block = 'displayfeaturedimagegenesis-';
|
||||
|
||||
/**
|
||||
* The plugin setting.
|
||||
* @var array
|
||||
* @var \DisplayFeaturedImageGenesisWidgetsBlocksFields
|
||||
*/
|
||||
protected $setting;
|
||||
private $fields;
|
||||
|
||||
/**
|
||||
* Register our block type.
|
||||
@@ -30,6 +29,7 @@ class DisplayFeaturedImageGenesisWidgetsBlocks {
|
||||
$this->register_script_style();
|
||||
include_once 'class-displayfeaturedimagegenesis-widgets-blocks-output.php';
|
||||
$output = new DisplayFeaturedImageGenesisWidgetsBlocksOutput();
|
||||
$fields = $this->get_fields_class();
|
||||
foreach ( $this->blocks() as $block => $data ) {
|
||||
if ( empty( $data['nickname'] ) || ! is_callable( array( $output, "render_{$data['nickname']}" ) ) ) {
|
||||
continue;
|
||||
@@ -39,7 +39,7 @@ class DisplayFeaturedImageGenesisWidgetsBlocks {
|
||||
array(
|
||||
'editor_script' => "{$this->block}block",
|
||||
'editor_style' => "{$this->block}block",
|
||||
'attributes' => $this->fields( $block ),
|
||||
'attributes' => $fields->fields( $block ),
|
||||
'render_callback' => array( $output, "render_{$data['nickname']}" ),
|
||||
)
|
||||
);
|
||||
@@ -86,13 +86,13 @@ class DisplayFeaturedImageGenesisWidgetsBlocks {
|
||||
* @return array
|
||||
*/
|
||||
protected function get_localization_data() {
|
||||
$blocks = $this->blocks();
|
||||
$common = array(
|
||||
'icon' => 'format-image',
|
||||
'category' => 'widgets',
|
||||
);
|
||||
$output = array();
|
||||
foreach ( $blocks as $block => $data ) {
|
||||
$fields = $this->get_fields_class();
|
||||
foreach ( $this->blocks() as $block => $data ) {
|
||||
if ( empty( $data['nickname'] ) ) {
|
||||
continue;
|
||||
}
|
||||
@@ -100,7 +100,7 @@ class DisplayFeaturedImageGenesisWidgetsBlocks {
|
||||
'main' => array(
|
||||
'title' => __( 'Block Settings', 'display-featured-image-genesis' ),
|
||||
'initialOpen' => true,
|
||||
'attributes' => $this->fields( $block ),
|
||||
'attributes' => $fields->fields( $block ),
|
||||
),
|
||||
);
|
||||
$common['block'] = "{$this->name}{$block}";
|
||||
@@ -117,140 +117,16 @@ class DisplayFeaturedImageGenesisWidgetsBlocks {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the fields for the block.
|
||||
*
|
||||
* @param $block
|
||||
*
|
||||
* @return array
|
||||
* Get the fields class.
|
||||
* @return \DisplayFeaturedImageGenesisWidgetsBlocksFields
|
||||
*/
|
||||
private 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 );
|
||||
private function get_fields_class() {
|
||||
if ( isset( $this->fields ) ) {
|
||||
return $this->fields;
|
||||
}
|
||||
include_once 'class-displayfeaturedimagegenesis-widgets-blocks-fields.php';
|
||||
$this->fields = new DisplayFeaturedImageGenesisWidgetsBlocksFields();
|
||||
|
||||
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
|
||||
*/
|
||||
protected 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;
|
||||
return $this->fields;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user