-
Notifications
You must be signed in to change notification settings - Fork 34
/
smart-custom-fields.php
325 lines (292 loc) · 9.63 KB
/
smart-custom-fields.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
/**
* Plugin name: Smart Custom Fields
* Plugin URI: https://github.com/inc2734/smart-custom-fields/
* Description: Smart Custom Fields is a simple plugin that management custom fields.
* Version: 5.0.0
* Author: inc2734
* Author URI: https://2inc.org
* Text Domain: smart-custom-fields
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*
* @package smart-custom-fields
* @author inc2734
* @license GPL-2.0+
*/
/**
* Directory url of this plugin.
*
* @var string
*/
define( 'SMART_CUSTOM_FIELDS_URL', untrailingslashit( plugin_dir_url( __FILE__ ) ) );
/**
* Directory path of this plugin.
*
* @var string
*/
define( 'SMART_CUSTOM_FIELDS_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
/**
* Main class.
*/
class Smart_Custom_Fields {
/**
* __construct
*/
public function __construct() {
require_once plugin_dir_path( __FILE__ ) . 'classes/class.config.php';
require_once plugin_dir_path( __FILE__ ) . 'classes/class.rest-api.php';
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
register_uninstall_hook( __FILE__, array( __CLASS__, 'uninstall' ) );
new Smart_Custom_Fields_Rest_API();
}
/**
* Loading translation files
*/
public function plugins_loaded() {
do_action( SCF_Config::PREFIX . 'load' );
require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.meta.php';
require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.setting.php';
require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.group.php';
require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.abstract-field-base.php';
require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.revisions.php';
require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.ajax.php';
require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.options-page.php';
require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.cache.php';
require_once plugin_dir_path( __FILE__ ) . 'classes/class.scf.php';
new Smart_Custom_Fields_Revisions();
if ( function_exists( 'wpseo_init' ) ) {
require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.yoast-seo-analysis.php';
new Smart_Custom_Fields_Yoast_SEO_Analysis();
}
foreach ( glob( plugin_dir_path( __FILE__ ) . 'classes/fields/*.php' ) as $form_item ) {
include_once $form_item;
$basename = basename( $form_item, '.php' );
$classname = preg_replace( '/^class\.field\-(.+)$/', 'Smart_Custom_Fields_Field_$1', $basename );
$classname = str_replace( '-', '_', $classname );
if ( class_exists( $classname ) ) {
new $classname();
}
}
add_action( 'after_setup_theme', array( $this, 'after_setup_theme' ) );
add_action( 'init', array( $this, 'register_post_type' ) );
add_action( 'init', array( $this, 'ajax_request' ) );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'current_screen', array( $this, 'current_screen' ) );
}
/**
* The action hook provides in after_setup_themeto be able to add fields
* from themes not only plugins.
*/
public function after_setup_theme() {
do_action( SCF_Config::PREFIX . 'fields-loaded' );
}
/**
* Uninstall proccesses
*/
public static function uninstall() {
$cf_posts = get_posts(
array(
'post_type' => SCF_Config::NAME,
'posts_per_page' => -1,
'post_status' => 'any',
)
);
foreach ( $cf_posts as $post ) {
wp_delete_post( $post->ID, true );
}
delete_post_meta_by_key( SCF_Config::PREFIX . 'repeat-multiple-data' );
// option の smart-cf-xxx を削除
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
global $wpdb;
$wpdb->query(
$wpdb->prepare(
"
DELETE FROM $wpdb->options
WHERE option_name LIKE %s
",
SCF_Config::PREFIX . '%'
)
);
// phpcs:enable
}
/**
* Run management screens.
*
* @param WP_Screen $screen Current WP_Screen object.
*/
public function current_screen( $screen ) {
// 一覧画面
if ( 'edit-' . SCF_Config::NAME === $screen->id ) {
return;
}
// 新規作成・編集画面
if ( SCF_Config::NAME === $screen->id ) {
require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.settings.php';
new Smart_Custom_Fields_Controller_Settings();
return;
}
// その他の新規作成・編集画面
if ( in_array( $screen->id, get_post_types(), true ) ) {
$post_id = $this->get_post_id_in_admin();
if ( SCF::get_settings( SCF::generate_post_object( $post_id, $screen->id ) ) ) {
require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.controller-base.php';
require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.editor.php';
new Smart_Custom_Fields_Revisions();
new Smart_Custom_Fields_Controller_Editor();
}
return;
}
// プロフィール編集画面
if ( in_array( $screen->id, array( 'profile', 'user-edit' ), true ) ) {
$user_id = $this->get_user_id_in_admin();
if ( SCF::get_settings( get_userdata( $user_id ) ) ) {
require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.controller-base.php';
require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.profile.php';
new Smart_Custom_Fields_Controller_Profile();
}
return;
}
// タグ、カテゴリー、タクソノミー
if ( $screen->taxonomy ) {
$term_id = $this->get_term_id_in_admin();
if ( $term_id ) {
$term = get_term( $term_id, $screen->taxonomy );
if ( SCF::get_settings( $term ) ) {
require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.controller-base.php';
require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.taxonomy.php';
new Smart_Custom_Fields_Controller_Taxonomy();
}
}
return;
}
// オプションページ
$menu_slug = preg_replace( '/^toplevel_page_(.+)$/', '$1', $screen->id );
$options_pages = SCF::get_options_pages();
if ( array_key_exists( $menu_slug, $options_pages ) ) {
$option = SCF::generate_option_object( $menu_slug );
if ( SCF::get_settings( $option ) ) {
require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.controller-base.php';
require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.option.php';
new Smart_Custom_Fields_Controller_Option();
}
}
}
/**
* Registering custom post type.
* Run the menu display in a different method.
*/
public function register_post_type() {
$labels = array(
'name' => __( 'Smart Custom Fields', 'smart-custom-fields' ),
'menu_name' => __( 'Smart Custom Fields', 'smart-custom-fields' ),
'name_admin_bar' => __( 'Smart Custom Fields', 'smart-custom-fields' ),
'add_new' => __( 'Add New', 'smart-custom-fields' ),
'add_new_item' => __( 'Add New', 'smart-custom-fields' ),
'new_item' => __( 'New Field', 'smart-custom-fields' ),
'edit_item' => __( 'Edit Field', 'smart-custom-fields' ),
'view_item' => __( 'View Field', 'smart-custom-fields' ),
'all_items' => __( 'All Fields', 'smart-custom-fields' ),
'search_items' => __( 'Search Fields', 'smart-custom-fields' ),
'parent_item_colon' => __( 'Parent Fields:', 'smart-custom-fields' ),
'not_found' => __( 'No Fields found.', 'smart-custom-fields' ),
'not_found_in_trash' => __( 'No Fields found in Trash.', 'smart-custom-fields' ),
);
register_post_type(
SCF_Config::NAME,
array(
'label' => 'Smart Custom Fields',
'labels' => $labels,
'public' => false,
'show_ui' => true,
'capability_type' => 'page',
'supports' => array( 'title', 'page-attributes' ),
'show_in_menu' => false,
)
);
}
/**
* Hooking the process that it want to fire when the ajax request.
*/
public function ajax_request() {
new Smart_Custom_Fields_Ajax();
}
/**
* Adding menus in management screen.
*/
public function admin_menu() {
add_menu_page(
'Smart Custom Fields',
'Smart Custom Fields',
'manage_options',
'edit.php?post_type=' . SCF_Config::NAME,
false,
false,
'80.026'
);
add_submenu_page(
'edit.php?post_type=' . SCF_Config::NAME,
__( 'Add New', 'smart-custom-fields' ),
__( 'Add New', 'smart-custom-fields' ),
'manage_options',
'post-new.php?post_type=' . SCF_Config::NAME
);
}
/**
* Getting the post ID in post editing page.
*
* @return int
*/
protected function get_post_id_in_admin() {
$post_id = false;
$_post = filter_input( INPUT_GET, 'post' );
if ( ! empty( $_post ) ) {
$post_id = $_post;
}
$_post_id = filter_input( INPUT_POST, 'post_ID' );
if ( ! empty( $_post_id ) ) {
$post_id = $_post_id;
}
return $post_id;
}
/**
* Getting the user ID in profile and user editing pages.
*
* @return int
*/
protected function get_user_id_in_admin() {
$screen = get_current_screen();
$user_id = false;
$_user_id = filter_input( INPUT_GET, 'user_id' );
if ( ! empty( $_user_id ) ) {
$user_id = $_user_id;
}
$_user_id = filter_input( INPUT_POST, 'user_id' );
if ( ! empty( $_user_id ) ) {
$user_id = $_user_id;
}
if ( 'profile' === $screen->id ) {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
}
return $user_id;
}
/**
* Getting the term ID in term editing page.
*
* @return int
*/
protected function get_term_id_in_admin() {
$term_id = false;
$tag_id = filter_input( INPUT_GET, 'tag_ID' );
if ( ! empty( $tag_id ) ) {
$term_id = $tag_id;
}
$tag_id = filter_input( INPUT_POST, 'tag_ID' );
if ( ! empty( $tag_id ) ) {
$term_id = $tag_id;
}
return $term_id;
}
}
new Smart_Custom_Fields();