Skip to content

Commit

Permalink
Merge pull request #28 from joanrodas/repeatable
Browse files Browse the repository at this point in the history
Repeatable
  • Loading branch information
joanrodas committed Mar 17, 2023
2 parents 7a3288a + f50ee62 commit 8cce994
Show file tree
Hide file tree
Showing 41 changed files with 1,126 additions and 1,105 deletions.
35 changes: 35 additions & 0 deletions CPF/Endpoints/Endpoints.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace CPF\Endpoints;

class Endpoints
{
public function __construct()
{
add_action('rest_api_init', [$this, 'register_endpoints']);
}

public function register_endpoints()
{
register_rest_route('custom-product-fields/v1', '/getFields/(?P<product_id>\d+)', array(
'methods' => 'GET',
'callback' => [$this, 'get_section_fields'],
'permission_callback' => '__return_true' //TODO: Permission
));
}

public function get_section_fields(\WP_REST_Request $request)
{
global $wpdb;
//$slug = $request->get_param('slug');
$product_id = $request->get_param('product_id');
// $results = $wpdb->get_results(
// $wpdb->prepare("SELECT meta_key, meta_value FROM {$wpdb->prefix}postmeta WHERE meta_key LIKE %s AND post_id = %d", [$slug . '%', $product_id]),
// OBJECT_K
// );
$results = get_post_meta($product_id);
return [
'fields' => $results
];
}
}
39 changes: 39 additions & 0 deletions CPF/Field/AssociationField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace CPF\Field;

class AssociationField extends Field
{

public function display($parent='')
{
$key = $parent . '_' . $this->slug;
$value = get_post_meta(get_the_ID(), '_' . $this->slug, true);
if ($value == '') $value = $this->default_value;
ob_start(); ?>
<p class="form-field _<?= $this->type ?>_field ">
<label for="_<?= $this->slug ?>"><?= $this->name ?></label>
<input type="color" class="short" style="" name="_<?= $this->slug ?>" id="_<?= $this->slug ?>" value="<?= $value ?>" placeholder="">
</p>
<?php echo ob_get_clean();
}

public function display_complex($parent='')
{
$key = $parent . '_' . $this->slug;
ob_start(); ?>
<p x-cloak class="form-field _<?= $this->type ?>_field ">
<label for="_<?= $this->slug ?>"><?= $this->name ?></label>
<input x-cloak type="color" class="short" style="" name="_<?= $this->slug . '[]' ?>" id="_<?= $this->slug ?>" :value="entries[tab] ? entries[tab]['<?= $this->slug ?>'] : '<?= $this->default_value ?>'" placeholder="">
</p>
<?php echo ob_get_clean();
}

public function save($product_id, $parent='')
{
$key = $parent . '_' . $this->slug;
if (isset($_POST[$key])) { // phpcs:ignore
update_post_meta($product_id, $key, $_POST[$key]); // phpcs:ignore
}
}
}
69 changes: 22 additions & 47 deletions CPF/Field/CheckboxField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,40 @@

namespace CPF\Field;

