wordpress-fieldmanager icon indicating copy to clipboard operation
wordpress-fieldmanager copied to clipboard

Bug on cron save when using $only_save_to_taxonomy and hooking onto "init"

Open mboynes opened this issue 9 years ago • 1 comments

On an options field using a Datasource_Term with $only_save_to_taxonomy = true, cron tasks (e.g. publishing a scheduled post) will overwrite the term relationships with empty values. If the field is defined on a context-specific action (e.g. fm_post_post), this won't occur because the field won't be defined; therefore, this would only happen if the field were defined on 'init' or some other global action.

The main issue here is that on cron, the values are loaded from post meta, which is limited because there are other modes of storage, e.g. terms and post_parent. The other factor at play is that $only_save_to_taxonomy ignores $skip_save (see #362), because the context does set $skip_save = true.

The following code will help replicate this issue:

function my_fm_terms() {
    $fm = new Fieldmanager_Group( array(
        'name' => 'terms',
        'children' => array(
            'categories' => new Fieldmanager_Select( array(
                'label' => __( 'Categories', 'fm-test' ),
                'remove_default_meta_boxes' => true,
                'datasource' => new Fieldmanager_Datasource_Term( array(
                    'taxonomy' => 'category',
                    'only_save_to_taxonomy' => true,
                ) ),
            ) ),
            'tags' => new Fieldmanager_Select( array(
                'label' => __( 'Tags', 'fm-test' ),
                'remove_default_meta_boxes' => true,
                'datasource' => new Fieldmanager_Datasource_Term( array(
                    'taxonomy' => 'post_tag',
                    'only_save_to_taxonomy' => true,
                ) ),
            ) ),
        ),
    ) );
    $fm->add_meta_box( __( 'Terms', 'fm-test' ), array( 'post' ) );
}
add_action( 'init', 'my_fm_terms' );

mboynes avatar Jul 10 '15 17:07 mboynes

The problem here is that the data is loaded in order to go through the save process, even though it's not being saved. However, not all data is loaded (terms aren't, post_parent isn't, etc.). Even if all data were loaded, we wouldn't want FM to automatically write anything.

Options:

  1. Call _doing_it_wrong() if a field is registered on init
  2. Find everywhere that an alternate data storage is written and check if DOING_CRON
  3. Properly load all the data and let it get saved

It's a tad ugly, but I'm in favor of option 2.

mboynes avatar Feb 20 '16 16:02 mboynes