PluginProbe
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management / 1.4.0
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management v1.4.0
1.6.1 1.6.0 1.5.1 1.5.0 1.4.0 1.3.0 trunk 0.0.1 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0
suredonation / inc / campaigns / campaign-cpt.php

campaign-cpt.php in SureDonation – Donation Forms, Fundraising Campaigns & Donor Management 1.4.0, at inc/campaigns/campaign-cpt.php

201 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Campaign Custom Post Type
4 *
5 * @package SureDonation
6 */
7
8 namespace SureDonation\Inc\Campaigns;
9
10 use SureDonation\Inc\Helper;
11 use SureDonation\Inc\Post_Types\Donation_Form;
12 use SureDonation\Inc\Traits\Get_Instance;
13
14 // Exit if accessed directly.
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 /**
20 * Campaign_Cpt class.
21 *
22 * @since 0.0.1
23 */
24 class Campaign_Cpt {
25 use Get_Instance;
26
27 /**
28 * Post type slug.
29 *
30 * @since 0.0.1
31 */
32 public const POST_TYPE = 'suredonation_cmpgn';
33
34 /**
35 * Meta key for the linked default form ID.
36 *
37 * @since 0.0.1
38 */
39 public const META_DEFAULT_FORM_ID = '_suredonation_default_form_id';
40
41 /**
42 * Constructor.
43 *
44 * @since 0.0.1
45 */
46 public function __construct() {
47 add_action( 'init', [ $this, 'register_post_type' ] );
48 add_action( 'init', [ $this, 'register_campaign_meta' ] );
49 add_action( 'save_post_' . self::POST_TYPE, [ $this, 'maybe_create_default_form' ], 20, 2 );
50 }
51
52 /**
53 * Register Campaign post type.
54 *
55 * @return void
56 * @since 0.0.1
57 */
58 public function register_post_type() {
59 $labels = [
60 'name' => _x( 'Campaigns', 'Post Type General Name', 'suredonation' ),
61 'singular_name' => _x( 'Campaign', 'Post Type Singular Name', 'suredonation' ),
62 'menu_name' => __( 'Campaigns', 'suredonation' ),
63 'name_admin_bar' => __( 'Campaign', 'suredonation' ),
64 'archives' => __( 'Campaign Archives', 'suredonation' ),
65 'attributes' => __( 'Campaign Attributes', 'suredonation' ),
66 'parent_item_colon' => __( 'Parent Campaign:', 'suredonation' ),
67 'all_items' => __( 'All Campaigns', 'suredonation' ),
68 'add_new_item' => __( 'Add New Campaign', 'suredonation' ),
69 'add_new' => __( 'Add New', 'suredonation' ),
70 'new_item' => __( 'New Campaign', 'suredonation' ),
71 'edit_item' => __( 'Edit Campaign', 'suredonation' ),
72 'update_item' => __( 'Update Campaign', 'suredonation' ),
73 'view_item' => __( 'View Campaign', 'suredonation' ),
74 'view_items' => __( 'View Campaigns', 'suredonation' ),
75 'search_items' => __( 'Search Campaign', 'suredonation' ),
76 'not_found' => __( 'Not found', 'suredonation' ),
77 'not_found_in_trash' => __( 'Not found in Trash', 'suredonation' ),
78 'featured_image' => __( 'Campaign Image', 'suredonation' ),
79 'set_featured_image' => __( 'Set campaign image', 'suredonation' ),
80 'remove_featured_image' => __( 'Remove campaign image', 'suredonation' ),
81 'use_featured_image' => __( 'Use as campaign image', 'suredonation' ),
82 'insert_into_item' => __( 'Insert into campaign', 'suredonation' ),
83 'uploaded_to_this_item' => __( 'Uploaded to this campaign', 'suredonation' ),
84 'items_list' => __( 'Campaigns list', 'suredonation' ),
85 'items_list_navigation' => __( 'Campaigns list navigation', 'suredonation' ),
86 'filter_items_list' => __( 'Filter campaigns list', 'suredonation' ),
87 ];
88
89 $args = [
90 'label' => __( 'Campaign', 'suredonation' ),
91 'description' => __( 'Fundraising Campaigns', 'suredonation' ),
92 'labels' => $labels,
93 'supports' => [ 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields' ],
94 'hierarchical' => false,
95 'public' => true,
96 'show_ui' => true,
97 'show_in_menu' => false, // We'll add it to custom menu.
98 'menu_position' => 25,
99 'menu_icon' => 'dashicons-heart',
100 'show_in_admin_bar' => true,
101 'show_in_nav_menus' => true,
102 'can_export' => true,
103 'has_archive' => true,
104 'exclude_from_search' => false,
105 'publicly_queryable' => true,
106 'capability_type' => 'post',
107 'show_in_rest' => true,
108 'rewrite' => [
109 'slug' => 'suredonation-campaigns',
110 'with_front' => false,
111 ],
112 ];
113
114 register_post_type( self::POST_TYPE, $args );
115 }
116
117 /**
118 * Register campaign meta for the campaign post type.
119 *
120 * @return void
121 * @since 0.0.1
122 */
123 public function register_campaign_meta() {
124 register_post_meta(
125 self::POST_TYPE,
126 Helper::SUREDONATION_CAMPAIGN_META_KEY,
127 [
128 'type' => 'string',
129 'description' => __( 'Consolidated campaign settings.', 'suredonation' ),
130 'single' => true,
131 'show_in_rest' => [
132 'schema' => [
133 'type' => 'string',
134 'context' => [ 'edit' ],
135 ],
136 ],
137 'auth_callback' => static function () {
138 return current_user_can( 'manage_options' );
139 },
140 ]
141 );
142 }
143
144 /**
145 * Create a default donation form when a campaign is first published.
146 *
147 * Only creates a form if:
148 * - The campaign is being published (not draft/pending)
149 * - No default form has been created yet for this campaign
150 *
151 * @param int $post_id Post ID.
152 * @param \WP_Post $post Post object.
153 * @return void
154 * @since 0.0.1
155 */
156 public function maybe_create_default_form( $post_id, $post ) {
157 // Skip autosave.
158 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
159 return;
160 }
161
162 // Only proceed for published campaigns.
163 if ( 'publish' !== $post->post_status ) {
164 return;
165 }
166
167 // Check if a default form already exists for this campaign.
168 $existing_form_id = get_post_meta( $post_id, self::META_DEFAULT_FORM_ID, true );
169 if ( ! empty( $existing_form_id ) ) {
170 $existing_form_id = absint( Helper::get_string_value( $existing_form_id ) );
171 if ( 0 >= $existing_form_id ) {
172 return;
173 }
174 // Verify the form still exists.
175 $existing_form = get_post( $existing_form_id );
176 if ( $existing_form instanceof \WP_Post && Donation_Form::POST_TYPE === $existing_form->post_type ) {
177 return; // Form already exists, don't create another.
178 }
179 }
180
181 // Create the default form.
182 $form_id = Donation_Form::create_default_form_for_campaign( $post_id, $post->post_title );
183
184 if ( $form_id ) {
185 // Store the form ID in campaign meta.
186 update_post_meta( $post_id, self::META_DEFAULT_FORM_ID, $form_id );
187 }
188 }
189
190 /**
191 * Get the default form ID for a campaign.
192 *
193 * @param int $campaign_id Campaign ID.
194 * @return int Form ID or 0 if not found.
195 * @since 0.0.1
196 */
197 public static function get_default_form_id( $campaign_id ) {
198 return absint( Helper::get_string_value( get_post_meta( $campaign_id, self::META_DEFAULT_FORM_ID, true ) ) );
199 }
200 }
201