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

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