Popup-Maker icon indicating copy to clipboard operation
Popup-Maker copied to clipboard

Short curcuit additional AND condition block processing if previous one failed.

Open danieliser opened this issue 5 years ago • 1 comments

This change was tested recently in Ahoy.

In the Condition processing loops (both JS & PHP),

  • [ ] add a break inside group loop if any condition passes.
  • [ ] add a break if the previous group_check ever comes up false.

IN PHP Method.

		// All Groups Must Return True. Break if any is false and set $loadable to false.
		foreach ( $this->get_conditions( $filters ) as $group => $conditions ) {

			// Groups are false until a condition proves true.
			$group_check = false;

			// At least one group condition must be true. Break this loop if any condition is true.
			foreach ( $conditions as $condition ) {

				// If any condition passes, set $group_check true and break.
				if ( ! $condition['not_operand'] && $this->check_condition( $condition ) ) {
					$group_check = true;
					break;
				} elseif ( $condition['not_operand'] && ! $this->check_condition( $condition ) ) {
					$group_check = true;
					break;
				}

				/**
				 * If any group condition passed, no need to check more in this group.
				 */
				if ($group_check) {
					break;
				}

			}

			/**
			 * If any group of conditions doesn't pass, message is not loadable.
			 *
			 * Set loadable to false and break.
			 */
			if ( ! $group_check ) {
				$loadable = false;
				break;
			}

		}

IN JS

        // All Groups Must Return True. Break if any is false and set loadable to false.
        for (g = 0; conditions.length > g; g++) {

            group = conditions[g];

            // Groups are false until a condition proves true.
            group_check = false;

            // At least one group condition must be true. Break this loop if any condition is true.
            for (c = 0; group.length > c; c++) {

                condition = $.extend({}, {
                    not_operand: false
                }, group[c]);

                // If any condition passes, set group_check true and break.
                if (!condition.not_operand && this.checkCondition(condition)) {
                    group_check = true;
                } else if (condition.not_operand && !this.checkCondition(condition)) {
                    group_check = true;
                }

                this.trigger_event('CheckingCondition', [group_check, condition]);

                /**
                 * If any group condition passed, no need to check more in this group.
                 */
                if (group_check) {
                    break;
                }
            }

            /**
             * If any group of conditions doesn't pass, message is not loadable.
             *
             * Set loadable to false and break.
             */
            if (!group_check) {
                loadable = false;
                break;
            }

        }

danieliser avatar May 07 '19 21:05 danieliser

@fpcorso I believe this also is done in Ahoy and can be ported quickly and easily.

danieliser avatar Dec 11 '20 07:12 danieliser