PluginProbe
Smart Custom Fields / 4.1.3
Smart Custom Fields v4.1.3
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 4.1.3, at smart-custom-fields.php

283 lines 9.0 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: 4.1.3
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 class Smart_Custom_Fields {
15
16 /**
17 * __construct
18 */
19 public function __construct() {
20 require_once plugin_dir_path( __FILE__ ) . 'classes/class.config.php';
21 require_once plugin_dir_path( __FILE__ ) . 'classes/class.rest-api.php';
22 add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
23 register_uninstall_hook( __FILE__, array( __CLASS__, 'uninstall' ) );
24
25 new Smart_Custom_Fields_Rest_API();
26 }
27
28 /**
29 * Loading translation files
30 */
31 public function plugins_loaded() {
32 load_plugin_textdomain(
33 'smart-custom-fields',
34 false,
35 dirname( plugin_basename( __FILE__ ) ) . '/languages'
36 );
37
38 do_action( SCF_Config::PREFIX . 'load' );
39 require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.meta.php';
40 require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.setting.php';
41 require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.group.php';
42 require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.abstract-field-base.php';
43 require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.revisions.php';
44 require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.ajax.php';
45 require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.options-page.php';
46 require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.cache.php';
47 require_once plugin_dir_path( __FILE__ ) . 'classes/class.scf.php';
48 new Smart_Custom_Fields_Revisions();
49
50 if ( function_exists( 'wpseo_init' ) ) {
51 require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.yoast-seo-analysis.php';
52 new Smart_Custom_Fields_Yoast_SEO_Analysis();
53 }
54
55 foreach ( glob( plugin_dir_path( __FILE__ ) . 'classes/fields/*.php' ) as $form_item ) {
56 include_once $form_item;
57 $basename = basename( $form_item, '.php' );
58 $classname = preg_replace( '/^class\.field\-(.+)$/', 'Smart_Custom_Fields_Field_$1', $basename );
59 $classname = str_replace( '-', '_', $classname );
60 if ( class_exists( $classname ) ) {
61 new $classname();
62 }
63 }
64
65 add_action( 'after_setup_theme', array( $this, 'after_setup_theme' ) );
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 * The action hook provides in after_setup_themeto be able to add fields
74 * from themes not only plugins.
75 */
76 public function after_setup_theme() {
77 do_action( SCF_Config::PREFIX . 'fields-loaded' );
78 }
79
80 /**
81 * Uninstall proccesses
82 */
83 public static function uninstall() {
84 $cf_posts = get_posts(
85 array(
86 'post_type' => SCF_Config::NAME,
87 'posts_per_page' => -1,
88 'post_status' => 'any',
89 )
90 );
91 foreach ( $cf_posts as $post ) {
92 wp_delete_post( $post->ID, true );
93 }
94 delete_post_meta_by_key( SCF_Config::PREFIX . 'repeat-multiple-data' );
95
96 // option の smart-cf-xxx を削除
97 global $wpdb;
98 $wpdb->query(
99 $wpdb->prepare(
100 "
101 DELETE FROM $wpdb->options
102 WHERE option_name LIKE %s
103 ",
104 SCF_Config::PREFIX . '%'
105 )
106 );
107 }
108
109 /**
110 * Run management screens
111 *
112 * @param WP_Screen $screen
113 */
114 public function current_screen( $screen ) {
115 // 一覧画面
116 if ( $screen->id === 'edit-' . SCF_Config::NAME ) {
117 }
118 // 新規作成・編集画面
119 elseif ( $screen->id === SCF_Config::NAME ) {
120 require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.settings.php';
121 new Smart_Custom_Fields_Controller_Settings();
122 }
123 // その他の新規作成・編集画面
124 elseif ( in_array( $screen->id, get_post_types() ) ) {
125 $post_id = $this->get_post_id_in_admin();
126 if ( SCF::get_settings( SCF::generate_post_object( $post_id, $screen->id ) ) ) {
127 require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.controller-base.php';
128 require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.editor.php';
129 new Smart_Custom_Fields_Revisions();
130 new Smart_Custom_Fields_Controller_Editor();
131 }
132 }
133 // プロフィール編集画面
134 elseif ( in_array( $screen->id, array( 'profile', 'user-edit' ) ) ) {
135 $user_id = $this->get_user_id_in_admin();
136 if ( SCF::get_settings( get_userdata( $user_id ) ) ) {
137 require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.controller-base.php';
138 require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.profile.php';
139 new Smart_Custom_Fields_Controller_Profile();
140 }
141 }
142 // タグ、カテゴリー、タクソノミー
143 elseif ( $screen->taxonomy ) {
144 $term_id = $this->get_term_id_in_admin();
145 if ( $term_id ) {
146 $term = get_term( $term_id, $screen->taxonomy );
147 if ( SCF::get_settings( $term ) ) {
148 require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.controller-base.php';
149 require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.taxonomy.php';
150 new Smart_Custom_Fields_Controller_Taxonomy();
151 }
152 }
153 }
154 // オプションページ
155 else {
156 $menu_slug = preg_replace( '/^toplevel_page_(.+)$/', '$1', $screen->id );
157 $options_pages = SCF::get_options_pages();
158
159 if ( array_key_exists( $menu_slug, $options_pages ) ) {
160 $Option = SCF::generate_option_object( $menu_slug );
161 if ( SCF::get_settings( $Option ) ) {
162 require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.controller-base.php';
163 require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.option.php';
164 new Smart_Custom_Fields_Controller_Option();
165 }
166 }
167 }
168 }
169
170 /**
171 * Registering custom post type.
172 * Run the menu display in a different method.
173 */
174 public function register_post_type() {
175 $labels = array(
176 'name' => __( 'Smart Custom Fields', 'smart-custom-fields' ),
177 'menu_name' => __( 'Smart Custom Fields', 'smart-custom-fields' ),
178 'name_admin_bar' => __( 'Smart Custom Fields', 'smart-custom-fields' ),
179 'add_new' => __( 'Add New', 'smart-custom-fields' ),
180 'add_new_item' => __( 'Add New', 'smart-custom-fields' ),
181 'new_item' => __( 'New Field', 'smart-custom-fields' ),
182 'edit_item' => __( 'Edit Field', 'smart-custom-fields' ),
183 'view_item' => __( 'View Field', 'smart-custom-fields' ),
184 'all_items' => __( 'All Fields', 'smart-custom-fields' ),
185 'search_items' => __( 'Search Fields', 'smart-custom-fields' ),
186 'parent_item_colon' => __( 'Parent Fields:', 'smart-custom-fields' ),
187 'not_found' => __( 'No Fields found.', 'smart-custom-fields' ),
188 'not_found_in_trash' => __( 'No Fields found in Trash.', 'smart-custom-fields' ),
189 );
190 register_post_type(
191 SCF_Config::NAME,
192 array(
193 'label' => 'Smart Custom Fields',
194 'labels' => $labels,
195 'public' => false,
196 'show_ui' => true,
197 'capability_type' => 'page',
198 'supports' => array( 'title', 'page-attributes' ),
199 'show_in_menu' => false,
200 )
201 );
202 }
203
204 /**
205 * Hooking the process that it want to fire when the ajax request.
206 */
207 public function ajax_request() {
208 $Ajax = new Smart_Custom_Fields_Ajax();
209 }
210
211 /**
212 * Adding menus in management screen.
213 */
214 public function admin_menu() {
215 add_menu_page(
216 'Smart Custom Fields',
217 'Smart Custom Fields',
218 'manage_options',
219 'edit.php?post_type=' . SCF_Config::NAME,
220 false,
221 false,
222 '80.026'
223 );
224 add_submenu_page(
225 'edit.php?post_type=' . SCF_Config::NAME,
226 __( 'Add New', 'smart-custom-fields' ),
227 __( 'Add New', 'smart-custom-fields' ),
228 'manage_options',
229 'post-new.php?post_type=' . SCF_Config::NAME
230 );
231 }
232
233 /**
234 * Getting the post ID in post editing page.
235 *
236 * @return int
237 */
238 protected function get_post_id_in_admin() {
239 $post_id = false;
240 if ( ! empty( $_GET['post'] ) ) {
241 $post_id = $_GET['post'];
242 } elseif ( ! empty( $_POST['post_ID'] ) ) {
243 $post_id = $_POST['post_ID'];
244 }
245 return $post_id;
246 }
247
248 /**
249 * Getting the user ID in profile and user editing pages.
250 *
251 * @return int
252 */
253 protected function get_user_id_in_admin() {
254 $screen = get_current_screen();
255 $user_id = false;
256 if ( ! empty( $_GET['user_id'] ) ) {
257 $user_id = $_GET['user_id'];
258 } elseif ( ! empty( $_POST['user_id'] ) ) {
259 $user_id = $_POST['user_id'];
260 } elseif ( $screen->id === 'profile' ) {
261 $current_user = wp_get_current_user();
262 $user_id = $current_user->ID;
263 }
264 return $user_id;
265 }
266
267 /**
268 * Getting the term ID in term editing page.
269 *
270 * @return int
271 */
272 protected function get_term_id_in_admin() {
273 $term_id = false;
274 if ( ! empty( $_GET['tag_ID'] ) ) {
275 $term_id = $_GET['tag_ID'];
276 } elseif ( ! empty( $_POST['tag_ID'] ) ) {
277 $term_id = $_POST['tag_ID'];
278 }
279 return $term_id;
280 }
281 }
282 new Smart_Custom_Fields();
283