| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Utils\traits; |
| 4 |
|
| 5 |
use StoreEngine\Admin\Settings\Base as BaseSettings; |
| 6 |
use StoreEngine\Utils\Fs; |
| 7 |
use StoreEngine\Utils\Helper; |
| 8 |
|
| 9 |
trait Pages { |
| 10 |
public static function get_store_page_contents( bool $prefix = false ): array { |
| 11 |
// Set the locale to the store locale to ensure pages are created in the correct language. |
| 12 |
Helper::switch_to_site_locale(); |
| 13 |
/** |
| 14 |
* Determines which pages are created during install. |
| 15 |
* |
| 16 |
* @since 0.0.5 |
| 17 |
*/ |
| 18 |
$page_lists = apply_filters( |
| 19 |
'storeengine/store_page_contents', |
| 20 |
[ |
| 21 |
'shop_page' => [ |
| 22 |
'title' => _x( 'Store Shop', 'Page title', 'storeengine' ), |
| 23 |
'slug' => ! $prefix ? _x( 'shop', 'Page slug', 'storeengine' ) : _x( 'store-shop', 'Page slug', 'storeengine' ), |
| 24 |
'content' => '', |
| 25 |
], |
| 26 |
'cart_page' => [ |
| 27 |
'title' => _x( 'Store Cart', 'Page title', 'storeengine' ), |
| 28 |
'slug' => ! $prefix ? _x( 'cart', 'Page slug', 'storeengine' ) : _x( 'store-cart', 'Page slug', 'storeengine' ), |
| 29 |
'content' => Fs::render_template( STOREENGINE_TEMPLATE_PATH . 'page-content/cart.php' ), |
| 30 |
], |
| 31 |
'checkout_page' => [ |
| 32 |
'title' => _x( 'Store Checkout', 'Page title', 'storeengine' ), |
| 33 |
'slug' => ! $prefix ? _x( 'checkout', 'Page slug', 'storeengine' ) : _x( 'store-checkout', 'Page slug', 'storeengine' ), |
| 34 |
'content' => Fs::render_template( STOREENGINE_TEMPLATE_PATH . 'page-content/checkout.php' ), |
| 35 |
], |
| 36 |
'thankyou_page' => [ |
| 37 |
'title' => _x( 'Store Thank You', 'Page title', 'storeengine' ), |
| 38 |
'slug' => ! $prefix ? _x( 'thank-you', 'Page slug', 'storeengine' ) : _x( 'store-thank-you', 'Page slug', 'storeengine' ), |
| 39 |
'content' => Fs::render_template( STOREENGINE_TEMPLATE_PATH . 'page-content/thankyou.php' ), |
| 40 |
], |
| 41 |
'dashboard_page' => [ |
| 42 |
'title' => _x( 'Store Dashboard', 'Page title', 'storeengine' ), |
| 43 |
'slug' => ! $prefix ? _x( 'dashboard', 'Page slug', 'storeengine' ) : _x( 'store-dashboard', 'Page slug', 'storeengine' ), |
| 44 |
'content' => Fs::render_template( STOREENGINE_TEMPLATE_PATH . 'page-content/dashboard.php' ), |
| 45 |
], |
| 46 |
'affiliate_registration_page' => [ |
| 47 |
'title' => _x( 'Store Affiliate Registration', 'Page title', 'storeengine' ), |
| 48 |
'slug' => ! $prefix ? _x( 'affiliate-registration', 'Page slug', 'storeengine' ) : _x( 'store-affiliate-registration', 'Page slug', 'storeengine' ), |
| 49 |
'content' => '<!-- wp:storeengine/shortcode {"shortcode":"storeengine/storeengine_affiliate_application"} /-->', |
| 50 |
], |
| 51 |
'membership_pricing_page' => [ |
| 52 |
'title' => _x( 'Store Membership Pricing', 'Page title', 'storeengine' ), |
| 53 |
'slug' => ! $prefix ? _x( 'membership-pricing', 'Page slug', 'storeengine' ) : _x( 'store-membership-pricing', 'Page slug', 'storeengine' ), |
| 54 |
'content' => '<!-- wp:storeengine/shortcode {"shortcode":"storeengine/storeengine_membership_pricing"} /-->', |
| 55 |
], |
| 56 |
] |
| 57 |
); |
| 58 |
|
| 59 |
|
| 60 |
Helper::restore_locale(); |
| 61 |
|
| 62 |
return $page_lists; |
| 63 |
} |
| 64 |
|
| 65 |
public static function create_initial_pages( bool $prefix = false ) { |
| 66 |
global $storeengine_settings; |
| 67 |
|
| 68 |
// Prepare settings data for update. |
| 69 |
$settings = (array) $storeengine_settings; |
| 70 |
|
| 71 |
// WordPress sets fresh_site to 0 after a page gets published. |
| 72 |
// Prevent fresh_site option from being set to 0 so that we can use it for further customizations. |
| 73 |
remove_action( 'publish_page', '_delete_option_fresh_site', 0 ); |
| 74 |
|
| 75 |
foreach ( self::get_store_page_contents( $prefix ) as $key => $page ) { |
| 76 |
$parent = ! empty( $page['parent'] ) ? Helper::get_settings( $page['parent'], 0 ) : 0; |
| 77 |
$status = ! empty( $page['post_status'] ) ? $page['post_status'] : 'publish'; |
| 78 |
$post_id = self::create_page( |
| 79 |
esc_sql( $page['slug'] ), |
| 80 |
$key, |
| 81 |
$page['title'], |
| 82 |
$page['content'], |
| 83 |
$parent, |
| 84 |
$status |
| 85 |
); |
| 86 |
|
| 87 |
if ( $post_id ) { |
| 88 |
$settings[ $key ] = $post_id; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
BaseSettings::save_settings( $settings ); |
| 95 |
} |
| 96 |
|
| 97 |
public static function create_page( $slug, $settings_key, $title, $content = '', $parent = 0, $status = 'publish' ): int { |
| 98 |
global $storeengine_settings, $wpdb; |
| 99 |
$settings = (array) $storeengine_settings; |
| 100 |
|
| 101 |
$settings_value = (int) ( $settings[ $settings_key ] ?? 0 ); |
| 102 |
if ( $settings_value > 0 ) { |
| 103 |
$page_object = get_post( $settings_value ); |
| 104 |
|
| 105 |
if ( $page_object && 'page' === $page_object->post_type && ! in_array( $page_object->post_status, [ 'pending', 'trash', 'future', 'auto-draft' ], true ) ) { |
| 106 |
// Valid page is already in place. |
| 107 |
return $settings_value; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
if ( strlen( $content ) > 0 ) { |
| 112 |
// Search for an existing page with the specified page content (typically a shortcode). |
| 113 |
$shortcode = str_replace( [ '<!-- storeengine:shortcode -->', '<!-- /storeengine:shortcode -->' ], '', $content ); |
| 114 |
$valid_page_found = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_content LIKE %s LIMIT 1;", "%{$shortcode}%" ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 115 |
} else { |
| 116 |
// Search for an existing page with the specified page slug. |
| 117 |
$valid_page_found = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_name = %s LIMIT 1;", $slug ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 118 |
} |
| 119 |
|
| 120 |
if ( $valid_page_found ) { |
| 121 |
return $valid_page_found; |
| 122 |
} |
| 123 |
|
| 124 |
// Search for a matching valid trashed page. |
| 125 |
if ( strlen( $content ) > 0 ) { |
| 126 |
// Search for an existing page with the specified page content (typically a shortcode). |
| 127 |
$trashed_page_found = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_content LIKE %s LIMIT 1;", "%{$content}%" ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 128 |
} else { |
| 129 |
// Search for an existing page with the specified page slug. |
| 130 |
$trashed_page_found = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_name = %s LIMIT 1;", $slug ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 131 |
} |
| 132 |
|
| 133 |
if ( $trashed_page_found ) { |
| 134 |
$page_id = $trashed_page_found; |
| 135 |
wp_update_post( [ |
| 136 |
'ID' => $page_id, |
| 137 |
'post_status' => $status, |
| 138 |
'comment_status' => 'closed', |
| 139 |
] ); |
| 140 |
} else { |
| 141 |
$page_data = [ |
| 142 |
'post_status' => $status, |
| 143 |
'post_type' => 'page', |
| 144 |
'post_author' => 1, |
| 145 |
'post_name' => $slug, |
| 146 |
'post_title' => $title, |
| 147 |
'post_content' => $content, |
| 148 |
'post_parent' => $parent, |
| 149 |
'comment_status' => 'closed', |
| 150 |
]; |
| 151 |
$page_id = wp_insert_post( $page_data ); |
| 152 |
|
| 153 |
if ( is_wp_error( $page_id ) ) { |
| 154 |
return 0; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Fire once StoreEngine page as been created. |
| 159 |
* |
| 160 |
* @since 0.0.5 |
| 161 |
* |
| 162 |
* @param int $page_id Post ID |
| 163 |
* @param array $page_data Post data. |
| 164 |
*/ |
| 165 |
do_action( 'storeengine/page_created', $page_id, $page_data ); |
| 166 |
} |
| 167 |
|
| 168 |
if ( $page_id ) { |
| 169 |
// Set template for handling block-theme compatibility. |
| 170 |
update_post_meta( $page_id, '_wp_page_template', 'storeengine-canvas.php' ); |
| 171 |
|
| 172 |
return $page_id; |
| 173 |
} |
| 174 |
|
| 175 |
return 0; |
| 176 |
} |
| 177 |
} |
| 178 |
|