| 1 |
<?php |
| 2 |
/** |
| 3 |
* FlashToolkit Meta Boxes |
| 4 |
* |
| 5 |
* Sets up the write panels used by custom post types. |
| 6 |
* |
| 7 |
* @class FT_Admin_Meta_Boxes |
| 8 |
* @version 1.1.0 |
| 9 |
* @package FlashToolkit/Admin/Meta Boxes |
| 10 |
* @category Admin |
| 11 |
* @author ThemeGrill |
| 12 |
*/ |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* FT_Admin_Meta_Boxes Class |
| 20 |
*/ |
| 21 |
class FT_Admin_Meta_Boxes { |
| 22 |
|
| 23 |
/** |
| 24 |
* Is meta boxes saved once? |
| 25 |
* |
| 26 |
* @var boolean |
| 27 |
*/ |
| 28 |
private static $saved_meta_boxes = false; |
| 29 |
|
| 30 |
/** |
| 31 |
* Meta box error messages. |
| 32 |
* |
| 33 |
* @var array |
| 34 |
*/ |
| 35 |
public static $meta_box_errors = array(); |
| 36 |
|
| 37 |
/** |
| 38 |
* Hook in tabs. |
| 39 |
*/ |
| 40 |
public function __construct() { |
| 41 |
add_action( 'add_meta_boxes', array( $this, 'remove_meta_boxes' ), 10 ); |
| 42 |
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 20 ); |
| 43 |
add_action( 'save_post', array( $this, 'save_meta_boxes' ), 1, 2 ); |
| 44 |
|
| 45 |
// Save Portfolio Meta Boxes |
| 46 |
add_action( 'flash_toolkit_process_portfolio_meta', 'FT_Meta_Box_Portfolio_Data::save', 10, 2 ); |
| 47 |
|
| 48 |
// Save Page Meta Boxes |
| 49 |
add_action( 'flash_toolkit_process_page_meta', 'FT_Meta_Box_Pageoptions_Data::save', 10, 2 ); |
| 50 |
|
| 51 |
// Save Layout Meta Boxes |
| 52 |
add_action( 'flash_toolkit_process_layout_meta', 'FT_Meta_Box_Layout_Data::save', 10, 2 ); |
| 53 |
|
| 54 |
// Error handling (for showing errors from meta boxes on next page load) |
| 55 |
add_action( 'admin_notices', array( $this, 'output_errors' ) ); |
| 56 |
add_action( 'shutdown', array( $this, 'save_errors' ) ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Add an error message. |
| 61 |
* @param string $text |
| 62 |
*/ |
| 63 |
public static function add_error( $text ) { |
| 64 |
self::$meta_box_errors[] = $text; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Save errors to an option. |
| 69 |
*/ |
| 70 |
public function save_errors() { |
| 71 |
update_option( 'flash_toolkit_meta_box_errors', self::$meta_box_errors ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Show any stored error messages. |
| 76 |
*/ |
| 77 |
public function output_errors() { |
| 78 |
$errors = maybe_unserialize( get_option( 'flash_toolkit_meta_box_errors' ) ); |
| 79 |
|
| 80 |
if ( ! empty( $errors ) ) { |
| 81 |
|
| 82 |
echo '<div id="flash-toolkit_errors" class="error notice is-dismissible">'; |
| 83 |
|
| 84 |
foreach ( $errors as $error ) { |
| 85 |
echo '<p>' . wp_kses_post( $error ) . '</p>'; |
| 86 |
} |
| 87 |
|
| 88 |
echo '</div>'; |
| 89 |
|
| 90 |
// Clear |
| 91 |
delete_option( 'flash_toolkit_meta_box_errors' ); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Add SI Meta boxes. |
| 97 |
*/ |
| 98 |
public function add_meta_boxes() { |
| 99 |
// Portfolio |
| 100 |
add_meta_box( 'flash-toolkit-portfolio-data', __( 'Portfolio Data', 'flash-toolkit' ), 'FT_Meta_Box_Portfolio_Data::output', 'portfolio', 'normal', 'high' ); |
| 101 |
|
| 102 |
// Page Header |
| 103 |
if ( is_flash_pro_active() ) { |
| 104 |
add_meta_box( 'flash-toolkit-pageheader-data', __( 'Page Options', 'flash-toolkit' ), 'FT_Meta_Box_Pageoptions_Data::output', 'page', 'normal', 'high' ); |
| 105 |
} |
| 106 |
|
| 107 |
// Layouts |
| 108 |
foreach ( flash_toolkit_get_layout_supported_screens() as $post_type ) { |
| 109 |
if ( post_type_exists( $post_type ) && is_flash_pro_active() ) { |
| 110 |
$post_type_object = get_post_type_object( $post_type ); |
| 111 |
add_meta_box( 'flash-toolkit-layout-data', sprintf( __( '%s Layout', 'flash-toolkit' ), $post_type_object->labels->singular_name ), 'FT_Meta_Box_Layout_Data::output', $post_type, 'side', 'default' ); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Remove bloat. |
| 118 |
*/ |
| 119 |
public function remove_meta_boxes() { |
| 120 |
global $post; |
| 121 |
|
| 122 |
if ( is_flash_pro_active() ) { |
| 123 |
remove_meta_box( 'page-layout', 'post', 'side' ); |
| 124 |
remove_meta_box( 'page-layout', 'page', 'side' ); |
| 125 |
remove_meta_box( 'header-transparency', 'page', 'side' ); |
| 126 |
} |
| 127 |
|
| 128 |
if ( 'portfolio' === $post->post_type && 0 === count( get_page_templates( $post ) ) ) { |
| 129 |
remove_meta_box( 'pageparentdiv', 'portfolio', 'side' ); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Check if we're saving, the trigger an action based on the post type. |
| 135 |
* @param int $post_id |
| 136 |
* @param object $post |
| 137 |
*/ |
| 138 |
public function save_meta_boxes( $post_id, $post ) { |
| 139 |
// $post_id and $post are required |
| 140 |
if ( empty( $post_id ) || empty( $post ) || self::$saved_meta_boxes ) { |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
// Don't save meta boxes for revisions or autosaves |
| 145 |
if ( defined( 'DOING_AUTOSAVE' ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) { |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
// Check the nonce |
| 150 |
if ( empty( $_POST['flash_toolkit_meta_nonce'] ) || ! wp_verify_nonce( $_POST['flash_toolkit_meta_nonce'], 'flash_toolkit_save_data' ) ) { |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
// Check the post being saved == the $post_id to prevent triggering this call for other save_post events |
| 155 |
if ( empty( $_POST['post_ID'] ) || $_POST['post_ID'] != $post_id ) { |
| 156 |
return; |
| 157 |
} |
| 158 |
|
| 159 |
// Check user has permission to edit |
| 160 |
if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
// We need this save event to run once to avoid potential endless loops. This would have been perfect: |
| 165 |
self::$saved_meta_boxes = true; |
| 166 |
|
| 167 |
// Check the post type |
| 168 |
if ( in_array( $post->post_type, array( 'portfolio' ) ) ) { |
| 169 |
do_action( 'flash_toolkit_process_' . $post->post_type . '_meta', $post_id, $post ); |
| 170 |
} |
| 171 |
|
| 172 |
// Trigger action |
| 173 |
$process_actions = array( 'layout', 'page' ); |
| 174 |
foreach ( $process_actions as $process_action ) { |
| 175 |
do_action( 'flash_toolkit_process_' . $process_action . '_meta', $post_id, $post ); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
new FT_Admin_Meta_Boxes(); |
| 181 |
|