| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package smart-custom-fields |
| 4 |
* @author inc2734 |
| 5 |
* @license GPL-2.0+ |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Smart_Custom_Fields_Controller_Editor class. |
| 10 |
*/ |
| 11 |
class Smart_Custom_Fields_Controller_Editor extends Smart_Custom_Fields_Controller_Base { |
| 12 |
|
| 13 |
/** |
| 14 |
* __construct |
| 15 |
*/ |
| 16 |
public function __construct() { |
| 17 |
parent::__construct(); |
| 18 |
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 10, 2 ); |
| 19 |
add_action( 'save_post', array( $this, 'save_post' ) ); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Displaying custom fields in post edit page. |
| 24 |
* |
| 25 |
* @param string $post_type Post type. |
| 26 |
* @param WP_Post $post WP_Post object. |
| 27 |
*/ |
| 28 |
public function add_meta_boxes( $post_type, $post ) { |
| 29 |
$settings = SCF::get_settings( $post ); |
| 30 |
foreach ( $settings as $setting ) { |
| 31 |
add_meta_box( |
| 32 |
SCF_Config::PREFIX . 'custom-field-' . $setting->get_id(), |
| 33 |
$setting->get_title(), |
| 34 |
array( $this, 'display_meta_box' ), |
| 35 |
$post_type, |
| 36 |
'normal', |
| 37 |
'default', |
| 38 |
$setting->get_groups() |
| 39 |
); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Saving meta data from custom fields in post edit page. |
| 45 |
* |
| 46 |
* @param int $post_id Post id. |
| 47 |
*/ |
| 48 |
public function save_post( $post_id ) { |
| 49 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
if ( ! filter_input( INPUT_POST, SCF_Config::NAME, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ) ) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
$this->save( filter_input_array( INPUT_POST ), get_post( $post_id ) ); |
| 62 |
} |
| 63 |
} |
| 64 |
|