class Fieldmanager_Context_Post extends Fieldmanager_Context_Storable (View source)

Use fieldmanager to create meta boxes on the new/edit post screen and save data primarily to post meta.

Properties

Fieldmanager_Group $fm

Base field.

string $uniqid

Unique ID of the form. Used for forms that are not built into WordPress.

from  Fieldmanager_Context
array $save_keys

Store the meta keys this field saves to, to catch naming conflicts.

from  Fieldmanager_Context
array $taxonomies_to_save

Taxonomies to save to.

from  Fieldmanager_Context_Storable
string $title

Title of meta box.

array $post_types

What post types to render this meta box.

string $context

Context (normal, advanced, or side).

priority $priority

Priority (high, core, default, or low).

Methods

__construct(string $title, mixed $post_types, string $context = 'normal', string $priority = 'default', Fieldmanager_Field $fm = null)

Add a context to a fieldmanager.

string
nonce_key()

Get the nonce key for this field.

array
refresh_nonce(array $response)

Include a fresh nonce for this field in a response with refreshed nonces.

bool
is_valid_nonce()

Check if the nonce is valid. Returns false if the nonce is missing and throws an exception if it's invalid. If all goes well, returns true.

mixed
prepare_data(mixed $old_value = null, mixed $new_value = null, object $fm = null)

Prepare the data for saving.

string
render_field(array $args = array())

Render the field.

save(mixed $data = null)

Handle saving data for any context.

save_field(object $field, mixed $data)

Save a single field.

save_walk_children(object $field, mixed $data)

Walk group children to save when serialize_data => false.

mixed
load()

Handle loading data for any context.

mixed
load_field(object $field)

Load a single field.

mixed
load_walk_children(object $field)

Walk group children to load when serialize_data => false.

static string|array
sanitize_scalar_value(mixed $value)

Meta and options are always stored as strings, so it's best to ensure that scalar values get cast as strings to ensure that update_metadata() and update_option() are able to correctly compare the current value against the previous value.

string|array
get_data(int $post_id, string $meta_key, bool $single = false)

Get post meta.

bool|int
add_data(int $post_id, string $meta_key, mixed $meta_value, bool $unique = false)

Add post meta.

mixed
update_data(int $post_id, string $meta_key, mixed $meta_value, mixed $data_prev_value = '')

Update post meta.

bool
delete_data(int $post_id, string $meta_key, mixed $meta_value = '')

Delete post meta.

meta_box_render_callback()

Registers render_meta_box() via admin_init callback to add meta boxes to content types.

render_meta_box(WP_Post $post, null $form_struct = null)

Helper to attach element_markup() to add_meta_box(). Prints markup for post editor.

remove_meta_boxes()

Helper to remove all built-in meta boxes for all specified taxonomies on a post type.

array
save_fields_for_attachment(array $post, array $attachment)

Handles saving Fieldmanager data when the custom meta boxes are used on an attachment.

delegate_save_post(int $post_id)

Action handler to delegate to appropriate methods when a post is saved.

save_fields_for_post(int $post_id)

Takes $_POST data and saves it to, calling save_to_post_meta() once validation is passed.

save_fields_for_cron(int $post_id)

Process fields during a cron request, without saving data since the data isn't changing.

save_to_post_meta(int $post_id, array $data = null)

Helper to save an array of data to post meta.

static mixed
safe_update_post(array $args)

Helper for fieldmanager internals to save a post without worrying about infinite loops.

Details

__construct(string $title, mixed $post_types, string $context = 'normal', string $priority = 'default', Fieldmanager_Field $fm = null)

Add a context to a fieldmanager.

Parameters

string $title

Metabox title.

mixed $post_types

Metabox post types.

string $context

Metabox context (normal, advanced, or side).

string $priority

Metabox priority (high, core, default, or low).

Fieldmanager_Field $fm

Current field.

protected string nonce_key()

Get the nonce key for this field.

Return Value

string

array refresh_nonce(array $response)

Since: 1.3.0

Include a fresh nonce for this field in a response with refreshed nonces.

Parameters

array $response

Response data.

Return Value

array

Updated response data.

protected bool is_valid_nonce()

Check if the nonce is valid. Returns false if the nonce is missing and throws an exception if it's invalid. If all goes well, returns true.

Return Value

bool

protected mixed prepare_data(mixed $old_value = null, mixed $new_value = null, object $fm = null)

Prepare the data for saving.

Parameters

mixed $old_value

Optional. The previous value.

mixed $new_value

Optional. The new value for the field.

object $fm

Optional. The Fieldmanager_Field to prepare.

Return Value

mixed

The filtered and sanitized value, safe to save.

protected string render_field(array $args = array())

Render the field.

Parameters

array $args

{ Optional. Arguments to adjust the rendering behavior.

@type mixed $data The existing data to display with the field. If
                  absent, data will be loaded using
                  Fieldmanager_Context::_load().
@type boolean $echo Output if true, return if false. Default is true.

}

Return Value

string

if $args['echo'] == false.

protected save(mixed $data = null)

Handle saving data for any context.

Parameters

