| @@ -1,0 +1,70 @@ | ||
| 1 | +<?php | |
| 2 | +namespace ABlocks\CreatePage\Page; | |
| 3 | + | |
| 4 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 5 | + exit; // Exit if accessed directly. | |
| 6 | +} | |
| 7 | + | |
| 8 | +abstract class Common { | |
| 9 | + protected const DEFAULT_STATUS = 'publish'; | |
| 10 | + protected string $page_type; | |
| 11 | + protected ?string $settings_field = null; | |
| 12 | + | |
| 13 | + protected string $slug; | |
| 14 | + protected string $title; | |
| 15 | + protected string $content; | |
| 16 | + protected string $status = 'publish'; | |
| 17 | + protected array $allowed_status = [ 'publish', 'draft' ]; | |
| 18 | + | |
| 19 | + | |
| 20 | + public static function init() : void { | |
| 21 | + // ensure WP_Rewrite is initialized | |
| 22 | + if ( ! isset( $GLOBALS['wp_rewrite'] ) ) { | |
| 23 | + global $wp_rewrite; | |
| 24 | + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited | |
| 25 | + $wp_rewrite = new \WP_Rewrite(); | |
| 26 | + } | |
| 27 | + | |
| 28 | + try { | |
| 29 | + ( new static() )->create(); | |
| 30 | + } catch ( \Error $e ) { | |
| 31 | + echo esc_html( $e->getMessage() ); | |
| 32 | + } | |
| 33 | + | |
| 34 | + } | |
| 35 | + | |
| 36 | + private function create(): void { | |
| 37 | + $page_id = wp_insert_post( new \WP_Post( (object) [ | |
| 38 | + 'ID' => 0, | |
| 39 | + 'post_title' => sanitize_text_field( $this->title ), | |
| 40 | + 'post_content' => ( $this->content ), | |
| 41 | + // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict | |
| 42 | + 'post_status' => in_array( $this->status, $this->allowed_status ) ? $this->status : self::DEFAULT_STATUS, | |
| 43 | + 'post_name' => empty( $this->slug ) ? sanitize_title( $this->title ) : sanitize_title( $this->slug ), | |
| 44 | + 'post_type' => 'page', | |
| 45 | + ] ) ); | |
| 46 | + if ( | |
| 47 | + ! $this->is_page_exists( $this->page_type ) && | |
| 48 | + ! is_wp_error( $page_id ) | |
| 49 | + ) { | |
| 50 | + update_post_meta( $page_id, 'ablock_page_type', $this->page_type ); | |
| 51 | + $settings_field = (string) $this->settings_field; | |
| 52 | + if ( ! empty( $settings_field ) ) { | |
| 53 | + | |
| 54 | + $ablocks_settings = json_decode( get_option( ABLOCKS_SETTINGS_NAME, '{}' ), true ); | |
| 55 | + | |
| 56 | + $ablocks_settings[ $settings_field ] = $page_id; | |
| 57 | + update_option( ABLOCKS_SETTINGS_NAME, wp_json_encode( $ablocks_settings ) ); | |
| 58 | + | |
| 59 | + } | |
| 60 | + } | |
| 61 | + } | |
| 62 | + protected function is_page_exists( string $meta_value ) : bool { | |
| 63 | + global $wpdb; | |
| 64 | + $query = "SELECT COUNT(*) FROM {$wpdb->postmeta} | |
| 65 | + WHERE meta_key = %s | |
| 66 | + AND meta_value = %s"; | |
| 67 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared | |
| 68 | + return intval( $wpdb->get_var( $wpdb->prepare( $query, 'ablock_page_type', $meta_value ) ) ) > 0; | |
| 69 | + } | |
| 70 | +} | |