Unyson icon indicating copy to clipboard operation
Unyson copied to clipboard

Save Same Options In Builder and Customizer

Open NwpDeveloper opened this issue 10 years ago • 21 comments

Is there a way to save the same option in page-builder and customizer? Or can you direct me to which methods are responsible for saving in page-builder and customizer?

NwpDeveloper avatar Mar 25 '16 17:03 NwpDeveloper

Do you mean to have an option in page-builder, when it's value is updated to update another option value in customizer options, and vice versa?

ghost avatar Mar 25 '16 17:03 ghost

Yes. Are there fw methods or hooks to alter save_post and customize_save_after to conditionally update both options?

NwpDeveloper avatar Mar 25 '16 18:03 NwpDeveloper

There is fw-storage option parameter. But at the moment it doesn't work with customizer options (only with post options), I will fix that soon.

https://github.com/ThemeFuse/Unyson/issues/1352#issuecomment-196378312

ghost avatar Mar 25 '16 19:03 ghost

It already works:

  1. Add in Theme Settings options

    'customizer-option-id' => array(
        'type' => 'switch',
    ),
    
  2. Add in Post options

    'post-option-id' => array(
        'type' => 'switch',
        'fw-storage' => array(
            'type' => 'wp-option',
            'wp-option' => 'theme_mods_'. get_option( 'stylesheet' ),
            'key' => 'fw_options/customizer-option-id',
        ),
    ),
    

ghost avatar Mar 28 '16 11:03 ghost

I'm unable to get this working in 2.5.0.

  1. Added in customizer.php
'customizer-option-id' => array(
    'type' => 'switch',
),
  1. Added in shortcode options.php tried {theme}/framework-customizations/theme/options/post.php and page.php
'post-option-id' => array(
    'type' => 'switch',
    'fw-storage' => array(
        'type' => 'wp-option',
        'wp-option' => 'theme_mods_'. get_option( 'stylesheet' ),
        'key' => 'fw_options/customizer-option-id',
    ),
),

I get a switch that doesn't update in customizer. The switch also doesn't show the ON and OFF values in customizer, which is odd since they are the defaults.

NwpDeveloper avatar Mar 28 '16 16:03 NwpDeveloper

Can you give me admin access to your site? (my email moldcraft[at]email.com)

ghost avatar Mar 28 '16 16:03 ghost

Also, does this fw-storage feature work for shortcode options #838?

For example: If the option_id is the same whether you put the option in customizer, shortcode option, page, post it will write the value data to all relevant db tables?

So, If I have save the same option id in customizer it will save it any place that option_id is used?

NwpDeveloper avatar Mar 28 '16 16:03 NwpDeveloper

I'm testing this locally and I don't believe I have the ability to give admin access.

NwpDeveloper avatar Mar 28 '16 16:03 NwpDeveloper

https://github.com/ThemeFuse/Unyson/issues/1388#issuecomment-202479776

Yes

ghost avatar Mar 28 '16 16:03 ghost

https://github.com/ThemeFuse/Unyson/issues/1388#issuecomment-202481705

Then I don't know how to help. I tested the above code on my localhost and it works.

ghost avatar Mar 28 '16 16:03 ghost

Does this feature work on a multi site?

I'm getting really odd behavior where saving in page-builder is completely deleting the theme_mods option_value. Sometimes it replaces the option_value column with just the switch value of 1 (not json encoded).

Then I have to clear theme_mods in wp_options for anything to write to that row.

If I save the switch in customizer it will save in theme_mods json encoded with key fw_option, but if I check postmeta fw_options post-option-id doesn't change for the current post_id or the last revision.

NwpDeveloper avatar Mar 28 '16 20:03 NwpDeveloper

I haven't tested it on multisite

ghost avatar Mar 28 '16 20:03 ghost

Ok. I'm duplicating this behavior on a single site with 2015 theme. Are we 100% sure fw-storage in 2.5.0 is stable?

Here are the steps I produced the issue.

  1. Stock WP single site
  2. 2015 Theme
  3. Added {theme}/inc/init.php
  4. Added {theme}/options/customizer.php