mixed $data

Data to save. Should be raw, e.g. POST data.

protected save_field(object $field, mixed $data)

Save a single field.

Parameters

object $field

Fieldmanager field.

mixed $data

Data to save.

Exceptions

FM_Developer_Exception

protected save_walk_children(object $field, mixed $data)

Walk group children to save when serialize_data => false.

Parameters

object $field

Fieldmanager field.

mixed $data

Data to save.

protected mixed load()

Handle loading data for any context.

Return Value

mixed

The loaded data.

protected mixed load_field(object $field)

Load a single field.

Parameters

object $field

The Fieldmanager field for which to load data.

Return Value

mixed

Data stored for that field in this context.

protected mixed load_walk_children(object $field)

Walk group children to load when serialize_data => false.

Parameters

object $field

Fieldmanager field for which to load data.

Return Value

mixed

Data stored for a singular field with serialized data, or array of data for a groups's children.

static string|array sanitize_scalar_value(mixed $value)

Since: 1.2.0

Meta and options are always stored as strings, so it's best to ensure that scalar values get cast as strings to ensure that update_metadata() and update_option() are able to correctly compare the current value against the previous value.

Parameters

mixed $value

Value being stored.

Return Value

string|array

If $value is scalar, a string is returned. Otherwise, $value returns untouched.

protected string|array get_data(int $post_id, string $meta_key, bool $single = false)

Get post meta.

Parameters

int $post_id

Post ID.

string $meta_key

Optional. The meta key to retrieve. By default, returns data for all keys. Default empty.

bool $single

Optional. If true, only returns the first value found for the given data_key. This won't apply to every context. Default is false.

Return Value

string|array

The stored data. If no data is found, should return an empty string (""). {\get_post_meta()}.

See also

get_post_meta().

protected bool|int add_data(int $post_id, string $meta_key, mixed $meta_value, bool $unique = false)

Add post meta.

Parameters

int $post_id

Post ID.

string $meta_key

Metadata name.

mixed $meta_value

Metadata value. Must be serializable if non-scalar.

bool $unique

Optional. If true, data will only be added if the object with the given $data_id doesn't already contain data for the given $data_key. This may not apply to every context. Default is false.

Return Value

bool|int

On success, should return the ID of the stored data (an integer, which will evaluate as true). If the $unique argument is set to true and data with the given key already exists, this should return false. {\add_post_meta()}.

See also

add_post_meta().

protected mixed update_data(int $post_id, string $meta_key, mixed $meta_value, mixed $data_prev_value = '')

Update post meta.

Parameters

int $post_id

Post ID.

string $meta_key

Metadata key.

mixed $meta_value

Metadata value. Must be serializable if non-scalar.

mixed $data_prev_value

Optional. Only update data if the previous value matches the data provided. This may not apply to every context.

Return Value

mixed

Should return data id if the data doesn't exist, otherwise should return true on success and false on failure. If the value passed to this method is the same as the stored value, this method should return false. {\update_post_meta()}.

See also

update_post_meta().

protected bool delete_data(int $post_id, string $meta_key, mixed $meta_value = '')

Delete post meta.

Parameters

int $post_id

Post ID.

string $meta_key

Metadata name.

mixed $meta_value

Optional. Metadata value. Must be serializable if non-scalar. Default empty.

Return Value

bool

False for failure. True for success. {\delete_post_meta()}.

See also

delete_post_meta().

meta_box_render_callback()

Registers render_meta_box() via admin_init callback to add meta boxes to content types.

render_meta_box(WP_Post $post, null $form_struct = null)

Helper to attach element_markup() to add_meta_box(). Prints markup for post editor.

Parameters

WP_Post $post

The post object.

null $form_struct

The structure of the form itself. Unused.

See also

http://codex.wordpress.org/Function_Reference/add_meta_box

remove_meta_boxes()

Helper to remove all built-in meta boxes for all specified taxonomies on a post type.

array save_fields_for_attachment(array $post, array $attachment)

Handles saving Fieldmanager data when the custom meta boxes are used on an attachment.

Calls save_fields_for_post with the post ID.

Parameters

array $post

The post fields.

array $attachment

The attachment fields.

Return Value

array

The post fields.

delegate_save_post(int $post_id)

Action handler to delegate to appropriate methods when a post is saved.

Parameters

int $post_id

The post ID.

save_fields_for_post(int $post_id)

Takes $_POST data and saves it to, calling save_to_post_meta() once validation is passed.

When using Fieldmanager as an API, do not call this function directly, call save_to_post_meta().

Parameters

int $post_id

The post ID.

save_fields_for_cron(int $post_id)

Process fields during a cron request, without saving data since the data isn't changing.

Parameters

int $post_id

The post ID.

save_to_post_meta(int $post_id, array $data = null)

Helper to save an array of data to post meta.

Parameters

int $post_id

The post ID.

array $data

The data to save.

static mixed safe_update_post(array $args)

Helper for fieldmanager internals to save a post without worrying about infinite loops.

Parameters

array $args

The post args to save.

Return Value

mixed

The result of the post update function.