Skip to content

Commit

Permalink
✨ 增加MetaBox组件
Browse files Browse the repository at this point in the history
  • Loading branch information
Licoy committed Nov 1, 2022
1 parent 6057a62 commit 9688f32
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 235 deletions.
26 changes: 0 additions & 26 deletions inc/classes/PuockMetaBox.php

This file was deleted.

36 changes: 36 additions & 0 deletions inc/classes/meta/MetaBox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Puock\Theme\classes\meta;

class MetaBox extends PuockAbsMeta
{
protected $instance_args = array(
'single' => true,
'post_type' => 'post',
'context' => 'normal',
'priority' => 'high',
);

/**
* @param string $id
* @param array $args
*/
public function __construct(string $id, array $args)
{
$this->id = $id;
$args['id'] = $id;
$this->instance_args = array_merge($this->instance_args, $args);
add_action('add_meta_boxes', array(&$this, 'add_meta_boxes'));
add_action('save_post', array(&$this, 'baseSaveData'));
}

public function add_meta_boxes($post_type)
{
$type = $this->instance_args['post_type'];
if ((is_string($type) && $type === $post_type) || (is_array($type) && in_array($post_type, $type))) {
add_meta_box($this->id, $this->instance_args['title'], array(&$this, 'baseRender'), $this->instance_args['post_type'],
$this->instance_args['context'], $this->instance_args['priority']);
}
}

}
158 changes: 158 additions & 0 deletions inc/classes/meta/PuockAbsMeta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php

namespace Puock\Theme\classes\meta;

abstract class PuockAbsMeta
{
private static $args = array();

private $cache_data = null;

protected $instance_args = array();

protected $id;

public static function newPostMeta($id, $args = array())
{
self::$args['post'][$id] = $args;
}

public static function newTaxonomyMeta($id, $args = array())
{
self::$args['taxonomy'][$id] = $args;
}

public static function newSection($id, $args = array())
{
self::$args['section'][$id] = $args;
}

public static function load()
{
foreach (self::$args as $type => $args) {
if ($type === 'post') {
foreach ($args as $id => $meta) {
new MetaBox($id, $meta);
}
}
}
}

public function getValue($post_id, $attr_id, $default = '')
{
if ($this->instance_args['single']) {
return get_post_meta($post_id, $attr_id, true);
} else {
if ($this->cache_data === null) {
$this->cache_data = get_post_meta($post_id, $this->id, true);
}
if (isset($this->cache_data[$attr_id])) {
return $this->cache_data[$attr_id];
}
}
return $default;
}

/**
* @param $post_id
* @param $args array[id|title|std|type|desc|options]
* @return void
*/
public function baseSaveData($post_id)
{
$args = $this->instance_args;
if (!wp_verify_nonce(@$_POST[$args['id'] . '_noncename'], plugin_basename(__FILE__))) {
return $post_id;
}
$data = array();
foreach ($args['options'] as $option) {
$val = $_POST[$option['id']] ?? '';
if (empty($val)) {
continue;
}
if ($args['single']) {
update_post_meta($post_id, $option['id'], $val);
} else {
$data[$option['id']] = $val;
}
}
if (!$args['single']) {
update_post_meta($post_id, $args['id'], $data);
}
}

/**
* @param $args array[id|title|std|type|desc|options]
* @return void
*/
public function baseRender($post)
{
$args = $this->instance_args;
echo '<input type="hidden" name="' . $args['id'] . '_noncename" id="' . $args['id'] . '_noncename" value="' . wp_create_nonce(plugin_basename(__FILE__)) . '" />';
echo "<table class='form-table' role='presentation'><tbody>";
foreach ($args['options'] as $arg) {
echo "<tr>";
$desc = '';
if (isset($arg['desc'])) {
$desc = "<p class='description'>{$arg['desc']}</p>";
}
$value = $this->getValue($post->ID, $arg['id'], $arg['std'] ?? '');
switch ($arg['type']) {
case 'title':
echo '<h4>' . $arg['title'] . '</h4>';
break;
case 'des':
echo '<p>' . $arg['desc'] . '</p>';
break;
case 'textarea':
echo '<th>' . $arg['title'] . '</th>';
echo '<td><textarea cols="40" rows="2" name="' . $arg['id'] . '">' . $value . '</textarea>' . $desc . '</td>';
break;
case 'color':
echo '<th>' . $arg['title'] . '</th>';
echo '<td><input type="color" value="' . $value . '" name="' . $arg['id'] . '"/>' . $desc . '</td>';
break;
case 'select':
if (@$arg['multiple'] && is_string($value)) {
$value = explode(',', $value);
}
echo '<th>' . $arg['title'] . '</th>';
echo '<td><select ' . (@$arg['multiple'] ? 'multiple' : '') . ' name="' . $arg['id'] . (@$arg['multiple'] ? '[]' : '') . '">';
foreach ($arg['options'] as $option) {
if (is_array($value)) {
$selected = in_array($option['value'], $value) ? 'selected' : '';
} else {
$selected = $value == $option['value'] ? 'selected' : '';
}
echo '<option value="' . $option['value'] . '" ' . $selected . '>' . $option['label'] . '</option>';
}
echo "</select>{$desc}</td>";
break;
case 'radio':
echo '<th>' . $arg['title'] . '</th>';
echo '<td>';
foreach ($arg['options'] as $option) {
$checked = "";
if ($value == $option['value']) {
$checked = 'checked = "checked"';
}
echo '<input ' . $checked . ' type="radio" class="kcheck" value="' . $option['value'] . '" name="' . $option['id'] . '_value"/>' . $option['label'];
}
echo $desc . '</td>';
break;
case 'checkbox':
echo '<th>' . $arg['title'] . '</th>';
$checked = '';
if ($value == 'true')
$checked = 'checked = "checked"';
echo '<td><label><input type="checkbox" name="' . $arg['id'] . '" value="true" ' . $checked . ' />' . $arg['title'] . '</label>' . $desc . '</td>';
break;
default:
echo '<th>' . $arg['title'] . '</th>';
echo '<td><input type="text" size="40" name="' . $arg['id'] . '" value="' . $value . '" />' . $desc . '</td>';
}
echo "</tr>";
}
echo "</tbody></table>";
}
}
3 changes: 3 additions & 0 deletions inc/fun/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ function pk_get_wp_links($link_cats = '')
if (empty($link_cats)) {
return null;
}
if(is_array($link_cats)){
$link_cats = implode(',', $link_cats);
}
$sql = "select links.*,terms.term_id,terms.name from {$wpdb->links} as links
LEFT JOIN (select * from {$wpdb->term_relationships} where term_taxonomy_id in ({$link_cats})) as relat on links.link_id = relat.object_id
LEFT JOIN (selecT * from {$wpdb->terms} where term_id in ({$link_cats})) as terms on terms.term_id = relat.term_taxonomy_id
Expand Down
Loading

0 comments on commit 9688f32

Please sign in to comment.