| 1 |
<?php |
| 2 |
/** |
| 3 |
* Donate endpoint. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Campaign_Widget_Endpoint |
| 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.5.0 |
| 10 |
* @version 1.6.55 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} // Exit if accessed directly |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Campaign_Widget_Endpoint' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Charitable_Campaign_Widget_Endpoint |
| 21 |
* |
| 22 |
* @since 1.5.0 |
| 23 |
*/ |
| 24 |
class Charitable_Campaign_Widget_Endpoint extends Charitable_Endpoint { |
| 25 |
|
| 26 |
/** Endpoint ID. */ |
| 27 |
const ID = 'campaign_widget'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Return the endpoint ID. |
| 31 |
* |
| 32 |
* @since 1.5.0 |
| 33 |
* |
| 34 |
* @return string |
| 35 |
*/ |
| 36 |
public static function get_endpoint_id() { |
| 37 |
return self::ID; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Add rewrite rules for the endpoint. |
| 42 |
* |
| 43 |
* @since 1.5.0 |
| 44 |
*/ |
| 45 |
public function setup_rewrite_rules() { |
| 46 |
add_rewrite_endpoint( 'widget', EP_PERMALINK ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Return the endpoint URL. |
| 51 |
* |
| 52 |
* @since 1.5.0 |
| 53 |
* |
| 54 |
* @global WP_Rewrite $wp_rewrite |
| 55 |
* @param array $args Mixed args. |
| 56 |
* @return string |
| 57 |
*/ |
| 58 |
public function get_page_url( $args = array() ) { |
| 59 |
global $wp_rewrite; |
| 60 |
|
| 61 |
$campaign_id = array_key_exists( 'campaign_id', $args ) ? $args['campaign_id'] : get_the_ID(); |
| 62 |
|
| 63 |
if ( $wp_rewrite->using_permalinks() && ! isset( $_GET['preview'] ) ) { |
| 64 |
$url = trailingslashit( get_permalink( $campaign_id ) ) . 'widget/'; |
| 65 |
} else { |
| 66 |
$url = esc_url_raw( add_query_arg( array( 'widget' => 1 ), get_permalink( $campaign_id ) ) ); |
| 67 |
} |
| 68 |
|
| 69 |
return $url; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Return whether we are currently viewing the endpoint. |
| 74 |
* |
| 75 |
* @since 1.5.0 |
| 76 |
* |
| 77 |
* @global WP_Query $wp_query |
| 78 |
* @param array $args Mixed args. |
| 79 |
* @return boolean |
| 80 |
*/ |
| 81 |
public function is_page( $args = array() ) { |
| 82 |
global $wp_query; |
| 83 |
|
| 84 |
return $wp_query->is_main_query() |
| 85 |
&& array_key_exists( 'widget', $wp_query->query_vars ) |
| 86 |
&& $wp_query->is_singular( Charitable::CAMPAIGN_POST_TYPE ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Prepare the template for the endpoint. |
| 91 |
* |
| 92 |
* @since 1.6.55 |
| 93 |
* |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
public function setup_template() { |
| 97 |
do_action( 'charitable_is_widget' ); |
| 98 |
|
| 99 |
add_filter( 'show_admin_bar', '__return_false' ); |
| 100 |
add_action( 'wp_head', 'charitable_hide_admin_bar' ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Return the template to display for this endpoint. |
| 105 |
* |
| 106 |
* @since 1.5.0 |
| 107 |
* @since 1.6.55 Returns array instead of string. |
| 108 |
* |
| 109 |
* @param string $template The default template. |
| 110 |
* @return array |
| 111 |
*/ |
| 112 |
public function get_template( $template ) { |
| 113 |
return apply_filters( 'charitable_widget_page_template', array( 'campaign-widget.php' ) ); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
endif; |
| 118 |
|