| 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.3.0 |
| 7 |
* Author: Takashi Kitajima |
| 8 |
* Author URI: http://2inc.org |
| 9 |
* Created: October 9, 2014 |
| 10 |
* Modified: March 19, 2015 |
| 11 |
* Text Domain: smart-custom-fields |
| 12 |
* Domain Path: /languages |
| 13 |
* License: GPLv2 |
| 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 |
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/class.scf.php'; |
| 44 |
new Smart_Custom_Fields_Revisions(); |
| 45 |
|
| 46 |
foreach ( glob( plugin_dir_path( __FILE__ ) . 'classes/fields/*.php' ) as $form_item ) { |
| 47 |
include_once $form_item; |
| 48 |
$basename = basename( $form_item, '.php' ); |
| 49 |
$classname = preg_replace( '/^class\.field\-(.+)$/', 'Smart_Custom_Fields_Field_$1', $basename ); |
| 50 |
if ( class_exists( $classname ) ) { |
| 51 |
new $classname(); |
| 52 |
} |
| 53 |
} |
| 54 |
do_action( SCF_Config::PREFIX . 'fields-loaded' ); |
| 55 |
|
| 56 |
add_action( 'init' , array( $this, 'register_post_type' ) ); |
| 57 |
add_action( 'admin_menu' , array( $this, 'admin_menu' ) ); |
| 58 |
add_action( 'current_screen', array( $this, 'current_screen' ) ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* アンインストール時の処理 |
| 63 |
*/ |
| 64 |
public static function uninstall() { |
| 65 |
$cf_posts = get_posts( array( |
| 66 |
'post_type' => SCF_Config::NAME, |
| 67 |
'posts_per_page' => -1, |
| 68 |
'post_status' => 'any', |
| 69 |
) ); |
| 70 |
foreach ( $cf_posts as $post ) { |
| 71 |
wp_delete_post( $post->ID, true ); |
| 72 |
} |
| 73 |
delete_post_meta_by_key( SCF_Config::PREFIX . 'repeat-multiple-data' ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* 各管理画面の実行 |
| 78 |
* |
| 79 |
* @param WP_Screen $screen |
| 80 |
*/ |
| 81 |
public function current_screen( $screen ) { |
| 82 |
// 一覧画面 |
| 83 |
if ( $screen->id === 'edit-' . SCF_Config::NAME ) { |
| 84 |
} |
| 85 |
// 新規作成・編集画面 |
| 86 |
elseif ( $screen->id === SCF_Config::NAME ) { |
| 87 |
require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.settings.php'; |
| 88 |
new Smart_Custom_Fields_Controller_Settings(); |
| 89 |
} |
| 90 |
// その他の新規作成・編集画面 |
| 91 |
elseif ( in_array( $screen->id, get_post_types() ) ) { |
| 92 |
$post_id = $this->get_post_id_in_admin(); |
| 93 |
$Post = new stdClass(); |
| 94 |
$Post->ID = $post_id; |
| 95 |
$Post->post_type = $screen->id; |
| 96 |
if ( SCF::get_settings( new WP_Post( $Post ) ) ) { |
| 97 |
require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.editor.php'; |
| 98 |
new Smart_Custom_Fields_Revisions(); |
| 99 |
new Smart_Custom_Fields_Controller_Editor(); |
| 100 |
} |
| 101 |
} |
| 102 |
// プロフィール編集画面 |
| 103 |
elseif ( in_array( $screen->id, array( 'profile', 'user-edit' ) ) ) { |
| 104 |
$user_id = $this->get_user_id_in_admin(); |
| 105 |
$user_data = get_userdata( $user_id ); |
| 106 |
$roles[0] = false; |
| 107 |
if ( $user_data ) { |
| 108 |
$roles = $user_data->roles; |
| 109 |
} |
| 110 |
if ( SCF::get_settings( get_userdata( $user_id ) ) ) { |
| 111 |
require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.editor.php'; |
| 112 |
require_once plugin_dir_path( __FILE__ ) . 'classes/controller/class.profile.php'; |
| 113 |
new Smart_Custom_Fields_Controller_Profile(); |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* カスタム投稿タイプの登録。メニュー表示は別メソッドで実行 |
| 120 |
*/ |
| 121 |
public function register_post_type() { |
| 122 |
$labels = array( |
| 123 |
'name' => __( 'Smart Custom Fields', 'smart-custom-fields' ), |
| 124 |
'menu_name' => __( 'Smart Custom Fields', 'smart-custom-fields' ), |
| 125 |
'name_admin_bar' => __( 'Smart Custom Fields', 'smart-custom-fields' ), |
| 126 |
'add_new' => __( 'Add New', 'smart-custom-fields' ), |
| 127 |
'add_new_item' => __( 'Add New', 'smart-custom-fields' ), |
| 128 |
'new_item' => __( 'New Field', 'smart-custom-fields' ), |
| 129 |
'edit_item' => __( 'Edit Field', 'smart-custom-fields' ), |
| 130 |
'view_item' => __( 'View Field', 'smart-custom-fields' ), |
| 131 |
'all_items' => __( 'All Fields', 'smart-custom-fields' ), |
| 132 |
'search_items' => __( 'Search Fields', 'smart-custom-fields' ), |
| 133 |
'parent_item_colon' => __( 'Parent Fields:', 'smart-custom-fields' ), |
| 134 |
'not_found' => __( 'No Fields found.', 'smart-custom-fields' ), |
| 135 |
'not_found_in_trash' => __( 'No Fields found in Trash.', 'smart-custom-fields' ) |
| 136 |
); |
| 137 |
register_post_type( |
| 138 |
SCF_Config::NAME, |
| 139 |
array( |
| 140 |
'label' => 'Smart Custom Fields', |
| 141 |
'labels' => $labels, |
| 142 |
'public' => false, |
| 143 |
'show_ui' => true, |
| 144 |
'capability_type' => 'page', |
| 145 |
'supports' => array( 'title', 'page-attributes' ), |
| 146 |
'show_in_menu' => false, |
| 147 |
) |
| 148 |
); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* 管理画面にメニューを追加 |
| 153 |
*/ |
| 154 |
public function admin_menu() { |
| 155 |
add_menu_page( |
| 156 |
'Smart Custom Fields', |
| 157 |
'Smart Custom Fields', |
| 158 |
'manage_options', |
| 159 |
'edit.php?post_type=' . SCF_Config::NAME, |
| 160 |
false, |
| 161 |
false, |
| 162 |
'80.026' |
| 163 |
); |
| 164 |
add_submenu_page( |
| 165 |
'edit.php?post_type=' . SCF_Config::NAME, |
| 166 |
__( 'Add New', 'smart-custom-fields' ), |
| 167 |
__( 'Add New', 'smart-custom-fields' ), |
| 168 |
'manage_options', |
| 169 |
'post-new.php?post_type=' . SCF_Config::NAME |
| 170 |
); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* 編集画面でその投稿のIDを取得 |
| 175 |
* |
| 176 |
* @return int |
| 177 |
*/ |
| 178 |
protected function get_post_id_in_admin() { |
| 179 |
$post_id = false; |
| 180 |
if ( !empty( $_GET['post'] ) ) { |
| 181 |
$post_id = $_GET['post']; |
| 182 |
} elseif ( !empty( $_POST['post_ID'] ) ) { |
| 183 |
$post_id = $_POST['post_ID']; |
| 184 |
} |
| 185 |
return $post_id; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* プロフィール、ユーザー編集画面でそのユーザーのIDを取得 |
| 190 |
* |
| 191 |
* @return int |
| 192 |
*/ |
| 193 |
protected function get_user_id_in_admin() { |
| 194 |
$screen = get_current_screen(); |
| 195 |
$user_id = false; |
| 196 |
if ( !empty( $_GET['user_id'] ) ) { |
| 197 |
$user_id = $_GET['user_id']; |
| 198 |
} elseif ( !empty( $_POST['user_id'] ) ) { |
| 199 |
$user_id = $_POST['user_id']; |
| 200 |
} elseif ( $screen->id === 'profile' ) { |
| 201 |
$current_user = wp_get_current_user(); |
| 202 |
$user_id = $current_user->ID; |
| 203 |
} |
| 204 |
return $user_id; |
| 205 |
} |
| 206 |
} |
| 207 |
new Smart_Custom_Fields(); |
| 208 |
|