PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.14.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.14.0
2.14.0 2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 All 81 releases
← All changes | includes/create-page/page/common.php +70 -0 2.7.5 → 2.14.0 View file →
@@ -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 +}