$options = array(
   'customizer-option-id' => array(
      'type' => 'switch',
    ),
);
  1. Added {theme}/options/posts/page.php
$options = array(
   'post-option-id' => array(
      'type' => 'switch',
      'fw-storage' => array(
        'type' => 'wp-option',
        'wp-option' => 'theme_mods_'. get_option( 'stylesheet' ),
        'key' => 'fw_options/customizer-option-id',
    ),
),
),

Result:

  1. Save switch ON in customizer

wp_options>theme_mods_twenty_fifteen:

a:1:{s:10:"fw_options";a:1:{s:20:"customizer-option-id";b:1;}}

wp_postmeta>post_id:

a:5:{s:12:"page-builder";a:4:{s:4:"json";s:2:"[]";s:14:"corrected_json";s:2:"[]";s:18:"shortcode_notation";s:0:"";s:14:"builder_active";b:1;}s:14:"post-option-id";b:0;}
  1. Save switch ON in page-builder

wp_options>theme_mods_twenty_fifteen: 1

wp_postmeta>post_id:

a:8:{s:12:"page-builder";a:4:{s:4:"json";s:2:"[]";s:14:"corrected_json";s:2:"[]";s:18:"shortcode_notation";s:0:"";s:14:"builder_active";b:1;}s:14:"post-option-id";b:0;}

Now if I save from page-builder it will write 1 to theme_mods for ON and it clears theme_mods for OFF. And if theme_mods has an option_value of 1 then any customizer changes will not write to the db.

NwpDeveloper avatar Mar 28 '16 21:03 NwpDeveloper

Oh, sorry, I forgot that the 'key' parameter in 'fw-storage' is not included in 2.5.0, it will be included in 2.5.1, so download Unyson from Github.

ghost avatar Mar 29 '16 10:03 ghost

Nice! What are the option parameters to save customizer option to page-builder? https://github.com/ThemeFuse/Unyson/issues/1388#issuecomment-201440068

I tried reversing the options and using 'customizer-option-id' => array( 'type' => 'switch', 'fw-storage' => array( 'type' => 'post-meta', 'post-id' => 11, 'post-meta' => 'post-option-id', ), ), but that still saves customizer-option-id to theme_mods.

NwpDeveloper avatar Mar 29 '16 18:03 NwpDeveloper

save customizer option to page-builder

It's not possible. Currently only post options support the fw-storage parameter.

You need this functionality or just testing?

ghost avatar Mar 29 '16 18:03 ghost

Ah Ok. I thought that was the point of #838.

What is class FW_Option_Storage_Type_Post_Meta for? I thought fw-storage is used to store shortcode options, page, post, customizer etc. and then write it to the specified db location under the specified id.

The desired behavior is:

  1. Customizer to Shortcode/Page/Posts Change customizer option Affects shortcode/page/post options Saves in wp_post_meta, wp_posts (or as specified in option)

  2. Shortcode/Page/Posts to Customizer Change shortcode/page/post options Affects customizer option Saves in wp_options (or as specified in option)

Does fw-storage work for shortcodes? When I put fw-storage with type wp-option inside a shortcodes options.php it doesn't work.

I'm just testing now, but I think this functionality is necessary.

NwpDeveloper avatar Mar 29 '16 18:03 NwpDeveloper

What is class FW_Option_Storage_Type_Post_Meta for?

To handle this:

array(
    'type' => 'switch',
    'fw-storage' => array(
        'type' => 'post-meta', // <---
        // ...
    ),
)

When this functionality will be necessary, let me know and I will implemented, at the moment I don't want to spend time for it.

At the moment (2) works.

customizer(native) ---> wp_option <--- postOptions(fw-storage)

ghost avatar Mar 29 '16 19:03 ghost

Does the current functionality work for shortcodes or just {theme}/framework-customizations/theme/options/posts/?

NwpDeveloper avatar Mar 29 '16 19:03 NwpDeveloper

It works with shortcodes. PageBuilder is an option located in post options, and fw-storage is processed recursive til the shortcodes options.

ghost avatar Mar 29 '16 19:03 ghost