PluginProbe
Smart Custom Fields / 5.0.8
Smart Custom Fields v5.0.8
1.5.1 1.5.2 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.7.0 2.0.0 2.0.1 2.1.0 2.1.1 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 3.0.0 3.0.1 3.1.0 3.1.1 3.1.2 All 68 releases
smart-custom-fields / smart-custom-fields.php

smart-custom-fields.php in Smart Custom Fields 5.0.8, at smart-custom-fields.php

332 lines 9.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin name: Smart Custom Fields
4 * Plugin URI: https://github.com/inc2734/smart-custom-fields/
5 * Description: Smart Custom Fields is a simple plugin that management custom fields.
6 * Version: 5.0.8
7 * Author: inc2734
8 * Author URI: https://2inc.org
9 * Text Domain: smart-custom-fields
10 * Domain Path: /languages
11 * License: GPLv2 or later
12 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
13 *
14 * @package smart-custom-fields
15 * @author inc2734
16 * @license GPL-2.0+
17 */
18
19 /**
20 * Directory url of this plugin.
21 *
22 * @var string
23 */
24 define( 'SMART_CUSTOM_FIELDS_URL', untrailingslashit( plugin_dir_url( __FILE__ ) ) );
25
26 /**
27 * Directory path of this plugin.
28 *
29 * @var string
30 */
31 define( 'SMART_CUSTOM_FIELDS_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
32
33 /**
34 * Main class.
35 */
36 class Smart_Custom_Fields {
37
38 /**
39 * __construct
40 */
41 public function __construct() {
42 require_once plugin_dir_path( __FILE__ ) . 'classes/class.config.php';
43 require_once plugin_dir_path( __FILE__ ) . 'classes/class.rest-api.php';
44 add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
45 register_uninstall_hook( __FILE__, array( __CLASS__, 'uninstall' ) );
46
47 new Smart_Custom_Fields_Rest_API();
48 }
49
50 /**
51 * Plugins loaded.
52 */
53 public function plugins_loaded() {
54 do_action( SCF_Config::PREFIX . 'load' );
55
56 require_once plugin_dir_path( __FILE__ ) . 'classes/class.scf.php';
57 require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.abstract-field-base.php';
58 require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.ajax.php';
59 require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.cache.php';
60 require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.group.php';
61 require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.meta.php';
62 require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.options-page.php';
63 require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.setting.php';
64
65 add_action( 'init', array( $this, '_init' ) );
66 add_action( 'init', array( $this, 'register_post_type' ) );
67 add_action( 'init', array( $this, 'ajax_request' ) );
68 add_action( 'admin_menu', array( $this, 'admin_menu' ) );
69 add_action( 'current_screen', array( $this, 'current_screen' ) );
70 }
71
72 /**
73 * Initialize.
74 */
75 public function _init() {
76 require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.revisions.php';
77 new Smart_Custom_Fields_Revisions();
78
79 if ( function_exists( 'wpseo_init' ) ) {
80 require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.yoast-seo-analysis.php';
81 new Smart_Custom_Fields_Yoast_SEO_Analysis();
82 }
83
84 foreach ( glob( plugin_dir_path( __FILE__ ) . 'classes/fields/*.php' ) as $form_item ) {
85 include_once $form_item;
86 $basename = basename( $form_item, '.php' );
87 $classname = preg_replace( '/^class\.field\-(.+)$/', 'Smart_Custom_Fields_Field_$1', $basename );
88 $classname = str_replace( '-', '_', $classname );
89 if ( class_exists( $classname ) ) {
90 new $classname();
91 }
92 }
93
94 /**
95 * The action hook provides in after_setup_themeto be able to add fields
96 * from themes not only plugins.
97 */
98 do_action( SCF_Config::PREFIX . 'fields-loaded' );
99 }
100
101 /**
102 * Uninstall proccesses
103 */
104 public static function uninstall() {
105 $cf_posts = get_posts(
106 array(
107 'post_type' => SCF_Config::NAME,
108 'posts_per_page' => -1,
109 'post_status' => 'any',
110 )
111 );
112 foreach ( $cf_posts as $post ) {
113 wp_delete_post( $post->ID, true );
114 }
115 delete_post_meta_by_key( SCF_Config::PREFIX . 'repeat-multiple-data' );
116
117 // option の smart-cf-xxx を削除
118 // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
119 global $wpdb;
120 $wpdb->query(
121 $wpdb->prepare(
122 "
123 DELETE FROM $wpdb->options
124 WHERE option_name LIKE %s
125 ",
126 SCF_Config::PREFIX . '%'
127 )
128 );
129 // phpcs:enable
130 }
131
132 /**
133 * Run management screens.
134 *
135 * @param WP_Screen $screen Current WP_Screen object.
136 */
137 public function current_screen( $screen ) {
138 // 一覧画面
139 if ( 'edit-' . SCF_Config::NAME === $screen->id ) {
140 return;
141 }
142
143 // 新規作成・編集画面
144 if ( SCF_Config::NAME === $screen->id ) {
145 require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.settings.php';
146 new Smart_Custom_Fields_Controller_Settings();
147 return;
148 }
149
150 // その他の新規作成・編集画面
151 if ( in_array( $screen->id, get_post_types(), true ) ) {
152 $post_id = $this->get_post_id_in_admin();
153 if ( SCF::get_settings( SCF::generate_post_object( $post_id, $screen->id ) ) ) {
154 require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.controller-base.php';
155 require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.editor.php';
156 new Smart_Custom_Fields_Revisions();
157 new Smart_Custom_Fields_Controller_Editor();
158 }
159 return;
160 }
161
162 // プロフィール編集画面
163 if ( in_array( $screen->id, array( 'profile', 'user-edit' ), true ) ) {
164 $user_id = $this->get_user_id_in_admin();
165 if ( SCF::get_settings( get_userdata( $user_id ) ) ) {
166 require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.controller-base.php';
167 require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.profile.php';
168 new Smart_Custom_Fields_Controller_Profile();
169 }
170 return;
171 }
172
173 // タグ、カテゴリー、タクソノミー
174 if ( $screen->taxonomy ) {
175 $term_id = $this->get_term_id_in_admin();
176 if ( $term_id ) {
177 $term = get_term( $term_id, $screen->taxonomy );
178 if ( SCF::get_settings( $term ) ) {
179 require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.controller-base.php';
180 require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.taxonomy.php';
181 new Smart_Custom_Fields_Controller_Taxonomy();
182 }
183 }
184 return;
185 }
186
187 // オプションページ
188 $menu_slug = preg_replace( '/^toplevel_page_(.+)$/', '$1', $screen->id );
189 $options_pages = SCF::get_options_pages();
190 if ( array_key_exists( $menu_slug, $options_pages ) ) {
191 $option = SCF::generate_option_object( $menu_slug );
192 if ( SCF::get_settings( $option ) ) {
193 require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.controller-base.php';
194 require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.option.php';
195 new Smart_Custom_Fields_Controller_Option();
196 }
197 }
198 }
199
200 /**
201 * Registering custom post type.
202 * Run the menu display in a different method.
203 */
204 public function register_post_type() {
205 $labels = array(
206 'name' => __( 'Smart Custom Fields', 'smart-custom-fields' ),
207 'menu_name' => __( 'Smart Custom Fields', 'smart-custom-fields' ),
208 'name_admin_bar' => __( 'Smart Custom Fields', 'smart-custom-fields' ),
209 'add_new' => __( 'Add New', 'smart-custom-fields' ),
210 'add_new_item' => __( 'Add New', 'smart-custom-fields' ),
211 'new_item' => __( 'New Field', 'smart-custom-fields' ),
212 'edit_item' => __( 'Edit Field', 'smart-custom-fields' ),
213 'view_item' => __( 'View Field', 'smart-custom-fields' ),
214 'all_items' => __( 'All Fields', 'smart-custom-fields' ),
215 'search_items' => __( 'Search Fields', 'smart-custom-fields' ),
216 'parent_item_colon' => __( 'Parent Fields:', 'smart-custom-fields' ),
217 'not_found' => __( 'No Fields found.', 'smart-custom-fields' ),
218 'not_found_in_trash' => __( 'No Fields found in Trash.', 'smart-custom-fields' ),
219 );
220 register_post_type(
221 SCF_Config::NAME,
222 array(
223 'label' => 'Smart Custom Fields',
224 'labels' => $labels,
225 'public' => false,
226 'show_ui' => true,
227 'capability_type' => 'page',
228 'supports' => array( 'title', 'page-attributes' ),
229 'show_in_menu' => false,
230 )
231 );
232 }
233
234 /**
235 * Hooking the process that it want to fire when the ajax request.
236 */
237 public function ajax_request() {
238 new Smart_Custom_Fields_Ajax();
239 }
240
241 /**
242 * Adding menus in management screen.
243 */
244 public function admin_menu() {
245 add_menu_page(
246 'Smart Custom Fields',
247 'Smart Custom Fields',
248 'manage_options',
249 'edit.php?post_type=' . SCF_Config::NAME,
250 false,
251 false,
252 '80.026'
253 );
254 add_submenu_page(
255 'edit.php?post_type=' . SCF_Config::NAME,
256 __( 'Add New', 'smart-custom-fields' ),
257 __( 'Add New', 'smart-custom-fields' ),
258 'manage_options',
259 'post-new.php?post_type=' . SCF_Config::NAME
260 );
261 }
262
263 /**
264 * Getting the post ID in post editing page.
265 *
266 * @return int
267 */
268 protected function get_post_id_in_admin() {
269 $post_id = false;
270 $_post = filter_input( INPUT_GET, 'post' );
271
272 if ( ! empty( $_post ) ) {
273 $post_id = $_post;
274 }
275
276 $_post_id = filter_input( INPUT_POST, 'post_ID' );
277 if ( ! empty( $_post_id ) ) {
278 $post_id = $_post_id;
279 }
280
281 return $post_id;
282 }
283
284 /**
285 * Getting the user ID in profile and user editing pages.
286 *
287 * @return int
288 */
289 protected function get_user_id_in_admin() {
290 $screen = get_current_screen();
291 $user_id = false;
292 $_user_id = filter_input( INPUT_GET, 'user_id' );
293
294 if ( ! empty( $_user_id ) ) {
295 $user_id = $_user_id;
296 }
297
298 $_user_id = filter_input( INPUT_POST, 'user_id' );
299 if ( ! empty( $_user_id ) ) {
300 $user_id = $_user_id;
301 }
302
303 if ( 'profile' === $screen->id ) {
304 $current_user = wp_get_current_user();
305 $user_id = $current_user->ID;
306 }
307
308 return $user_id;
309 }
310
311 /**
312 * Getting the term ID in term editing page.
313 *
314 * @return int
315 */
316 protected function get_term_id_in_admin() {
317 $term_id = false;
318 $tag_id = filter_input( INPUT_GET, 'tag_ID' );
319 if ( ! empty( $tag_id ) ) {
320 $term_id = $tag_id;
321 }
322
323 $tag_id = filter_input( INPUT_POST, 'tag_ID' );
324 if ( ! empty( $tag_id ) ) {
325 $term_id = $tag_id;
326 }
327
328 return $term_id;
329 }
330 }
331 new Smart_Custom_Fields();
332