class CheckboxField
class CheckboxField extends Field
{

public function __construct(string $type, string $slug, string $name, bool $save_individual = true)
public function __construct(string $type, string $slug, string $name)
{
$this->type = $type;
$this->slug = $slug;
$this->name = $name;
parent::__construct($type, $slug, $name);
$this->default_value = '0';
$this->save_individual = $save_individual;
add_action('woocommerce_process_product_meta', [$this, 'save']);
}


public static function create(string $type, string $slug, string $name)
public function display($parent='')
{
return (new self($type, $slug, $name));
}

public function display()
{
$input = '';
$checked = get_post_meta(get_the_ID(), '_' . $this->slug, true);
$key = $parent . '_' . $this->slug;
$checked = get_post_meta(get_the_ID(), $key, true);
if ($checked == '') $checked = $this->default_value;
if ($this->type == 'checkbox') {
ob_start(); ?>
<p class="form-field _<?= $this->type ?>_field ">
<label for="_<?= $this->slug ?>"><?= $this->name ?></label>
<input type="checkbox" <?= $checked === '1' ? 'checked' : '' ?> name="_<?= $this->slug ?>" id="_<?= $this->slug ?>" value="1">
</p>
<?php $input = ob_get_clean();
}
echo $input;
ob_start(); ?>
<p class="form-field _<?= $this->type ?>_field ">
<label for="<?= $key ?>"><?= $this->name ?></label>
<input type="checkbox" <?= $checked === '1' ? 'checked' : '' ?> name="<?= $key ?>" id="<?= $key ?>" value="1">
</p>
<?php echo ob_get_clean();
}

public function display_complex() {
$input = '';
if ($this->type === 'checkbox') {
ob_start(); ?>
<p class="form-field _<?= $this->type ?>_field">
<label for="_<?= $this->slug ?>"><?= $this->name ?></label>
<input x-cloak type="checkbox" :checked="entries[tab] ? (entries[tab]['<?= $this->slug ?>'] == '1' ? true : false) : ('<?= $this->default_value ?>' == '1' ? true : false)" name="_<?= $this->slug . '[]' ?>" id="_<?= $this->slug ?>" value="1">
</p>
<?php $input = ob_get_clean();
}
echo $input;
public function display_complex($parent='') {
ob_start(); ?>
<p x-data="{field_name: '<?= $parent ?>_' + tab + '_<?= $this->slug ?>'}" class="form-field _<?= $this->type ?>_field">
<label :for="field_name"><?= $this->name ?></label>
<input x-cloak type="checkbox" :checked="section_fields[field_name] ? (section_fields[field_name] == '1' ? true : false) : ('<?= $this->default_value ?>' == '1' ? true : false)" :name="field_name" :id="field_name" value="1">
</p>
<?php echo ob_get_clean();
}

public function default_value($default_value) {
$this->default_value = $default_value;

return $this;
}

public function save($product_id)
{
if (!$this->save_individual) return;

$key = '_' . $this->slug;
public function save($product_id, $parent='')
{
$key = $parent . '_' . $this->slug;
if (isset($_POST[$key])) { // phpcs:ignore
update_post_meta($product_id, $key, '1'); // phpcs:ignore
}
Expand Down
79 changes: 28 additions & 51 deletions CPF/Field/ColorField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,44 @@

namespace CPF\Field;

class ColorField
class ColorField extends Field
{
use Traits\Datalist;

public function __construct(string $type, string $slug, string $name, bool $save_individual = true)
public function __construct(string $type, string $slug, string $name)
{
$this->type = $type;
$this->slug = $slug;
$this->name = $name;
parent::__construct($type, $slug, $name);
$this->default_value = "#000000";
$this->save_individual = $save_individual;
add_action('woocommerce_process_product_meta', [$this, 'save']);
}


public static function create(string $type, string $slug, string $name)
{
return (new self($type, $slug, $name));
}

public function display()
public function display($parent='')
{
$input = '';
$value = get_post_meta(get_the_ID(), '_' . $this->slug, true);
$key = $parent . '_' . $this->slug;
$value = get_post_meta(get_the_ID(), $key, true);
if ($value == '') $value = $this->default_value;
if ($this->type == 'color') {
ob_start(); ?>
<p class="form-field _<?= $this->type ?>_field ">
<label for="_<?= $this->slug ?>"><?= $this->name ?></label>
<input type="color" class="short" style="" name="_<?= $this->slug ?>" id="_<?= $this->slug ?>" value="<?= $value ?>" placeholder="">
</p>
<?php $input = ob_get_clean();
}
echo $input;
}

public function display_complex() {
$input = '';
if ($this->type == 'color') {
ob_start(); ?>
<p x-cloak class="form-field _<?= $this->type ?>_field ">
<label for="_<?= $this->slug ?>"><?= $this->name ?></label>
<input x-cloak type="color" class="short" style="" name="_<?= $this->slug . '[]' ?>" id="_<?= $this->slug ?>" :value="entries[tab] ? entries[tab]['<?= $this->slug ?>'] : '<?= $this->default_value ?>'" placeholder="">
</p>
<?php $input = ob_get_clean();
}
echo $input;
ob_start(); ?>
<p class="form-field _<?= $this->type ?>_field ">
<label for="<?= $key ?>"><?= $this->name ?></label>
<input type="color" class="short" style="" name="<?= $key ?>" id="<?= $key ?>" value="<?= $value ?>" placeholder="">
</p>
<?php echo ob_get_clean();
}

public function default_value($default_value) {
$this->default_value = $default_value;

return $this;
}

public function save($product_id)
public function display_complex($parent='')
{
if (!$this->save_individual) return;

$key = '_' . $this->slug;
if (isset($_POST[$key])) { // phpcs:ignore
update_post_meta($product_id, $key, $_POST[$key]); // phpcs:ignore
}
ob_start(); ?>
<p x-data="{field_name: '<?= $parent ?>_' + tab + '_<?= $this->slug ?>'}" class="form-field _<?= $this->type ?>_field">
<label :for="field_name"><?= $this->name ?></label>
<input x-cloak type="color" <?php if( !empty($this->datalist) ): ?> :list="field_name + '_datalist'" <?php endif; ?> class="short" style="" :name="field_name" :id="field_name" :value="section_fields[field_name] ? section_fields[field_name] : '<?= $this->default_value ?>'" placeholder="">
<?php if (!empty($this->datalist)): ?>
<datalist :id="field_name + '_datalist'">
<?php foreach( $this->datalist as $option ): ?>
<option value="<?= $option ?>">
<?php endforeach; ?>
</datalist>
<?php endif; ?>
</p>
<?php echo ob_get_clean();
}

}
63 changes: 28 additions & 35 deletions CPF/Field/DateField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,44 @@

namespace CPF\Field;

class DateField
class DateField extends Field
{
use Traits\Datalist;

public function __construct(string $type, string $slug, string $name)
{
$this->type = $type;
$this->slug = $slug;
$this->name = $name;
parent::__construct($type, $slug, $name);
$this->default_value = date('Y-m-d');
add_action('woocommerce_process_product_meta', [$this, 'save']);
}


public static function create(string $type, string $slug, string $name)
public function display($parent='')
{
return (new self($type, $slug, $name));
}

public function display()
{
$input = '';
$value = get_post_meta(get_the_ID(), '_' . $this->slug, true);
$key = $parent . '_' . $this->slug;
$value = get_post_meta(get_the_ID(), $key, true);
if ($value == '') $value = $this->default_value;
if ($this->type == 'date') {
ob_start(); ?>
<p class="form-field _<?= $this->type ?>_field ">
<label for="_<?= $this->slug ?>"><?= $this->name ?></label>
<input type="date" class="short" style="" name="_<?= $this->slug ?>" id="_<?= $this->slug ?>" value="<?= $value ?>" placeholder="">
</p>
<?php $input = ob_get_clean();
}
echo $input;
ob_start(); ?>
<p class="form-field _<?= $this->type ?>_field ">
<label for="<?= $key ?>"><?= $this->name ?></label>
<input type="date" class="short" style="" name="<?= $key ?>" id="<?= $key ?>" value="<?= $value ?>" placeholder="">
</p>
<?php echo ob_get_clean();
}

public function default_value($default_value) {
$this->default_value = $default_value;

return $this;
}

public function save($product_id)

public function display_complex($parent='')
{
$key = '_' . $this->slug;
if (isset($_POST[$key])) { // phpcs:ignore
update_post_meta($product_id, $key, $_POST[$key]); // phpcs:ignore
}
ob_start(); ?>
<p x-data="{field_name: '<?= $parent ?>_' + tab + '_<?= $this->slug ?>'}" class="form-field _<?= $this->type ?>_field">
<label :for="field_name"><?= $this->name ?></label>
<input x-cloak type="date" <?php if( !empty($this->datalist) ): ?> :list="field_name + '_datalist'" <?php endif; ?> class="short" style="" :name="field_name" :id="field_name" :value="section_fields[field_name] ? section_fields[field_name] : '<?= $this->default_value ?>'" placeholder="">
<?php if (!empty($this->datalist)): ?>
<datalist :id="field_name + '_datalist'">
<?php foreach( $this->datalist as $option ): ?>
<option value="<?= $option ?>">
<?php endforeach; ?>
</datalist>
<?php endif; ?>
</p>
<?php echo ob_get_clean();
}

}

0 comments on commit 8cce994

Please sign in to comment.