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

264 lines 8.2 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: 1.6.2
7 * Author: inc2734
8 * Author URI: http://2inc.org
9 * Created: October 9, 2014
10 * Modified: Novermber 21, 2015
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 * 翻訳ファイルの読み込み
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 add_action( 'after_setup_theme', array( $this, 'after_setup_theme' ) );
38 }
39
40 /**
41 * 各クラスの読み込み
42 */
43 public function after_setup_theme() {
44 do_action( SCF_Config::PREFIX . 'load' );
45 require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.meta.php';
46 require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.setting.php';
47 require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.group.php';
48 require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.abstract-field-base.php';
49 require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.revisions.php';
50 require_once plugin_dir_path( __FILE__ ) . 'classes/models/class.ajax.php';
51 require_once plugin_dir_path( __FILE__ ) . 'classes/class.scf.php';
52 new Smart_Custom_Fields_Revisions();
53
54 foreach ( glob( plugin_dir_path( __FILE__ ) . 'classes/fields/*.php' ) as $form_item ) {
55 include_once $form_item;
56 $basename = basename( $form_item, '.php' );
57 $classname = preg_replace( '/^class\.field\-(.+)$/', 'Smart_Custom_Fields_Field_$1', $basename );
58 if ( class_exists( $classname ) ) {
59 new $classname();
60 }
61 }
62 do_action( SCF_Config::PREFIX . 'fields-loaded' );
63
64 add_action( 'init' , array( $this, 'register_post_type' ) );
65 add_action( 'init' , array( $this, 'ajax_request' ) );
66 add_action( 'admin_menu' , array( $this, 'admin_menu' ) );
67 add_action( 'current_screen', array( $this, 'current_screen' ) );
68 }
69
70 /**
71 * アンインストール時の処理
72 */
73 public static function uninstall() {
74 $cf_posts = get_posts( array(
75 'post_type' => SCF_Config::NAME,
76 'posts_per_page' => -1,
77 'post_status' => 'any',
78 ) );
79 foreach ( $cf_posts as $post ) {
80 wp_delete_post( $post->ID, true );
81 }
82 delete_post_meta_by_key( SCF_Config::PREFIX . 'repeat-multiple-data' );
83
84 // option の smart-cf-xxx を削除
85 global $wpdb;
86 $wpdb->query(
87 $wpdb->prepare(
88 "
89 DELETE FROM $wpdb->options
90 WHERE option_name LIKE %s
91 ",
92 SCF_Config::PREFIX . '%'
93 )
94 );
95 }
96
97 /**
98 * 各管理画面の実行
99 *
100 * @param WP_Screen $screen
101 */
102 public function current_screen( $screen ) {
103 // 一覧画面
104 if ( $screen->id === 'edit-' . SCF_Config::NAME ) {
105 }
106 // 新規作成・編集画面
107 elseif ( $screen->id === SCF_Config::NAME ) {
108 require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.settings.php';
109 new Smart_Custom_Fields_Controller_Settings();
110 }
111 // その他の新規作成・編集画面
112 elseif ( in_array( $screen->id, get_post_types() ) ) {
113 $post_id = $this->get_post_id_in_admin();
114 $Post = new stdClass();
115 $Post->ID = $post_id;
116 $Post->post_type = $screen->id;
117 if ( SCF::get_settings( new WP_Post( $Post ) ) ) {
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 $user_data = get_userdata( $user_id );
128 $roles[0] = false;
129 if ( $user_data ) {
130 $roles = $user_data->roles;
131 }
132 if ( SCF::get_settings( get_userdata( $user_id ) ) ) {
133 require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.controller-base.php';
134 require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.profile.php';
135 new Smart_Custom_Fields_Controller_Profile();
136 }
137 }
138 // タグ、カテゴリー、タクソノミー
139 elseif ( $screen->taxonomy ) {
140 $term_id = $this->get_term_id_in_admin();
141 if ( $term_id ) {
142 $term = get_term( $term_id, $screen->taxonomy );
143 if ( SCF::get_settings( $term ) ) {
144 require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.controller-base.php';
145 require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.taxonomy.php';
146 new Smart_Custom_Fields_Controller_Taxonomy();
147 }
148 }
149 }
150 }
151
152 /**
153 * カスタム投稿タイプの登録。メニュー表示は別メソッドで実行
154 */
155 public function register_post_type() {
156 $labels = array(
157 'name' => __( 'Smart Custom Fields', 'smart-custom-fields' ),
158 'menu_name' => __( 'Smart Custom Fields', 'smart-custom-fields' ),
159 'name_admin_bar' => __( 'Smart Custom Fields', 'smart-custom-fields' ),
160 'add_new' => __( 'Add New', 'smart-custom-fields' ),
161 'add_new_item' => __( 'Add New', 'smart-custom-fields' ),
162 'new_item' => __( 'New Field', 'smart-custom-fields' ),
163 'edit_item' => __( 'Edit Field', 'smart-custom-fields' ),
164 'view_item' => __( 'View Field', 'smart-custom-fields' ),
165 'all_items' => __( 'All Fields', 'smart-custom-fields' ),
166 'search_items' => __( 'Search Fields', 'smart-custom-fields' ),
167 'parent_item_colon' => __( 'Parent Fields:', 'smart-custom-fields' ),
168 'not_found' => __( 'No Fields found.', 'smart-custom-fields' ),
169 'not_found_in_trash' => __( 'No Fields found in Trash.', 'smart-custom-fields' )
170 );
171 register_post_type(
172 SCF_Config::NAME,
173 array(
174 'label' => 'Smart Custom Fields',
175 'labels' => $labels,
176 'public' => false,
177 'show_ui' => true,
178 'capability_type' => 'page',
179 'supports' => array( 'title', 'page-attributes' ),
180 'show_in_menu' => false,
181 )
182 );
183 }
184
185 /**
186 * Ajax リクエストのときに発火させたい処理をフックさせる
187 */
188 public function ajax_request() {
189 $Ajax = new Smart_Custom_Fields_Ajax();
190 }
191
192 /**
193 * 管理画面にメニューを追加
194 */
195 public function admin_menu() {
196 add_menu_page(
197 'Smart Custom Fields',
198 'Smart Custom Fields',
199 'manage_options',
200 'edit.php?post_type=' . SCF_Config::NAME,
201 false,
202 false,
203 '80.026'
204 );
205 add_submenu_page(
206 'edit.php?post_type=' . SCF_Config::NAME,
207 __( 'Add New', 'smart-custom-fields' ),
208 __( 'Add New', 'smart-custom-fields' ),
209 'manage_options',
210 'post-new.php?post_type=' . SCF_Config::NAME
211 );
212 }
213
214 /**
215 * 編集画面でその投稿のIDを取得
216 *
217 * @return int
218 */
219 protected function get_post_id_in_admin() {
220 $post_id = false;
221 if ( !empty( $_GET['post'] ) ) {
222 $post_id = $_GET['post'];
223 } elseif ( !empty( $_POST['post_ID'] ) ) {
224 $post_id = $_POST['post_ID'];
225 }
226 return $post_id;
227 }
228
229 /**
230 * プロフィール、ユーザー編集画面でそのユーザーのIDを取得
231 *
232 * @return int
233 */
234 protected function get_user_id_in_admin() {
235 $screen = get_current_screen();
236 $user_id = false;
237 if ( !empty( $_GET['user_id'] ) ) {
238 $user_id = $_GET['user_id'];
239 } elseif ( !empty( $_POST['user_id'] ) ) {
240 $user_id = $_POST['user_id'];
241 } elseif ( $screen->id === 'profile' ) {
242 $current_user = wp_get_current_user();
243 $user_id = $current_user->ID;
244 }
245 return $user_id;
246 }
247
248 /**
249 * ターム編集画面でそのタームのIDを取得
250 *
251 * @return int
252 */
253 protected function get_term_id_in_admin() {
254 $term_id = false;
255 if ( !empty( $_GET['tag_ID'] ) ) {
256 $term_id = $_GET['tag_ID'];
257 } elseif ( !empty( $_POST['tag_ID'] ) ) {
258 $term_id = $_POST['tag_ID'];
259 }
260 return $term_id;
261 }
262 }
263 new Smart_Custom_Fields();
264