| @@ -9,8 +9,11 @@ | ||
| 9 | 9 | * @since 1.0 |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | 12 | // Exit if accessed directly. |
| 13 | +use Give\Framework\Database\DB; | |
| 14 | +use Give\Campaigns\Models\Campaign; | |
| 15 | + | |
| 13 | 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | 17 | exit; |
| 15 | 18 | } |
| 16 | 19 | |
| @@ -225,12 +228,120 @@ | ||
| 225 | 228 | |
| 226 | 229 | return $output; |
| 227 | 230 | } |
| 228 | 231 | |
| 229 | - /** | |
| 230 | - * Donors Dropdown | |
| 231 | - * | |
| 232 | - * Renders an HTML Dropdown of all donors. | |
| 232 | + /** | |
| 233 | + * @since 4.1.0 | |
| 234 | + */ | |
| 235 | + public function campaigns_dropdown($args = []) | |
| 236 | + { | |
| 237 | + $defaults = [ | |
| 238 | + 'name' => 'campaigns', | |
| 239 | + 'id' => 'campaigns', | |
| 240 | + 'class' => '', | |
| 241 | + 'multiple' => false, | |
| 242 | + 'selected' => 0, | |
| 243 | + 'chosen' => false, | |
| 244 | + 'number' => 30, | |
| 245 | + 'placeholder' => esc_attr__('All Campaigns', 'give'), | |
| 246 | + 'data' => [ | |
| 247 | + 'search-type' => 'campaign', | |
| 248 | + ], | |
| 249 | + 'query_args' => [], | |
| 250 | + ]; | |
| 251 | + | |
| 252 | + $args = wp_parse_args($args, $defaults); | |
| 253 | + | |
| 254 | + $campaigns_args = wp_parse_args( | |
| 255 | + $args['query_args'], | |
| 256 | + [ | |
| 257 | + 'orderby' => 'id', | |
| 258 | + 'order' => 'DESC', | |
| 259 | + 'per_page' => $args['number'], | |
| 260 | + ] | |
| 261 | + ); | |
| 262 | + | |
| 263 | + /** | |
| 264 | + * Filter the campaigns dropdown. | |
| 265 | + * | |
| 266 | + * @since 4.1.0 | |
| 267 | + * | |
| 268 | + * @param array $campaigns_args Arguments for campaigns_dropdown query. | |
| 269 | + * | |
| 270 | + * @return array Arguments for campaigns_dropdown query. | |
| 271 | + */ | |
| 272 | + $campaigns_args = apply_filters('give_campaigns_dropdown_args', $campaigns_args); | |
| 273 | + | |
| 274 | + $campaigns = DB::table('give_campaigns', 'campaigns') | |
| 275 | + ->select( | |
| 276 | + ['campaigns.id', 'id'], | |
| 277 | + ['campaigns.form_id', 'defaultFormId'], | |
| 278 | + ['campaign_title', 'title'], | |
| 279 | + ['GROUP_CONCAT(campaign_forms.form_id)', 'form_ids'] | |
| 280 | + ) | |
| 281 | + ->join(function ($builder) { | |
| 282 | + $builder | |
| 283 | + ->leftJoin("give_campaign_forms", "campaign_forms") | |
| 284 | + ->on("campaign_forms.campaign_id", "id"); | |
| 285 | + }) | |
| 286 | + ->groupBy("campaigns.id") | |
| 287 | + ->orderBy($campaigns_args['orderby'], $campaigns_args['order']) | |
| 288 | + ->limit($campaigns_args['per_page']) | |
| 289 | + ->getAll(); | |
| 290 | + | |
| 291 | + $options = []; | |
| 292 | + | |
| 293 | + // Ensure the selected campaign is included in options | |
| 294 | + if (false !== $args['selected'] && $args['selected'] !== 0) { | |
| 295 | + $selectedCampaign = Campaign::find((int)$args['selected']); | |
| 296 | + | |
| 297 | + if ($selectedCampaign) { | |
| 298 | + $selected_title = empty($selectedCampaign->title) | |
| 299 | + ? sprintf(__('Untitled (#%s)', 'give'), $selectedCampaign->id) | |
| 300 | + : $selectedCampaign->title; | |
| 301 | + $options[$args['selected']] = esc_html($selected_title); | |
| 302 | + } | |
| 303 | + } | |
| 304 | + | |
| 305 | + $options[0] = esc_html__('No campaigns found.', 'give'); | |
| 306 | + if ( ! empty($campaigns)) { | |
| 307 | + $options[0] = $args['placeholder']; | |
| 308 | + foreach ($campaigns as $campaign) { | |
| 309 | + $campaign_title = empty($campaign->title) | |
| 310 | + ? sprintf(__('Untitled (#%s)', 'give'), $campaign->id) | |
| 311 | + : $campaign->title; | |
| 312 | + | |
| 313 | + $options[absint($campaign->id)] = esc_html($campaign_title); | |
| 314 | + } | |
| 315 | + } | |
| 316 | + | |
| 317 | + $output = $this->select( | |
| 318 | + [ | |
| 319 | + 'name' => $args['name'], | |
| 320 | + 'selected' => $args['selected'], | |
| 321 | + 'id' => $args['id'], | |
| 322 | + 'class' => $args['class'], | |
| 323 | + 'options' => $options, | |
| 324 | + 'chosen' => $args['chosen'], | |
| 325 | + 'multiple' => $args['multiple'], | |
| 326 | + 'placeholder' => $args['placeholder'], | |
| 327 | + 'show_option_all' => false, | |
| 328 | + 'show_option_none' => false, | |
| 329 | + 'data' => array_merge( | |
| 330 | + $args['data'], | |
| 331 | + ['campaigns' => json_encode($campaigns)] | |
| 332 | + ), | |
| 333 | + ] | |
| 334 | + ); | |
| 335 | + | |
| 336 | + return $output; | |
| 337 | + } | |
| 338 | + | |
| 339 | + | |
| 340 | + /** | |
| 341 | + * Donors Dropdown | |
| 342 | + * | |
| 343 | + * Renders an HTML Dropdown of all donors. | |
| 233 | 344 | * |
| 234 | 345 | * @since 1.0 |
| 235 | 346 | * @access public |
| 236 | 347 | * |