PostService.php
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\WordPress\Posts; |
| 4 | |
| 5 | use WP_Post; |
| 6 | |
| 7 | /** |
| 8 | * Handles Post related services. |
| 9 | */ |
| 10 | class PostService { |
| 11 | /** |
| 12 | * The post. |
| 13 | * |
| 14 | * @var WP_Post|null |
| 15 | */ |
| 16 | public $post; |
| 17 | |
| 18 | /** |
| 19 | * Get the form post. |
| 20 | * |
| 21 | * @param WP_Post|int|null $post The form or post type post. |
| 22 | * |
| 23 | * @return WP_Post|null |
| 24 | */ |
| 25 | public function getFormPost( $post = null ) { |
| 26 | return $this->getFormPostFromBlock( $post ) ?? $this->getFormPostFromShortcode( $post ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Get the form post from a page. |
| 31 | * |
| 32 | * @param WP_Post|int|null $post The form or post type post. |
| 33 | * |
| 34 | * @return WP_Post|null |
| 35 | */ |
| 36 | public function getFormPostFromBlock( $post = null ) { |
| 37 | $this->post = get_post( $post ?? $this->post ); |
| 38 | |
| 39 | // we don't have a post. |
| 40 | if ( empty( $this->post->ID ) ) { |
| 41 | return null; |
| 42 | } |
| 43 | |
| 44 | // get the checkout form block. |
| 45 | $block = wp_get_first_block( parse_blocks( $this->post->post_content ), 'surecart/checkout-form' ); |
| 46 | |
| 47 | if ( empty( $block['attrs']['id'] ) ) { |
| 48 | return null; |
| 49 | } |
| 50 | |
| 51 | // get the post. |
| 52 | return get_post( $block['attrs']['id'] ?? null ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Get the form post from a shortcode. |
| 57 | * |
| 58 | * @param WP_Post|null $post The form or post type post. |
| 59 | * |
| 60 | * @return WP_Post|null |
| 61 | */ |
| 62 | public function getFormPostFromShortcode( $post = null ) { |
| 63 | $this->post = get_post( $post ?? $this->post ); |
| 64 | |
| 65 | if ( empty( $this->post->ID ) ) { |
| 66 | return null; |
| 67 | } |
| 68 | |
| 69 | if ( has_shortcode( $this->post->post_content, 'sc_form' ) ) { |
| 70 | $shortcode = get_shortcode_regex( ['sc_form'] ); |
| 71 | preg_match( "/$shortcode/", $this->post->post_content, $matches ); |
| 72 | $attrs = shortcode_parse_atts( $matches[3] ?? '' ); |
| 73 | return ! empty( $attrs['id'] ) ? get_post( $attrs['id'] ) : null; |
| 74 | } |
| 75 | |
| 76 | return null; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get the form block. |
| 81 | * |
| 82 | * @param WP_Post|null $post The form or post type post. |
| 83 | * @return array|null |
| 84 | */ |
| 85 | public function getFormBlock( $post = null ) { |
| 86 | $this->post = get_post( $post ?? $this->post ); |
| 87 | |
| 88 | // we don't have a post. |
| 89 | if ( empty( $this->post->ID ) ) { |
| 90 | return null; |
| 91 | } |
| 92 | |
| 93 | // this is not the block we are are looking for. |
| 94 | if ( ! has_block( 'surecart/checkout-form', $this->post ) && ! has_block( 'surecart/form', $this->post ) ) { |
| 95 | return null; |
| 96 | } |
| 97 | |
| 98 | $blocks = parse_blocks( $this->post->post_content ); |
| 99 | |
| 100 | // we have a checkout form block. |
| 101 | if ( has_block( 'surecart/checkout-form', $post ) ) { |
| 102 | $block = wp_get_first_block( $blocks, 'surecart/checkout-form' ); |
| 103 | // Get the form post id. |
| 104 | $this->post = get_post( $block['attrs']['id'] ?? null ); |
| 105 | } |
| 106 | |
| 107 | $blocks = parse_blocks( $this->post->post_content ); |
| 108 | |
| 109 | return wp_get_first_block( $blocks, 'surecart/form' ); |
| 110 | } |
| 111 | } |
| 112 |