| 1 |
<?php |
| 2 |
|
| 3 |
namespace SureCart\Models; |
| 4 |
|
| 5 |
use SureCart\Models\Product; |
| 6 |
|
| 7 |
/** |
| 8 |
* Checkout form class |
| 9 |
*/ |
| 10 |
class Form { |
| 11 |
/** |
| 12 |
* Holds the form post. |
| 13 |
* |
| 14 |
* @var \WP_Post; |
| 15 |
*/ |
| 16 |
protected $post; |
| 17 |
|
| 18 |
/** |
| 19 |
* Get the post |
| 20 |
* |
| 21 |
* @param [type] $id |
| 22 |
*/ |
| 23 |
final public function __construct( $id = 0 ) { |
| 24 |
$this->post = get_post( $id ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* The form's post type |
| 29 |
* |
| 30 |
* @return string The post type. |
| 31 |
*/ |
| 32 |
protected function getPostType() { |
| 33 |
// TODO: get this from registration. |
| 34 |
return 'sc_form'; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Get the stored product choices |
| 39 |
* |
| 40 |
* @param int|WP_Post $id Post object or id. |
| 41 |
* @return array |
| 42 |
*/ |
| 43 |
protected function getPriceIds( $id ) { |
| 44 |
$this->post = get_post( $id ); |
| 45 |
$blocks = parse_blocks( $this->post->post_content ); |
| 46 |
return $this->getNested( $blocks, 'price_id' ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get a form's attribute |
| 51 |
* |
| 52 |
* @param string $attribute Attribute name. |
| 53 |
* @return mixed |
| 54 |
*/ |
| 55 |
public function getAttribute( $attribute ) { |
| 56 |
$blocks = parse_blocks( $this->post->post_content ); |
| 57 |
$form_block = $blocks[0] ?? false; |
| 58 |
if ( ! $form_block || 'surecart/form' !== $form_block['blockName'] ) { |
| 59 |
return ''; |
| 60 |
} |
| 61 |
return $form_block['attrs'][ $attribute ] ?? null; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Get the form's mode |
| 66 |
* |
| 67 |
* @param int|WP_Post $id Post object or id. |
| 68 |
* @return string |
| 69 |
*/ |
| 70 |
protected function getMode( $id ) { |
| 71 |
$this->post = get_post( $id ); |
| 72 |
$blocks = parse_blocks( $this->post->post_content ); |
| 73 |
$form_block = $blocks[0] ?? false; |
| 74 |
if ( ! $form_block || 'surecart/form' !== $form_block['blockName'] ) { |
| 75 |
return ''; |
| 76 |
} |
| 77 |
return $form_block['attrs']['mode'] ?? 'live'; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Get nested values from an array |
| 82 |
* |
| 83 |
* @param array $array Array to search. |
| 84 |
* @param string $nested_key Nested key to search for. |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
protected function getNested( array $array, $nested_key ) { |
| 88 |
$return = array(); |
| 89 |
array_walk_recursive( |
| 90 |
$array, |
| 91 |
function( $a, $key ) use ( &$return, $nested_key ) { |
| 92 |
if ( $nested_key === $key ) { |
| 93 |
$return[] = $a; |
| 94 |
} |
| 95 |
} |
| 96 |
); |
| 97 |
return $return; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get the form's products. |
| 102 |
* |
| 103 |
* @param int|WP_Post Block post id. |
| 104 |
*/ |
| 105 |
protected function getProducts( $id ) { |
| 106 |
$ids = $this->getPriceIds( $id ); |
| 107 |
// no products. |
| 108 |
if ( empty( $ids ) ) { |
| 109 |
return []; |
| 110 |
} |
| 111 |
|
| 112 |
// get prices with their product |
| 113 |
$prices = Price::where( |
| 114 |
[ |
| 115 |
'ids' => $ids, |
| 116 |
] |
| 117 |
)->with( [ 'product' ] )->get(); |
| 118 |
|
| 119 |
$products = []; |
| 120 |
foreach ( $prices as $price ) { |
| 121 |
$products[] = $price->product; |
| 122 |
} |
| 123 |
return $products; |
| 124 |
} |
| 125 |
|
| 126 |
protected function getPosts( $id ) { |
| 127 |
$blocks = $this->searchBlocks( $id ); |
| 128 |
$shortcodes = $this->searchShortcodes( $id ); |
| 129 |
return array_merge( $blocks, $shortcodes ); |
| 130 |
} |
| 131 |
|
| 132 |
public function searchBlocks( $id ) { |
| 133 |
return get_posts( |
| 134 |
[ |
| 135 |
's' => '<!-- wp:surecart/checkout-form {"id":' . (int) $id, |
| 136 |
'sentence' => 1, |
| 137 |
'post_type' => 'any', |
| 138 |
'per_page' => -1, |
| 139 |
] |
| 140 |
); |
| 141 |
} |
| 142 |
|
| 143 |
public function searchShortcodes( $id ) { |
| 144 |
return get_posts( |
| 145 |
[ |
| 146 |
's' => '[sc_form id=' . (int) $id, |
| 147 |
'sentence' => 1, |
| 148 |
'post_type' => 'any', |
| 149 |
'per_page' => -1, |
| 150 |
] |
| 151 |
); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Get blocks from the posts. |
| 156 |
* |
| 157 |
* @param array $posts Array of posts. |
| 158 |
* @return array Array of blocks. |
| 159 |
*/ |
| 160 |
public function getBlocksFromPosts( $posts ) { |
| 161 |
$blocks = []; |
| 162 |
foreach ( $posts as $post ) { |
| 163 |
$parsed_blocks = parse_blocks( $post->post_content ); |
| 164 |
$blocks = array_merge( $blocks, $this->findCheckoutBlocks( $parsed_blocks, $post ) ); |
| 165 |
} |
| 166 |
return array_filter( $blocks ); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Find our checkout block. |
| 171 |
* |
| 172 |
* @param array $post_blocks Blocks array. |
| 173 |
* @param \WP_Post $post_object Post object. |
| 174 |
* @return array |
| 175 |
*/ |
| 176 |
public function findCheckoutBlocks( $post_blocks, \WP_Post $post_object ) { |
| 177 |
$blocks = []; |
| 178 |
foreach ( $post_blocks as $block ) { |
| 179 |
if ( 'surecart/checkout-form' === $block['blockName'] ) { |
| 180 |
$block['post'] = $post_object; |
| 181 |
$blocks[] = $block; |
| 182 |
} elseif ( ! empty( $block['innerBlocks'] ) ) { |
| 183 |
$blocks = array_merge( $blocks, $this->findCheckoutBlocks( $block['innerBlocks'], $post_object ) ); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
return array_filter( $blocks ); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Static Facade Accessor |
| 192 |
* |
| 193 |
* @param string $method Method to call. |
| 194 |
* @param mixed $params Method params. |
| 195 |
* |
| 196 |
* @return mixed |
| 197 |
*/ |
| 198 |
public static function __callStatic( $method, $params ) { |
| 199 |
return call_user_func_array( [ new static(), $method ], $params ); |
| 200 |
} |
| 201 |
} |
| 202 |
|