-
Notifications
You must be signed in to change notification settings - Fork 3
/
init.php
175 lines (131 loc) · 5.05 KB
/
init.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/*
Plugin Name: Shortcode to Gutenberg Block
Plugin URI: https://wordpress.org/plugins/shortcode-to-gutenberg
Description: Converts your shortcodes to Gutenberg Blocks
Version: 0.1.0
Author: Pramod Jodhani
Author URI: http://pramodjodhani.com
*/
define('STG_FILE' , __FILE__);
define('STG_URL' , plugins_url("" , STG_FILE)."/" );
require_once "lib/util.php";
require_once "admin/init.php";
require_once "lib/STG.class.php";
require_once "metabox.php";
//delete these required
//require_once "test/file.php";
class STG_Main {
function __construct() {
add_action( 'wp_enqueue_scripts', array($this , "front_scripts") );
add_action( 'admin_enqueue_scripts', array($this , "backend_scripts") );
add_action( 'init', array($this , "register_gutenblock") );
add_action( 'init', array($this , "register_gutenblock_posttype") );
add_action("admin_footer" , array($this , "generate_gutenberg_blocks_code"));
}
function front_scripts() {
//wp_enqueue_style( 'skedool-css-front', plugins_url( "/css/style.css", __FILE__ ) );
//wp_enqueue_script( 'skedool-js-front', plugins_url( "/js/script.js", __FILE__ ) , array(), '1.0.0', true);
}
function backend_scripts() {
wp_enqueue_style( 'stg-css-admin', plugins_url( "/css/adminstyle.css", __FILE__ ) );
wp_enqueue_script( 'stg-js-admin', plugins_url( "/js/common.js", __FILE__ ) , array(), '1.0.0', true);
$css = plugin_dir_url( __FILE__ ) . 'css/dashicons-picker.css';
wp_enqueue_style( 'dashicons-picker', $css, array( 'dashicons' ), '1.0' );
$js = plugin_dir_url( __FILE__ ) . 'js/dashicons-picker.js';
wp_enqueue_script( 'dashicons-picker', $js, array( 'jquery' ), '1.0' );
}
function register_gutenblock_posttype() {
$labels = array(
'name' => 'GutenBlock',
'singular_name' => 'GutenBlock',
'menu_name' => 'GutenBlocks',
'name_admin_bar' => 'Gutenberg Block',
'add_new' => 'Add New Block',
'add_new_item' => 'Add New Gutenberg Block',
'new_item' => 'New Gutenberg Block',
'edit_item' => 'Edit Gutenberg Block',
'view_item' => 'View Gutenberg Block',
'all_items' => 'All Blocks',
'search_items' => 'Search Blocks',
'parent_item_colon' => 'Parent Block:',
'not_found' => 'No Blocks found.',
'not_found_in_trash' => 'No Blocks found in Trash.',
);
$args = array(
'label' => "Guten Block",
'labels' => $labels,
'description' => 'Create Guten block from shortcodes',
'public' => false,
'publicly_queryable' => false,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'menu_icon' => 'dashicons-editor-table',
//'rewrite' => array( 'slug' => 'book' ),
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', )
);
register_post_type( 'gutenblock', $args );
}
function register_gutenblock() {
$qry = new WP_Query(array(
"post_type"=>"gutenblock",
"posts_per_page" => 20,
"post_status" => "publish"
));
while($qry->have_posts()) {
$qry->the_post();
$post_id = get_the_ID();
$stg_namespace = get_post_meta($post_id , "stg_namespace" , true);
$stg_php_function = get_post_meta($post_id , "stg_php_function" , true);
$stg_php_function = stg_trim_fn($stg_php_function);
if(function_exists($stg_php_function)) {
register_block_type( $stg_namespace , array(
'render_callback' => $stg_php_function,
) );
}
}
}
function generate_gutenberg_blocks_code() {
global $current_screen;
if (!isset($current_screen)) {$current_screen = get_current_screen();}
if ( ( method_exists($current_screen, 'is_block_editor') && $current_screen->is_block_editor() )
|| ( function_exists('is_gutenberg_page')) && is_gutenberg_page() ) {
$qry = new WP_Query(array(
"post_type"=>"gutenblock",
"posts_per_page" => 20,
"post_status" => "publish"
));
while($qry->have_posts()) {
$qry->the_post();
$this->handle_single_block_post(get_the_ID());
}
}
}
function handle_single_block_post($post_id) {
if( function_exists( 'register_block_type' ) ) {
//if(is_gutenberg_page()) {
//$base = esc_attr(get_post_meta($post_id , "stg_base" , true));
$namespace = esc_attr(get_post_meta($post_id , "stg_namespace" , true));
$cat = esc_attr(get_post_meta($post_id , "stg_category" , true));
$title = get_the_title($post_id);
$json = esc_attr(get_post_meta($post_id , "stg_form_json" , true));
$icon = esc_attr(get_post_meta($post_id , "stg_dashicon" , true));
$guten = new Shortcode_guten($namespace , $cat , $title, $json , $icon);
$html = $guten->get_head();
$html .= $guten->get_edit();
$html .= $guten->get_save();
$html .= $guten->get_footer();
//debug
//echo "<pre>";
echo $html;
//exit;
//}
}
}
}
new STG_Main();