Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

active_callback not working for checkboxes #1809

Closed
mapsteps opened this issue Feb 19, 2018 · 11 comments
Closed

active_callback not working for checkboxes #1809

mapsteps opened this issue Feb 19, 2018 · 11 comments
Milestone

Comments

@mapsteps
Copy link
Contributor

mapsteps commented Feb 19, 2018

Issue description:

Hi @aristath,

active callbacks don't seem to work for checkboxes any more.

Version used:

3.0.25

Code to reproduce the issue (config + field(s))

Kirki::add_field( 'theme', array(
	'type' => 'select',
	'settings' => 'my_setting',
	'label' => esc_attr__( 'My Label', 'textdomain' ),
	'section' => 'my_section',
	'default' => 'option1',
	'priority' => 0,
	'multiple' => 1,
	'choices' => array(
		'option1' => esc_attr__( 'Show', 'textdomain' ),
		'option2' => esc_attr__( 'Hide', 'textdomain' ),
	),
) );

Kirki::add_field( 'theme', array(
	'type' => 'checkbox',
	'settings' => 'my_setting2',
	'label' => esc_attr__( 'My Other Label', 'textdomain' ),
	'section' => 'my_section',
	'priority' => 2,
	'active_callback' => array(
		array(
		'setting' => 'my_setting',
		'operator' => '!==',
		'value' => 'option2',
		),
	)
) );
@sergeysedykh
Copy link

sergeysedykh commented Feb 21, 2018

Same here.

I was using 3.0.16 in my projects and it was fine.

In the latest version some fields are displayed even if they shouldn't according to the active_callback rules.

Nothing fancy with the config or the fields: trying to display / hide fields based on the switch (=checkbox) field value.

@two-points
Copy link

Even I am experiencing the same issue. It was working fine earlier.

@aristath
Copy link
Contributor

fixed in the dev branch, will be included in v3.0.26 👍

@sergeysedykh
Copy link

Thanks a lot, @aristath!

@sergeysedykh
Copy link

I've just checked the dev version and it's working fine again. Thank you!

@sergeysedykh
Copy link

I'm afraid, I was too quick in my conclusions. The commit fixed the issue partially.

There's still something wrong with the active_callback.

It's not only the matter of checkboxes, but applies to multiple active_callback conditions.


Kirki::add_field(
	'custom_theme_mod', array(
		'type'        => 'toggle',
		'settings'    => 'home_archive_default',
		'label'       => esc_html__( 'Default Settings', 'text-domain' ),
		'description' => esc_html__( 'You may change the default settings in Layout → ', 'text-domain' ) . esc_html__( 'Post Archive', 'text-domain' ),
		'section'     => 'home',
		'default'     => true,
		'priority'    => 10,
	)
);

Kirki::add_field(
	'custom_theme_mod', array(
		'type'        => 'radio-image',
		'settings'    => 'home_archive_type',
		'label'       => esc_html__( 'Type', 'text-domain' ),
		'section'     => 'home',
		'default'     => 'standard',
		'priority'    => 10,
		'choices'     => array(
			'standard'  => $template_directory_uri . '/images/layout-full.png',
			'list'      => $template_directory_uri . '/images/layout-list.png',
			'grid'      => $template_directory_uri . '/images/layout-grid.png',
			'masonry'   => $template_directory_uri . '/images/layout-masonry.png',
		),
		'active_callback' => array(
			array(
				'setting'  => 'home_archive_default',
				'operator' => '==',
				'value'    => false,
			),
		),
	)
);

Kirki::add_field(
	'custom_theme_mod', array(
		'type'        => 'select',
		'settings'    => 'home_columns',
		'label'       => esc_html__( 'Columns', 'text-domain' ),
		'description' => esc_html__( 'Three- and four-column layouts are visible on larger screens only.', 'text-domain' ),
		'section'     => 'home',
		'default'     => '2',
		'priority'    => 10,
		'choices'     => array(
			'2'         => '2',
			'3'         => '3',
			'4'         => '4',
		),
		'active_callback' => array(
			array(
				array(
					'setting'  => 'home_archive_default',
					'operator' => '==',
					'value'    => false,
				),
			),
			array(
				array(
					'setting'  => 'home_archive_type',
					'operator' => '==',
					'value'    => 'grid',
				),
				array(
					'setting'  => 'home_archive_type',
					'operator' => '==',
					'value'    => 'masonry',
				),
			),
		),
	)
);

