/** * Displaying custom fields in post edit page * * @param string $post_type * @param WP_Post $post */ public function add_meta_boxes($post_type, $post) { $settings = SCF::get_settings($post); foreach ($settings as $Setting) { add_meta_box(SCF_Config::PREFIX . 'custom-field-' . $Setting->get_id(), $Setting->get_title(), array($this, 'display_meta_box'), $post_type, 'normal', 'default', $Setting->get_groups()); } }
/** * リビジョンデータを保存 * *_post_meta はリビジョンIDのときに自動的に本物IDに変換して処理してしまうので、*_metadata を使うこと * * @param int $post_id リビジョンの投稿ID */ public function wp_insert_post($post_id) { if (!isset($_POST[SCF_Config::NAME])) { return; } if (!wp_is_post_revision($post_id)) { return; } $settings = SCF::get_settings(get_post($post_id)); if (!$settings) { return; } check_admin_referer(SCF_Config::NAME . '-fields', SCF_Config::PREFIX . 'fields-nonce'); $Meta = new Smart_Custom_Fields_Meta(get_post($post_id)); $Meta->save($_POST); }
/** * カスタムフィールドを表示 * * @param object $term */ public function edit_form_fields($term) { $settings = SCF::get_settings($term); foreach ($settings as $Setting) { $callback_args['args'] = $Setting->get_groups(); ?> <table class="form-table"> <tr> <th scope="row"><?php echo esc_html($Setting->get_title()); ?> </th> <td><?php $this->display_meta_box($term, $callback_args); ?> </td> </tr> </table> <?php } }
/** * カスタムフィールドを表示 * * @param WP_User $user */ public function user_profile($user) { printf('<h3>%s</h3>', esc_html__('Custom Fields', 'smart-custom-fields')); $settings = SCF::get_settings($user); foreach ($settings as $Setting) { $callback_args['args'] = $Setting->get_groups(); ?> <table class="form-table"> <tr> <th scope="row"><?php echo esc_html($Setting->get_title()); ?> </th> <td><?php $this->display_meta_box($user, $callback_args); ?> </td> </tr> </table> <?php } }
/** * 渡されたリビジョンからデータをリストア * * @param WP_Post $revision */ public function restore($revision) { switch ($this->meta_type) { case 'post': $object = get_post($this->id); break; default: $object = null; } if (is_null($object) || !is_a($revision, 'WP_Post')) { return; } $settings = SCF::get_settings($object); foreach ($settings as $Setting) { $fields = $Setting->get_fields(); foreach ($fields as $Field) { $field_name = $Field->get('name'); $this->delete($field_name); $value = SCF::get($field_name, $revision->ID); if (is_array($value)) { foreach ($value as $val) { if (is_array($val)) { foreach ($val as $v) { // ループ内複数値項目 $this->add($field_name, $v); } } else { // ループ内単一項目 or ループ外複数値項目 $this->add($field_name, $val); } } } else { // ループ外単一項目 $this->add($field_name, $value); } } } $repeat_multiple_data = SCF::get_repeat_multiple_data($revision); $repeat_multiple_data_name = SCF_Config::PREFIX . 'repeat-multiple-data'; $this->delete($repeat_multiple_data_name); $this->update($repeat_multiple_data_name, $repeat_multiple_data); }
/** * Run management screens * * @param WP_Screen $screen */ public function current_screen($screen) { // 一覧画面 if ($screen->id === 'edit-' . SCF_Config::NAME) { } elseif ($screen->id === SCF_Config::NAME) { require_once plugin_dir_path(__FILE__) . 'classes/controller/class.settings.php'; new Smart_Custom_Fields_Controller_Settings(); } elseif (in_array($screen->id, get_post_types())) { $post_id = $this->get_post_id_in_admin(); $Post = new stdClass(); $Post->ID = $post_id; $Post->post_type = $screen->id; if (SCF::get_settings(new WP_Post($Post))) { require_once plugin_dir_path(__FILE__) . 'classes/controller/class.controller-base.php'; require_once plugin_dir_path(__FILE__) . 'classes/controller/class.editor.php'; new Smart_Custom_Fields_Revisions(); new Smart_Custom_Fields_Controller_Editor(); } } elseif (in_array($screen->id, array('profile', 'user-edit'))) { $user_id = $this->get_user_id_in_admin(); $user_data = get_userdata($user_id); $roles[0] = false; if ($user_data) { $roles = $user_data->roles; } if (SCF::get_settings(get_userdata($user_id))) { require_once plugin_dir_path(__FILE__) . 'classes/controller/class.controller-base.php'; require_once plugin_dir_path(__FILE__) . 'classes/controller/class.profile.php'; new Smart_Custom_Fields_Controller_Profile(); } } elseif ($screen->taxonomy) { $term_id = $this->get_term_id_in_admin(); if ($term_id) { $term = get_term($term_id, $screen->taxonomy); if (SCF::get_settings($term)) { require_once plugin_dir_path(__FILE__) . 'classes/controller/class.controller-base.php'; require_once plugin_dir_path(__FILE__) . 'classes/controller/class.taxonomy.php'; new Smart_Custom_Fields_Controller_Taxonomy(); } } } }
/** * @group get_groups */ public function test_get_groups__Groupが返ってくるか() { $settings = SCF::get_settings(get_post($this->post_id)); foreach ($settings as $Setting) { foreach ($Setting->get_groups() as $Group) { // グループ名が数字の場合は null が返る $this->assertTrue(in_array($Group->get_name(), array(null, null, 'group-name-3', 'group-name-4'), true)); } } }