/
wp-static-blocks.php
113 lines (102 loc) · 3.08 KB
/
wp-static-blocks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
Plugin Name: Static Blocks
Description: Cms Static Blocks
Version: 1.0
Author: Sergey Cherepanov
Author URI: http://www.cherepanov.org.ua
*/
class StaticBlock
{
static function init()
{
$labels = array(
'name' => _x('Static Blocks', 'post type general name'),
'singular_name' => _x('Static Block', 'post type singular name'),
'add_new' => _x('Add new', 'Block'),
'add_new_item' => __('Add new'),
'edit_item' => __('Edit'),
'new_item' => __('New Static Block'),
'view_item' => __('Static Block Info'),
'search_items' => __('Search'),
'not_found' => __('Not Found'),
'not_found_in_trash' => __('Not Found in trash'),
'parent_item_colon' => ''
);
register_post_type('static_block',
array(
'labels' => $labels,
'public' => true,
'supports' => array('title', 'editor'),
)
);
}
static public function init_meta_boxes()
{
add_meta_box('identifier', 'Identifier', array('StaticBlock', 'meta_box_control'), 'static_block', 'normal', 'high');
}
static public function meta_box_control($post)
{
?>
<label>Identifier:<input type="text" value="<?php echo get_post_meta($post->ID, '_identifier', true) ?>"
name="identifier"/></label>
<?php
}
static public function save($post_ID)
{
if (isset($_POST['identifier'])) {
if ($sku = trim($_POST['identifier'])) {
update_post_meta($post_ID, '_identifier', $sku);
} else {
delete_post_meta($post_ID, '_identifier');
}
}
}
}
add_action('init', array('StaticBlock', 'init'));
if (is_admin()) {
add_action('load-post.php', array('StaticBlock', 'init_meta_boxes'));
add_action('load-post-new.php', array('StaticBlock', 'init_meta_boxes'));
add_action('edit_post', array('StaticBlock', 'save'));
}
function get_product_identifier($post = null)
{
if (!$post) {
global $post;
}
return get_post_meta($post->ID, '_identifier', true);
}
/**
* @param string|int $identifier
* @return array|bool
*/
function get_static_blocks_by_id($identifier)
{
/** @var $wpdb wpdb */
global $wpdb;
if (!$table = _get_meta_table('post')) {
return false;
}
$postIds = $wpdb->get_col($wpdb->prepare("SELECT `post_id` FROM $table WHERE `meta_key` = '_identifier' AND `meta_value` = %s", $identifier));
if (empty($postIds)) {
return false;
}
return get_posts(array('include' => $postIds, 'post_type' => 'static_block'));
}
/**
* @param int $identifier
* @return string
*/
function get_block_content($identifier)
{
$result = '';
$blocks = get_static_blocks_by_id($identifier);
if ($blocks) {
foreach ($blocks as $block) {
if ($block->post_content) {
$result .= apply_filters('the_content', $block->post_content);
}
}
}
return $result;
}