false, 'help' => false ), array(), array() ); } function initialize() { // Initialize number for field name attributes. $this->number = 1; $this->post_types = array(); $this->widget_form_fields = array(); add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) ); add_action( 'save_post', array( $this, 'save_widget_post_meta' ), 10, 3 ); } /** * This handles the 'add_meta_boxes' action. It merges widget fields into the form_options array before adding the * meta box * * @param $post_type */ public function add_meta_boxes( $post_type ) { $this->form_options = array(); foreach ( $this->widget_form_fields as $widget_id => $post_type_form_fields ) { foreach( $post_type_form_fields as $form_fields ) { if ( in_array( 'all', $form_fields['post_types'] ) || in_array( $post_type, $form_fields['post_types'] ) ) { foreach ( $form_fields['fields'] as $field_name => $field ) { $this->form_options[$widget_id . '_' . $field_name] = $field; } } } } if ( ! empty( $this->form_options ) ) { add_meta_box( 'siteorigin-widgets-meta-box', __( 'Widgets Bundle Post Meta Data', 'so-widgets-bundle' ), array( $this, 'render_widgets_meta_box' ), $post_type, 'advanced' ); } } /** * This is the callback used by add_meta_box to render the widgets meta box form and enqueue any necessary scripts. * * @param $post */ public function render_widgets_meta_box( $post ) { wp_enqueue_script( 'sow-meta-box-manager-js', plugin_dir_url(SOW_BUNDLE_BASE_FILE).'base/js/meta-box-manager' . SOW_BUNDLE_JS_SUFFIX . '.js', array( 'jquery' ), SOW_BUNDLE_VERSION, true ); $widget_post_meta = get_post_meta( $post->ID, self::POST_META_KEY, true ); $this->form( $widget_post_meta ); ?> post_types ) ) { $this->post_types[] = $post_type; } } if( ! isset( $this->widget_form_fields[$widget_id] ) ) { $this->widget_form_fields[$widget_id] = array(); } $this->widget_form_fields[$widget_id][] = array( 'post_types' => $post_types, 'fields' => $fields ); } /** * This handles the 'save_post' action. It checks for a nonce, checks user permissions, and filters the input data * and decodes it from the JSON format before storing it in the post's meta data. * * @param $post_id */ public function save_widget_post_meta( $post_id ) { if ( empty( $_POST['_widget_post_meta_nonce'] ) || !wp_verify_nonce( $_POST['_widget_post_meta_nonce'], 'widget_post_meta_save' ) ) return; if ( !current_user_can( 'edit_post', $post_id ) ) return; $widget_post_meta = isset( $_POST['widget_post_meta'] ) ? stripslashes_deep( $_POST['widget_post_meta'] ) : ''; $widget_post_meta = json_decode( $widget_post_meta, true); update_post_meta( $post_id, self::POST_META_KEY, $widget_post_meta ); } /** * This function is used to retrieve a widget's post meta data. * * @param $post_id string The id of the post for which the meta data is stored. * @param $widget_id string The id of the widget for which the meta data is stored. * @param $meta_key string The key of the meta data value which is to be retrieved. * * @return mixed An empty string if the meta data is not found, else the meta data in whatever format it was stored. */ public function get_widget_post_meta( $post_id, $widget_id, $meta_key ) { $widget_post_meta = get_post_meta( $post_id, self::POST_META_KEY, true ); if( empty( $widget_post_meta ) ) return ''; $widget_post_meta_field = $widget_id . '_' . $meta_key; if( ! isset( $widget_post_meta[$widget_post_meta_field] ) ) return ''; return $widget_post_meta[$widget_post_meta_field]; } }