PluginProbe
YayExtra – WooCommerce Extra Product Options / trunk
YayExtra – WooCommerce Extra Product Options vtrunk
2.1.0 trunk 1.1.4 1.1.5 1.1.8 1.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3 1.3.1 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.5.2 1.5.3 1.5.5 1.5.6 All 30 releases
yayextra / includes / Helper / OptionTree.php

OptionTree.php in YayExtra – WooCommerce Extra Product Options trunk, at includes/Helper/OptionTree.php

136 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace YayExtra\Helper;
3
4 defined( 'ABSPATH' ) || exit;
5
6 /**
7 * Walk the nested option tree (steps / accordions / leaf).
8 */
9 class OptionTree {
10
11 const KIND_LEAF = 'leaf';
12 const KIND_STEPS = 'steps';
13 const KIND_STEP = 'step';
14 const KIND_ACCORDIONS = 'accordions';
15 const KIND_ACCORDION = 'accordion';
16
17 /**
18 * Option types locked to Pro. Lite storefront does not render these.
19 *
20 * @var string[]
21 */
22 const PRO_ONLY_TYPES = array(
23 'button_multi',
24 'swatches_multi',
25 'date_picker',
26 'time_picker',
27 'file_upload',
28 'file_download',
29 'image_upload',
30 'popup',
31 'product_list',
32 'link',
33 );
34
35 /**
36 * @param array|null $option Option node.
37 * @return string
38 */
39 public static function get_kind( $option ) {
40 $type = isset( $option['type']['value'] ) ? (string) $option['type']['value'] : '';
41 if ( in_array( $type, array( self::KIND_STEPS, self::KIND_STEP, self::KIND_ACCORDIONS, self::KIND_ACCORDION ), true ) ) {
42 return $type;
43 }
44 return self::KIND_LEAF;
45 }
46
47 /**
48 * @param array|null $option Option node.
49 * @return bool
50 */
51 public static function is_leaf( $option ) {
52 return self::KIND_LEAF === self::get_kind( $option );
53 }
54
55 /**
56 * Whether this option type is Pro-only (not rendered on Lite storefront).
57 *
58 * @param string $type Option type value.
59 * @return bool
60 */
61 public static function is_pro_only_type( $type ) {
62 return in_array( (string) $type, self::PRO_ONLY_TYPES, true );
63 }
64
65 /**
66 * Flatten leaf options from a tree (skips steps/step containers).
67 *
68 * @param array $options Tree.
69 * @return array
70 */
71 public static function flatten_leaves( $options = array() ) {
72 $out = array();
73 if ( empty( $options ) || ! is_array( $options ) ) {
74 return $out;
75 }
76 foreach ( $options as $option ) {
77 if ( ! is_array( $option ) ) {
78 continue;
79 }
80 if ( self::is_leaf( $option ) ) {
81 $out[] = $option;
82 continue;
83 }
84 $children = isset( $option['children'] ) && is_array( $option['children'] ) ? $option['children'] : array();
85 $out = array_merge( $out, self::flatten_leaves( $children ) );
86 }
87 return $out;
88 }
89
90 /**
91 * Find a node by id anywhere in the tree.
92 *
93 * @param array $options Tree.
94 * @param string $id Node id.
95 * @return array
96 */
97 public static function find_by_id( $options, $id ) {
98 if ( empty( $options ) || ! is_array( $options ) ) {
99 return array();
100 }
101 foreach ( $options as $option ) {
102 if ( ! is_array( $option ) ) {
103 continue;
104 }
105 if ( isset( $option['id'] ) && (string) $option['id'] === (string) $id ) {
106 return $option;
107 }
108 $children = isset( $option['children'] ) && is_array( $option['children'] ) ? $option['children'] : array();
109 $found = self::find_by_id( $children, $id );
110 if ( ! empty( $found ) ) {
111 return $found;
112 }
113 }
114 return array();
115 }
116
117 /**
118 * Frontend script expects a flat `options` list of leaves (logics / costs).
119 *
120 * @param array $sets Option sets from get_option_set_array.
121 * @return array
122 */
123 public static function flatten_sets_for_script( $sets ) {
124 if ( empty( $sets ) || ! is_array( $sets ) ) {
125 return array();
126 }
127 foreach ( $sets as &$set ) {
128 if ( ! empty( $set['options'] ) && is_array( $set['options'] ) ) {
129 $set['options'] = self::flatten_leaves( $set['options'] );
130 }
131 }
132 unset( $set );
133 return $sets;
134 }
135 }
136