-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathplugin.php
160 lines (141 loc) · 4.45 KB
/
plugin.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/*
* Plugin Name: mytheme-blocks-2012
* Plugin URI: https://alialaa.com/
* Description: Blocks for mytheme.
* Author: Ali Alaa
* Author URI https://alialaa.com/
*/
if (!defined('ABSPATH')) {
exit;
}
include_once('src/metabox.php');
function mytheme_blocks_categories($categories, $post)
{
return array_merge(
$categories,
array(
array(
'slug' => 'mytheme-category',
'title' => __('My Theme Category', 'mytheme-blocks'),
'icon' => 'wordpress'
)
)
);
}
add_filter('block_categories', 'mytheme_blocks_categories', 10, 2);
function mytheme_blocks_register_block_type($block, $options = array())
{
register_block_type(
'mytheme-blocks/' . $block,
array_merge(
array(
'editor_script' => 'mytheme-blocks-editor-script',
'editor_style' => 'mytheme-blocks-editor-style',
'script' => 'mytheme-blocks-script',
'style' => 'mytheme-blocks-style'
),
$options
)
);
}
function mytheme_blocks_enqueue_assets()
{
wp_enqueue_script(
'mytheme-blocks-editor-js',
plugins_url('dist/editor_script.js', __FILE__),
array('wp-data', 'wp-plugins', 'wp-edit-post', 'wp-i18n', 'wp-components', 'wp-data', 'wp-compose')
);
}
add_action('enqueue_block_editor_assets', 'mytheme_blocks_enqueue_assets');
function mytheme_blocks_register()
{
wp_register_script(
'mytheme-blocks-editor-script',
plugins_url('dist/editor.js', __FILE__),
array('wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor', 'wp-components', 'lodash', 'wp-blob', 'wp-data', 'wp-html-entities', 'wp-compose', 'wp-block-editor')
);
wp_register_script(
'mytheme-blocks-script',
plugins_url('dist/script.js', __FILE__),
array('jquery')
);
wp_register_style(
'mytheme-blocks-editor-style',
plugins_url('dist/editor.css', __FILE__),
array('wp-edit-blocks')
);
wp_register_style(
'mytheme-blocks-style',
plugins_url('dist/style.css', __FILE__)
);
mytheme_blocks_register_block_type('firstblock');
mytheme_blocks_register_block_type('secondblock');
mytheme_blocks_register_block_type('team-member');
mytheme_blocks_register_block_type('team-members');
mytheme_blocks_register_block_type(
'latest-posts',
array(
'render_callback' => 'mytheme_blocks_render_latest_posts_block',
'attributes' => array(
'numberOfPosts' => array(
'type' => 'number',
'default' => 5
),
'postCategories' => array(
'type' => 'string',
)
)
)
);
mytheme_blocks_register_block_type('redux');
mytheme_blocks_register_block_type('todo-list');
mytheme_blocks_register_block_type('todo-list-count');
mytheme_blocks_register_block_type('meta');
}
add_action('init', 'mytheme_blocks_register');
function mytheme_blocks_render_latest_posts_block($attributes)
{
$args = array(
'posts_per_page' => $attributes['numberOfPosts']
);
if ($attributes['postCategories']) {
$args['cat'] = $attributes['postCategories'];
}
$query = new WP_Query($args);
$posts = '';
if ($query->have_posts()) {
$posts .= '<ul class="wp-block-mytheme-blocks-latest-posts">';
while ($query->have_posts()) {
$query->the_post();
$posts .= '<li><a href="' . esc_url(get_the_permalink()) . '">'
. get_the_title() . '</a></li>';
}
$posts .= '</ul>';
wp_reset_postdata();
return $posts;
} else {
return '<div>' . __('No Posts Found', "mytheme-blocks") . '</div>';
}
}
function mytheme_blocks_register_post_template()
{
$post_type_object = get_post_type_object('post');
$post_type_object->template = array(
array('mytheme-blocks/meta'),
array('core/paragraph', array(
'content' => 'cljljlj'
)),
array(
'mytheme-blocks/team-members',
array(
'columns' => 2
),
array(
array('mytheme-blocks/team-member', array('title' => 'ljljljl;j')),
array('mytheme-blocks/team-member'),
)
)
);
}
add_action('init', 'mytheme_blocks_register_post_template');