I'm still seeing the Columns select field, even if Default Settings are active:

image

And if I disable Default Settings and select the List (second option) type layout, I still see the Columns field, however it should be visible only if Masonry or Grid is selected:

image

I would highly appreciate, if you could check the issue, @aristath. Thanks!

aristath added a commit that referenced this issue Mar 11, 2018
@aristath
Copy link
Contributor

Took me 4 hours, but I believe it's ok now.
The problem was that in your code you're using nested requirements and that was not properly implemented (nested arrays change the relation from AND to OR and vice-versa every time you go a level deeper).
It should be ok now no matter how many levels deep you go 👍

@sergeysedykh
Copy link

Awesome, it's working now! And looks like it's working much faster now.

Many thanks, you're a ⭐️!

I'm a little bit confused about this sentence though:

It should be ok now no matter how many levels deep you go 👍

What do you mean by many levels?

I thought there're only two levels in active_callback. The first level is with AND operator, while the nested levels are with OR operator.

Or has this recently been changed in Kirki?

@aristath aristath added this to the 3.0.26 milestone Mar 17, 2018
aristath added a commit that referenced this issue Mar 17, 2018
* develop: (73 commits)
  fixes #1730
  fixes #1830
  GDPR: Load webfont-loader locally
  Update fonts
  fixes #1834
  Apply WordPress Coding Standards
  Update kirki-helper-class.md
  see #1797
  cleanup unused vars
  See #1807
  Additional fix for #1809
  fixes #1828
  fixes #1808
  fix #1814
  fix #1797
  fixes #1809
  Update sortable.md
  fixes #1787
  update webfonts & grunt
  changelog
  ...

# Conflicts:
#	modules/postmessage/class-kirki-modules-postmessage.php
@aristath
Copy link
Contributor

I thought there're only two levels in active_callback. The first level is with AND operator, while the nested levels are with OR operator.

Actually it's alternating.
So 1st level is AND, 2nd OR, 3rd AND, 4th OR etc.
Usually no more than 2 levels are needed so this is not that well-known.

For example you could do something like this if needed

'active_callback' => array(
	array(
		array(
			'setting'  => 'setting1',
			'operator' => '==',
			'value'    => false,
		),
		// OR
		array(
			array(
				'setting'  => 'setting2',
				'operator' => '==',
				'value'    => true,
			),
			// AND
			array(
				array(
					'setting'  => 'setting3',
					'operator' => '==',
					'value'    => true,
				),
				// OR
				array(
					'setting'  => 'setting4',
					'operator' => '==',
					'value'    => true,
				),
			),
		),
	),
	// AND
	array(
		array(
			'setting'  => 'setting5',
			'operator' => '==',
			'value'    => true,
		),
		// OR
		array(
			'setting'  => 'setting6',
			'operator' => '==',
			'value'    => false,
		),
	),
),

@sergeysedykh
Copy link

Thanks for explanation! Looks much more flexible now, though I can hardly image conditions with more than two levels.

@Stas0238
Copy link

Stas0238 commented Dec 1, 2020

I thought there're only two levels in active_callback. The first level is with AND operator, while the nested levels are with OR operator.

Actually it's alternating.
So 1st level is AND, 2nd OR, 3rd AND, 4th OR etc.
Usually no more than 2 levels are needed so this is not that well-known.

For example you could do something like this if needed

'active_callback' => array(
	array(
		array(
			'setting'  => 'setting1',
			'operator' => '==',
			'value'    => false,
		),
		// OR
		array(
			array(
				'setting'  => 'setting2',
				'operator' => '==',
				'value'    => true,
			),
			// AND
			array(
				array(
					'setting'  => 'setting3',
					'operator' => '==',
					'value'    => true,
				),
				// OR
				array(
					'setting'  => 'setting4',
					'operator' => '==',
					'value'    => true,
				),
			),
		),
	),
	// AND
	array(
		array(
			'setting'  => 'setting5',
			'operator' => '==',
			'value'    => true,
		),
		// OR
		array(
			'setting'  => 'setting6',
			'operator' => '==',
			'value'    => false,
		),
	),
),

@aristath Can you please, add this explanation to your documentation too :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants