PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 2.2.0
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v2.2.0
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.2 2.2.1 2.2.0 2.1.2 2.1.1 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 All 66 releases
better-payment / includes / Campaign / CPT.php

CPT.php in Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More 2.2.0, at includes/Campaign/CPT.php

307 lines 11.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Better_Payment\Lite\Campaign;
4
5 use Better_Payment\Lite\Admin\DB;
6 use Better_Payment\Lite\Controller;
7 use Better_Payment\Lite\Campaign\Elements\ElementRegistry;
8 use Better_Payment\Lite\Campaign\Templates\TemplateManager;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 /**
15 * Registers the bp_campaign Custom Post Type and the
16 * hidden campaign builder admin page.
17 */
18 class CPT extends Controller {
19
20 /**
21 * Register the CPT and the builder admin page.
22 */
23 public function register() {
24 $args = apply_filters( 'better_payment/campaign/cpt_args', [
25 'label' => __( 'Campaigns', 'better-payment' ),
26 'labels' => [
27 'name' => __( 'Campaigns', 'better-payment' ),
28 'singular_name' => __( 'Campaign', 'better-payment' ),
29 'add_new' => __( 'Add New', 'better-payment' ),
30 'add_new_item' => __( 'Add New Campaign', 'better-payment' ),
31 'edit_item' => __( 'Edit Campaign', 'better-payment' ),
32 'new_item' => __( 'New Campaign', 'better-payment' ),
33 'view_item' => __( 'View Campaign', 'better-payment' ),
34 'search_items' => __( 'Search Campaigns', 'better-payment' ),
35 'not_found' => __( 'No campaigns found', 'better-payment' ),
36 'not_found_in_trash' => __( 'No campaigns found in trash', 'better-payment' ),
37 ],
38 'public' => true,
39 'publicly_queryable' => true,
40 'show_ui' => true,
41 'show_in_menu' => false,
42 'show_in_rest' => true,
43 'rewrite' => [ 'slug' => 'bp-campaign', 'with_front' => false ],
44 'supports' => [ 'title', 'thumbnail' ],
45 'has_archive' => false,
46 'menu_icon' => 'dashicons-heart',
47 ] );
48
49 register_post_type( 'bp_campaign', $args );
50 }
51
52 /**
53 * Register the hidden campaign builder page under the Better Payment menu.
54 */
55 public function register_builder_page() {
56 add_submenu_page(
57 '',
58 __( 'Campaign Builder', 'better-payment' ),
59 __( 'Campaign Builder', 'better-payment' ),
60 'manage_options',
61 'bp-campaign-builder',
62 [ $this, 'render_builder_page' ]
63 );
64
65 // Hidden pages (parent = '') are not found by get_admin_page_title(), leaving
66 // $title null and causing a strip_tags() deprecation in admin-header.php.
67 // Set the global before the header loads.
68 add_action( 'current_screen', static function ( $screen ) {
69 if ( 'admin_page_bp-campaign-builder' === $screen->id ) {
70 global $title;
71 $title = __( 'Campaign Builder', 'better-payment' );
72 }
73 } );
74 }
75
76 /**
77 * Inject the Campaigns entry into the Better Payment submenu list
78 * immediately after Transactions.
79 *
80 * @param array $list
81 * @param string $prefix
82 * @return array
83 */
84 public function inject_campaigns_submenu( array $list, string $prefix ): array {
85 $new = [];
86 $transactions_key = $prefix . '-admin&tab=transactions';
87
88 foreach ( $list as $slug => $item ) {
89 $new[ $slug ] = $item;
90
91 if ( $slug === $transactions_key ) {
92 $new['edit.php?post_type=bp_campaign'] = [
93 'title' => __( 'Campaigns', 'better-payment' ),
94 'capability' => 'manage_options',
95 'callback' => '',
96 ];
97 }
98 }
99
100 return $new;
101 }
102
103 /**
104 * Render the campaign builder page shell — React app mounts here.
105 */
106 public function render_builder_page() {
107 $campaign_id = isset( $_GET['campaign_id'] ) ? absint( $_GET['campaign_id'] ) : 0;
108 $campaign = $campaign_id ? get_post( $campaign_id ) : null;
109
110 if ( $campaign && $campaign->post_type !== 'bp_campaign' ) {
111 $campaign = null;
112 $campaign_id = 0;
113 }
114
115 wp_enqueue_script( 'bp-campaign-builder' );
116 wp_enqueue_style( 'bp-campaign-builder' );
117
118 $nonce = wp_create_nonce( 'wp_rest' );
119
120 ?>
121 <div id="bp-campaign-builder"
122 data-campaign-id="<?php echo esc_attr( $campaign_id ); ?>"
123 data-rest-url="<?php echo esc_url( rest_url( 'better-payment/v1/' ) ); ?>"
124 data-nonce="<?php echo esc_attr( $nonce ); ?>"
125 data-admin-url="<?php echo esc_url( admin_url() ); ?>"
126 data-campaigns-url="<?php echo esc_url( admin_url( 'admin.php?page=better-payment-admin&tab=campaigns' ) ); ?>"
127 ></div>
128 <?php
129 }
130
131 /**
132 * Rewrite the edit link for bp_campaign posts to point to the builder.
133 * Covers row-action "Edit", title links, and any get_edit_post_link() call.
134 *
135 * @param string $url
136 * @param int $post_id
137 * @param string $_context
138 * @return string
139 */
140 public function filter_edit_link( string $url, int $post_id, string $_context ): string {
141 if ( get_post_type( $post_id ) !== 'bp_campaign' ) {
142 return $url;
143 }
144
145 return esc_url( admin_url( 'admin.php?page=bp-campaign-builder&campaign_id=' . $post_id ) );
146 }
147
148 /**
149 * Redirect edit.php?post_type=bp_campaign to the custom campaigns tab so
150 * users never land on the raw WP post list screen.
151 * Skipped for AJAX and REST requests.
152 */
153 public function redirect_cpt_list() {
154 if ( wp_doing_ajax() ) {
155 return;
156 }
157
158 $post_type = isset( $_GET['post_type'] ) ? sanitize_key( $_GET['post_type'] ) : '';
159
160 if (
161 'edit.php' === $GLOBALS['pagenow']
162 && $post_type === 'bp_campaign'
163 ) {
164 wp_safe_redirect( admin_url( 'admin.php?page=better-payment-admin&tab=campaigns' ) );
165 exit;
166 }
167 }
168
169 /**
170 * Redirect post.php?action=edit for bp_campaign in case the old URL is
171 * reached directly (bookmarks, browser history, etc.).
172 */
173 public function redirect_edit_post() {
174 $action = isset( $_GET['action'] ) ? sanitize_key( $_GET['action'] ) : '';
175 $post_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : 0;
176
177 if ( $action !== 'edit' || ! $post_id ) {
178 return;
179 }
180
181 if ( get_post_type( $post_id ) !== 'bp_campaign' ) {
182 return;
183 }
184
185 wp_safe_redirect( admin_url( 'admin.php?page=bp-campaign-builder&campaign_id=' . $post_id ) );
186 exit;
187 }
188
189 /**
190 * Redirect post-new.php for bp_campaign to the campaigns list so the
191 * template-select modal flow is used instead of the classic editor.
192 */
193 public function redirect_new_post() {
194 if (
195 isset( $_GET['post_type'] ) &&
196 sanitize_key( $_GET['post_type'] ) === 'bp_campaign'
197 ) {
198 wp_safe_redirect( admin_url( 'edit.php?post_type=bp_campaign' ) );
199 exit;
200 }
201 }
202
203 /**
204 * Enqueue the campaign-list script and its template data on the
205 * bp_campaign post list table page.
206 */
207 public function enqueue_list_assets( string $hook ) {
208 if ( $hook !== 'edit.php' ) {
209 return;
210 }
211
212 $post_type = isset( $_GET['post_type'] ) ? sanitize_key( $_GET['post_type'] ) : '';
213 if ( $post_type !== 'bp_campaign' ) {
214 return;
215 }
216
217 $version = defined( 'WP_DEBUG' ) && WP_DEBUG ? time() : BETTER_PAYMENT_VERSION;
218
219 wp_enqueue_script(
220 'bp-campaign-list',
221 BETTER_PAYMENT_ASSETS . '/admin/campaign-list/campaign-list.min.js',
222 [ 'react', 'react-dom', 'wp-element', 'wp-i18n' ],
223 $version,
224 true
225 );
226
227 wp_enqueue_style(
228 'bp-campaign-list',
229 BETTER_PAYMENT_ASSETS . '/admin/campaign-list/campaign-list.min.css',
230 [],
231 $version
232 );
233
234 wp_localize_script( 'bp-campaign-list', 'betterPaymentCampaignData', [
235 'templates' => array_values( TemplateManager::get_all() ),
236 'restUrl' => rest_url( 'better-payment/v1/' ),
237 'nonce' => wp_create_nonce( 'wp_rest' ),
238 'adminUrl' => admin_url(),
239 ] );
240 }
241
242 /**
243 * Enqueue campaign builder assets on the builder page only.
244 */
245 public function enqueue_builder_assets() {
246 $page = isset( $_GET['page'] ) ? sanitize_text_field( $_GET['page'] ) : '';
247
248 if ( $page !== 'bp-campaign-builder' ) {
249 return;
250 }
251
252 $version = defined( 'WP_DEBUG' ) && WP_DEBUG ? time() : BETTER_PAYMENT_VERSION;
253
254 wp_register_script(
255 'bp-campaign-builder',
256 BETTER_PAYMENT_ASSETS . '/admin/campaign-builder/campaign-builder.min.js',
257 [ 'react', 'react-dom', 'wp-element', 'wp-api-fetch', 'wp-i18n' ],
258 $version,
259 true
260 );
261
262 wp_register_style(
263 'bp-campaign-builder',
264 BETTER_PAYMENT_ASSETS . '/admin/campaign-builder/campaign-builder.min.css',
265 [],
266 $version
267 );
268
269 // Load campaign display CSS so the preview modal renders correctly.
270 $display_css = BETTER_PAYMENT_PATH . '/assets/blocks/campaign-display/style.min.css';
271 if ( file_exists( $display_css ) ) {
272 wp_enqueue_style(
273 'better-payment-campaign-display-style',
274 BETTER_PAYMENT_ASSETS . '/blocks/campaign-display/style.min.css',
275 [],
276 filemtime( $display_css )
277 );
278 }
279
280 wp_enqueue_media();
281
282 // Zero out all WP admin chrome spacing so the builder fills edge-to-edge.
283 wp_add_inline_style( 'bp-campaign-builder', '
284 #wpcontent { padding-left: 0 !important; }
285 #adminmenushadow { display: none !important; }
286 #wpbody-content { overflow: hidden !important; padding: 0 !important; }
287 #wpbody-content .wrap { margin: 0 !important; padding: 0 !important; max-width: none !important; }
288 ' );
289
290 // Global currency from plugin settings — builder uses this everywhere.
291 $global_currency = DB::get_settings( 'better_payment_settings_general_general_currency' );
292 if ( ! is_string( $global_currency ) || $global_currency === '' ) {
293 $global_currency = 'USD';
294 }
295
296 // Localize element registry, templates, and global settings to JS builder.
297 wp_localize_script( 'bp-campaign-builder', 'betterPaymentCampaignData', [
298 'elements' => array_values( ElementRegistry::get_all() ),
299 'templates' => array_values( TemplateManager::get_all() ),
300 'globalCurrency' => $global_currency,
301 'restUrl' => rest_url( 'better-payment/v1/' ),
302 'nonce' => wp_create_nonce( 'wp_rest' ),
303 'pluginUrl' => plugins_url( '', BETTER_PAYMENT_BASENAME ),
304 ] );
305 }
306 }
307