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

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