| 1 |
<?php |
| 2 |
/** |
| 3 |
* Endpoint abstract model. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_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 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Endpoint' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Charitable_Endpoint |
| 21 |
* |
| 22 |
* @since 1.5.0 |
| 23 |
*/ |
| 24 |
abstract class Charitable_Endpoint implements Charitable_Endpoint_Interface { |
| 25 |
|
| 26 |
/** The endpoint's unique identifier. */ |
| 27 |
const ID = ''; |
| 28 |
|
| 29 |
/** The endpoint's priority in terms of when it should be loaded. */ |
| 30 |
const PRIORITY = 10; |
| 31 |
|
| 32 |
/** |
| 33 |
* Whether the endpoint can be cached. |
| 34 |
* |
| 35 |
* @since 1.5.4 |
| 36 |
* |
| 37 |
* @var boolean |
| 38 |
*/ |
| 39 |
protected $cacheable = true; |
| 40 |
|
| 41 |
/** |
| 42 |
* Whether comments are disabled on this endpoint. |
| 43 |
* |
| 44 |
* @since 1.6.36 |
| 45 |
* |
| 46 |
* @var boolean |
| 47 |
*/ |
| 48 |
protected $comments_disabled = true; |
| 49 |
|
| 50 |
/** |
| 51 |
* Add rewrite rules for the endpoint. |
| 52 |
* |
| 53 |
* Unless the child class defines this, this won't do anything for an endpoint. |
| 54 |
* |
| 55 |
* @since 1.5.0 |
| 56 |
*/ |
| 57 |
public function setup_rewrite_rules() { |
| 58 |
/* Do nothing by default. */ |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Add new query vars for the endpoint. |
| 63 |
* |
| 64 |
* Unless the child class defines this, this won't do anything for an endpoint. |
| 65 |
* |
| 66 |
* @since 1.5.0 |
| 67 |
* |
| 68 |
* @param array $vars The query vars. |
| 69 |
* @return array |
| 70 |
*/ |
| 71 |
public function add_query_vars( array $vars ) { |
| 72 |
/* Return vars unchanged by default. */ |
| 73 |
return $vars; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* If the user should be redirected somewhere, return a URL. Otherwise, |
| 78 |
* simply return false. |
| 79 |
* |
| 80 |
* @since 1.6.26 |
| 81 |
* |
| 82 |
* @return false|string |
| 83 |
*/ |
| 84 |
public function get_redirect() { |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Whether the endpoint uses a custom template. |
| 90 |
* |
| 91 |
* @since 1.6.55 |
| 92 |
* |
| 93 |
* @return boolean |
| 94 |
*/ |
| 95 |
public function uses_custom_template() { |
| 96 |
return ! charitable_is_block_theme(); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Prepare the template for the endpoint. |
| 101 |
* |
| 102 |
* @since 1.6.55 |
| 103 |
* |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
public function setup_template() { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Set up the endpoint template. |
| 112 |
* |
| 113 |
* By default, we will return the default template that WordPress already selected. |
| 114 |
* |
| 115 |
* @since 1.5.0 |
| 116 |
* |
| 117 |
* @param string $template The default template. |
| 118 |
* @return string|array |
| 119 |
*/ |
| 120 |
public function get_template( $template ) { |
| 121 |
return $template; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Get the content to display for the endpoint. |
| 126 |
* |
| 127 |
* By default, we will return the default content that is passed by WordPress. |
| 128 |
* |
| 129 |
* @since 1.5.0 |
| 130 |
* |
| 131 |
* @param string $content The default content. |
| 132 |
* @return string |
| 133 |
*/ |
| 134 |
public function get_content( $content ) { |
| 135 |
return $content; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Return the body class to add for the endpoint. |
| 140 |
* |
| 141 |
* By default, this will be the endpoint ID with underscores replaced by hyphens. |
| 142 |
* |
| 143 |
* @since 1.5.0 |
| 144 |
* |
| 145 |
* @return string |
| 146 |
*/ |
| 147 |
public function get_body_class() { |
| 148 |
return str_replace( '_', '-', $this->get_endpoint_id() ); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Whether an endpoint can be cached. |
| 153 |
* |
| 154 |
* @since 1.5.4 |
| 155 |
* |
| 156 |
* @return boolean |
| 157 |
*/ |
| 158 |
public function is_cacheable() { |
| 159 |
return $this->cacheable; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Whether comments are disabled for this endpoint. |
| 164 |
* |
| 165 |
* @since 1.6.36 |
| 166 |
* |
| 167 |
* @return boolean |
| 168 |
*/ |
| 169 |
public function comments_disabled() { |
| 170 |
return $this->comments_disabled; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Return a nav menu object, or null if the endpoint should not be |
| 175 |
* added to navigation menus. |
| 176 |
* |
| 177 |
* @since 1.6.29 |
| 178 |
* |
| 179 |
* @return object|null |
| 180 |
*/ |
| 181 |
public function nav_menu_object() { |
| 182 |
return null; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Return a nav menu object. |
| 187 |
* |
| 188 |
* @since 1.6.29 |
| 189 |
* |
| 190 |
* @param string $title Menu item title. |
| 191 |
* @return object |
| 192 |
*/ |
| 193 |
public function get_nav_menu_object( $title ) { |
| 194 |
$menu_object = new stdClass(); |
| 195 |
$menu_object->classes = array( 'charitable-menu' ); |
| 196 |
$menu_object->type = 'custom'; |
| 197 |
$menu_object->object_id = $this->get_endpoint_id(); |
| 198 |
$menu_object->title = $title; |
| 199 |
$menu_object->object = 'custom'; |
| 200 |
$menu_object->url = esc_url_raw( $this->get_page_url() ); |
| 201 |
$menu_object->attr_title = $title; |
| 202 |
$menu_object->db_id = ''; |
| 203 |
$menu_object->menu_item_parent = 0; |
| 204 |
$menu_object->target = ''; |
| 205 |
$menu_object->xfn = ''; |
| 206 |
|
| 207 |
return $menu_object; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Given a base URL and a slug to append, return an |
| 212 |
* updated endpoint URL. |
| 213 |
* |
| 214 |
* @since 1.6.44 |
| 215 |
* |
| 216 |
* @param string $base_url The base URL. |
| 217 |
* @param string $slug The slug to append. |
| 218 |
* @return string |
| 219 |
*/ |
| 220 |
public function sanitize_endpoint_url( $base_url, $slug ) { |
| 221 |
$url_parts = parse_url( $base_url ); |
| 222 |
|
| 223 |
if ( ! array_key_exists( 'query', $url_parts ) ) { |
| 224 |
return trailingslashit( $base_url ) . trailingslashit( $slug ); |
| 225 |
} |
| 226 |
|
| 227 |
/* Strip out the query string. */ |
| 228 |
$base_url = str_replace( '?' . $url_parts['query'], '', $base_url ); |
| 229 |
|
| 230 |
/* Append our slug, then the query string. */ |
| 231 |
return trailingslashit( $base_url ) . trailingslashit( $slug ) . '?' . $url_parts['query']; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
endif; |
| 236 |
|