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

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