| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable Tools Settings UI. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Tools_Settings |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2022, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.7.0.7 |
| 10 |
* @version 1.6.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! class_exists( 'Charitable_Tools_Settings' ) ) : |
| 19 |
|
| 20 |
/** |
| 21 |
* Charitable_Tools_Settings |
| 22 |
* |
| 23 |
* @since 1.6.0 |
| 24 |
*/ |
| 25 |
final class Charitable_Tools_Settings { |
| 26 |
|
| 27 |
/** |
| 28 |
* The single instance of this class. |
| 29 |
* |
| 30 |
* @since 1.6.0 |
| 31 |
* |
| 32 |
* @var Charitable_Tools_Settings|null |
| 33 |
*/ |
| 34 |
private static $instance = null; |
| 35 |
|
| 36 |
/** |
| 37 |
* Create object instance. |
| 38 |
* |
| 39 |
* @since 1.6.0 |
| 40 |
*/ |
| 41 |
private function __construct() { |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Returns and/or create the single instance of this class. |
| 46 |
* |
| 47 |
* @since 1.6.0 |
| 48 |
* |
| 49 |
* @return Charitable_Tools_Settings |
| 50 |
*/ |
| 51 |
public static function get_instance() { |
| 52 |
if ( is_null( self::$instance ) ) { |
| 53 |
self::$instance = new self(); |
| 54 |
} |
| 55 |
|
| 56 |
return self::$instance; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Add the tools tab settings fields. |
| 61 |
* |
| 62 |
* @since 1.6.0 |
| 63 |
* |
| 64 |
* @return array<string,array> |
| 65 |
*/ |
| 66 |
public function add_tools_fields() { |
| 67 |
|
| 68 |
if ( ! charitable_is_settings_view( 'tools' ) ) { |
| 69 |
return array(); |
| 70 |
} |
| 71 |
|
| 72 |
$data_fields = $this->get_user_donation_field_options(); |
| 73 |
|
| 74 |
return array( |
| 75 |
'section' => array( |
| 76 |
'title' => '', |
| 77 |
'type' => 'hidden', |
| 78 |
'priority' => 1000, |
| 79 |
'value' => 'import', |
| 80 |
), |
| 81 |
'section_import' => array( |
| 82 |
'title' => __( 'Import', 'charitable' ), |
| 83 |
'type' => 'heading', |
| 84 |
'priority' => 2, |
| 85 |
), |
| 86 |
'import_campaign' => array( |
| 87 |
'label_for' => __( 'Campaign Import <span class="badge beta">Beta</span>', 'charitable' ), |
| 88 |
'type' => 'file', |
| 89 |
'wrapper_class' => 'test', |
| 90 |
'nonce_action_name' => 'import_campaign', |
| 91 |
'nonce_field_name' => 'charitable_nonce', |
| 92 |
'name' => 'campaign', |
| 93 |
'action' => 'tools-campaign', |
| 94 |
'default' => false, |
| 95 |
'button_label' => 'Import Campaign', |
| 96 |
'help' => sprintf( |
| 97 |
/* translators: %1$s: HTML strong tag. %2$s: HTML closing strong tag. %1$s: HTML break tag. */ |
| 98 |
__( 'Imports a campaign JSON file into this website. It will not overwrite any previous campaigns, and it will set to "inactive" with a new campaign ID.%3$s %1$sCampaign tools do not include donations or campaign creators%2$s.' ), |
| 99 |
'<strong>', |
| 100 |
'</strong>', |
| 101 |
'<br />' |
| 102 |
), |
| 103 |
'priority' => 25, |
| 104 |
'default' => 2 |
| 105 |
), |
| 106 |
'import_donations' => array( |
| 107 |
'label_for' => __( 'Campaign Donations <span class="badge beta">Beta</span>', 'charitable' ), |
| 108 |
'type' => 'file', |
| 109 |
'wrapper_class' => 'test', |
| 110 |
'nonce_action_name' => 'import_donations', |
| 111 |
'nonce_field_name' => 'charitable_nonce', |
| 112 |
'name' => 'donations', |
| 113 |
'action' => 'tools-donation', |
| 114 |
'default' => false, |
| 115 |
'button_label' => 'Import Donations', |
| 116 |
'help' => sprintf( |
| 117 |
/* translators: %1$s: HTML strong tag. %2$s: HTML closing strong tag. %1$s: HTML break tag. */ |
| 118 |
__( 'Imports a donation JSON file into the selected campaign. It will not overwrite any previous donations or prevent duplicates.%3$s %1$sDonation tools do not include users - if a donor\'s email address is not found, the donation will not be assigned to a user.%2$s.' ), |
| 119 |
'<strong>', |
| 120 |
'</strong>', |
| 121 |
'<br />' |
| 122 |
), |
| 123 |
'priority' => 25, |
| 124 |
'default' => 2, |
| 125 |
'options' => $this->get_campaign_list( true ), |
| 126 |
'select_name' => 'tools_campaign', |
| 127 |
), |
| 128 |
'section' => array( |
| 129 |
'title' => '', |
| 130 |
'type' => 'hidden', |
| 131 |
'priority' => 2000, |
| 132 |
'value' => 'export', |
| 133 |
), |
| 134 |
'section_export' => array( |
| 135 |
'title' => __( 'Export', 'charitable' ), |
| 136 |
'class' => 'section-heading', |
| 137 |
'type' => 'heading', |
| 138 |
'priority' => 202, |
| 139 |
), |
| 140 |
'export_campaign' => array( |
| 141 |
'label_for' => __( 'Campaign Export <span class="badge beta">Beta</span>', 'charitable' ), |
| 142 |
'type' => 'select-form', |
| 143 |
'wrapper_class' => 'test', |
| 144 |
'nonce_action_name' => 'export_campaign', |
| 145 |
'nonce_field_name' => 'charitable_nonce', |
| 146 |
'name' => 'campaign', |
| 147 |
'action' => 'export-campaign', |
| 148 |
'default' => false, |
| 149 |
'button_label' => 'Export Campaign', |
| 150 |
'help' => sprintf( |
| 151 |
/* translators: %1$s: HTML strong tag. %2$s: HTML closing strong tag. %1$s: HTML break tag. */ |
| 152 |
__( 'Campaign exports files can be used to create a backup of your campaigns or to import campaigns into another site with Charitable installed.%3$s %1$sCampaign exports do not include donations or campaign creators%2$s.' ), |
| 153 |
'<strong>', |
| 154 |
'</strong>', |
| 155 |
'<br />' |
| 156 |
), |
| 157 |
'priority' => 225, |
| 158 |
'default' => 2, |
| 159 |
'options' => $this->get_campaigns_to_export(), |
| 160 |
), |
| 161 |
'export_donations' => array( |
| 162 |
'label_for' => __( 'Donations Export <span class="badge beta">Beta</span>', 'charitable' ), |
| 163 |
'type' => 'select-form', |
| 164 |
'wrapper_class' => 'test', |
| 165 |
'nonce_action_name' => 'export_donations_from_campaign', |
| 166 |
'nonce_field_name' => 'charitable_nonce', |
| 167 |
'name' => 'donations', |
| 168 |
'action' => 'export-donations-from-campaign', |
| 169 |
'default' => false, |
| 170 |
'button_label' => 'Export Donations', |
| 171 |
'help' => sprintf( |
| 172 |
/* translators: %1$s: HTML strong tag. %2$s: HTML closing strong tag. %1$s: HTML break tag. */ |
| 173 |
__( 'Donation exports files can be used to create a backup of your donations or to import donations if you are migrating campaigns to another site with Charitable installed. %3$s All donation types (paid, refunded, pending) will be exported. %1$sRecurring donations are not included%2$s.' ), |
| 174 |
'<strong>', |
| 175 |
'</strong>', |
| 176 |
'<br />' |
| 177 |
), |
| 178 |
'priority' => 227, |
| 179 |
'default' => 2, |
| 180 |
'options' => $this->get_campaigns_to_export( true ), |
| 181 |
), |
| 182 |
); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Return the list of user donation field options. |
| 187 |
* |
| 188 |
* @since 1.6.0 |
| 189 |
* |
| 190 |
* @return string[] |
| 191 |
*/ |
| 192 |
protected function get_user_donation_field_options() { |
| 193 |
$fields = charitable()->donation_fields()->get_data_type_fields( 'user' ); |
| 194 |
|
| 195 |
return array_combine( |
| 196 |
array_keys( $fields ), |
| 197 |
wp_list_pluck( $fields, 'label' ) |
| 198 |
); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Return the list of user donation field options. |
| 203 |
* |
| 204 |
* @since 1.7.0.7 |
| 205 |
* |
| 206 |
* @return string[] |
| 207 |
*/ |
| 208 |
protected function get_campaign_list( $include_donations = false ) { |
| 209 |
|
| 210 |
$campaigns = array(); |
| 211 |
|
| 212 |
$args = array( |
| 213 |
'posts_per_page' => -1, |
| 214 |
'orderby' => 'post_title', |
| 215 |
'order' => 'ASC', |
| 216 |
'fields' => array('post_title', 'ID') |
| 217 |
); |
| 218 |
|
| 219 |
$results = Charitable_Campaigns::query( $args ); |
| 220 |
|
| 221 |
if ( empty( $results->posts ) ) { |
| 222 |
return array(); |
| 223 |
} |
| 224 |
|
| 225 |
foreach ( $results->posts as $index => $post ) : |
| 226 |
$campaigns[ $post->ID ] = $post->post_title; |
| 227 |
if ( $include_donations ) { |
| 228 |
$donations = charitable_get_table( 'campaign_donations' )->get_donations_on_campaign( $post->ID ); |
| 229 |
$donation_count = count( $donations ); |
| 230 |
$plural = ( ( $donation_count > 1 ) || 0 === $donation_count ) ? 's' : ''; |
| 231 |
$campaigns[ $post->ID ] = $post->post_title . ' (' . count( $donations ) . ' donation' . $plural . ') '; |
| 232 |
} |
| 233 |
endforeach; |
| 234 |
|
| 235 |
return( $campaigns ); |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Return the list of user donation field options. |
| 241 |
* |
| 242 |
* @since 1.7.0.7 |
| 243 |
* |
| 244 |
* @return string[] |
| 245 |
*/ |
| 246 |
protected function get_campaigns_to_export( $include_donations = false ) { |
| 247 |
|
| 248 |
$campaigns = array(); |
| 249 |
|
| 250 |
$args = array( |
| 251 |
'posts_per_page' => -1, |
| 252 |
'orderby' => 'post_title', |
| 253 |
'order' => 'ASC', |
| 254 |
'fields' => array('post_title', 'ID') |
| 255 |
); |
| 256 |
|
| 257 |
$results = Charitable_Campaigns::query( $args ); |
| 258 |
|
| 259 |
if ( empty( $results->posts ) ) { |
| 260 |
return array(); |
| 261 |
} |
| 262 |
|
| 263 |
foreach ( $results->posts as $index => $post ) : |
| 264 |
$campaigns[ $post->ID ] = $post->post_title; |
| 265 |
if ( $include_donations ) { |
| 266 |
$donations = charitable_get_table( 'campaign_donations' )->get_donations_on_campaign( $post->ID ); |
| 267 |
$donation_count = count( $donations ); |
| 268 |
$plural = ( ( $donation_count > 1 ) || 0 === $donation_count ) ? 's' : ''; |
| 269 |
$campaigns[ $post->ID ] = $post->post_title . ' (' . count( $donations ) . ' donation' . $plural . ') '; |
| 270 |
} |
| 271 |
endforeach; |
| 272 |
|
| 273 |
return( $campaigns ); |
| 274 |
|
| 275 |
} |
| 276 |
|
| 277 |
} |
| 278 |
|
| 279 |
endif; |
| 280 |
|