PageUrl.php
| 1 | <?php |
| 2 | |
| 3 | namespace Kirki\App\Supports; |
| 4 | |
| 5 | defined('ABSPATH') || exit; |
| 6 | |
| 7 | use Kirki\App\Constants\CollectionTypes; |
| 8 | use Kirki\App\Constants\PageMetaKeys; |
| 9 | use Kirki\App\Constants\PostTypes; |
| 10 | use Kirki\App\Models\Page as PageModel; |
| 11 | use Kirki\App\Models\PostMeta; |
| 12 | use Kirki\App\Utils\PostUtil; |
| 13 | use Kirki\Framework\Sanitizer; |
| 14 | |
| 15 | use function Kirki\App\is_not_empty_array; |
| 16 | |
| 17 | class PageUrl { |
| 18 | |
| 19 | /** |
| 20 | * The page |
| 21 | * |
| 22 | * @var PageModel |
| 23 | */ |
| 24 | protected $page; |
| 25 | |
| 26 | /** |
| 27 | * The page permalink |
| 28 | * |
| 29 | * @var string |
| 30 | */ |
| 31 | protected $page_permalink; |
| 32 | |
| 33 | /** |
| 34 | * Constructor |
| 35 | * |
| 36 | * @param PageModel|int $page |
| 37 | */ |
| 38 | public function __construct($page) |
| 39 | { |
| 40 | if (is_numeric($page)) { |
| 41 | $page = PageModel::find($page); |
| 42 | } |
| 43 | |
| 44 | $this->page = $page; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Create a new instance |
| 49 | * |
| 50 | * @param PageModel|int $page |
| 51 | * @return static |
| 52 | */ |
| 53 | public static function create($page) |
| 54 | { |
| 55 | return new static($page); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Get the page |
| 60 | * |
| 61 | * @return PageModel |
| 62 | */ |
| 63 | public function get_page() |
| 64 | { |
| 65 | return $this->page; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Get the permalink of a page |
| 70 | * |
| 71 | * @return string |
| 72 | */ |
| 73 | public function get_page_permalink() |
| 74 | { |
| 75 | if ($this->page_permalink) { |
| 76 | return $this->page_permalink; |
| 77 | } |
| 78 | |
| 79 | return $this->page_permalink = $this->process_page_permalink($this->page); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * process the permalink of a page |
| 84 | * |
| 85 | * @param PageModel|int $page |
| 86 | * @return string |
| 87 | */ |
| 88 | protected function process_page_permalink($page) |
| 89 | { |
| 90 | if (is_numeric($page)) { |
| 91 | $page = PageModel::find($page); |
| 92 | } |
| 93 | |
| 94 | $post_permalink = PostUtil::get_permalink($page); |
| 95 | $protocol = strpos(home_url(), 'https://') !== false ? 'https' : 'http'; |
| 96 | |
| 97 | if ($protocol === 'https') { |
| 98 | return str_replace('http://', 'https://', $post_permalink); |
| 99 | } |
| 100 | |
| 101 | return str_replace('https://', 'http://', $post_permalink); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Get the ajax url |
| 106 | * |
| 107 | * @return string |
| 108 | */ |
| 109 | public function get_ajax_url() |
| 110 | { |
| 111 | $protocol = strpos(home_url(), 'https://') !== false ? 'https' : 'http'; |
| 112 | return admin_url('admin-ajax.php', $protocol); |
| 113 | } |
| 114 | |
| 115 | public function get_preview_url() |
| 116 | { |
| 117 | if ($this->page->post_type === PostTypes::TEMPLATE) { |
| 118 | $meta = isset($page->meta) && $page->meta->not_empty() ? $page->meta->pluck('meta_value', 'meta_key')->to_array() : []; |
| 119 | $conditions = $meta[PageMetaKeys::TEMPLATE_CONDITIONS] ?? PostMeta::get_meta_value($this->page->ID, PageMetaKeys::TEMPLATE_CONDITIONS, []); |
| 120 | |
| 121 | [ |
| 122 | 'type' => $type, |
| 123 | 'data' => $data |
| 124 | ] = CollectionItem::get_items_from_condition($conditions); |
| 125 | |
| 126 | if (empty($data)) { |
| 127 | return $this->get_page_permalink(); |
| 128 | } |
| 129 | |
| 130 | switch ($type) { |
| 131 | case CollectionTypes::POST: |
| 132 | return $this->process_page_permalink($data[0]['ID']); |
| 133 | case CollectionTypes::USER: |
| 134 | return get_author_posts_url($data[0]['ID']); |
| 135 | case CollectionTypes::TERM: |
| 136 | return get_term_link($data[0]['ID']); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return $this->get_page_permalink(); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Get the editor url of a page |
| 145 | * |
| 146 | * @return string |
| 147 | */ |
| 148 | public function get_editor_url() |
| 149 | { |
| 150 | $query_param = [ |
| 151 | 'action' => KIRKI_EDITOR_ACTION |
| 152 | ]; |
| 153 | |
| 154 | if ($this->page->post_type === PostTypes::UTILITY) { |
| 155 | $query_param['p'] = $this->page->ID; |
| 156 | } |
| 157 | |
| 158 | $editor_url = add_query_arg( |
| 159 | $query_param, |
| 160 | $this->get_page_permalink() |
| 161 | ); |
| 162 | |
| 163 | if (EditorPreview::has_valid_token()) { |
| 164 | $token = EditorPreview::get_token_from_header(); |
| 165 | |
| 166 | $editor_url = add_query_arg( |
| 167 | [ |
| 168 | 'editor-preview-token' => $token, |
| 169 | ], |
| 170 | $editor_url |
| 171 | ); |
| 172 | } |
| 173 | |
| 174 | return $editor_url; |
| 175 | |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Get the iframe url of a page |
| 180 | * |
| 181 | * @return string |
| 182 | */ |
| 183 | public function get_iframe_url() |
| 184 | { |
| 185 | $iframe_url_params = [ |
| 186 | 'action' => KIRKI_EDITOR_ACTION, |
| 187 | 'load_for' => 'kirki-iframe', |
| 188 | 'post_id' => $this->page->ID, |
| 189 | ]; |
| 190 | |
| 191 | $editor_preview_token = $this->get_editor_preview_token(); |
| 192 | |
| 193 | if (!empty($editor_preview_token)) { |
| 194 | $iframe_url_params['editor-preview-token'] = $editor_preview_token; |
| 195 | } |
| 196 | |
| 197 | return add_query_arg( |
| 198 | $iframe_url_params, |
| 199 | $this->get_page_permalink() |
| 200 | ); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Get the nonce |
| 205 | * |
| 206 | * @return string |
| 207 | */ |
| 208 | public function get_nonce() |
| 209 | { |
| 210 | return wp_create_nonce('wp_rest'); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Get the editor preview token |
| 215 | * |
| 216 | * @return string |
| 217 | */ |
| 218 | public function get_editor_preview_token() |
| 219 | { |
| 220 | $token = filter_input(INPUT_GET, 'editor-preview-token'); |
| 221 | $editor_preview_token = Sanitizer::apply_rule($token, Sanitizer::TEXT); |
| 222 | |
| 223 | return !empty($editor_preview_token) ? $editor_preview_token : ''; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Get the home url |
| 228 | * |
| 229 | * @return string |
| 230 | */ |
| 231 | public function get_home_url() |
| 232 | { |
| 233 | return home_url(); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Get the site url |
| 238 | * |
| 239 | * @return string |
| 240 | */ |
| 241 | public function get_site_url() |
| 242 | { |
| 243 | return get_site_url(); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Get the admin url |
| 248 | * |
| 249 | * @return string |
| 250 | */ |
| 251 | public function get_admin_url() |
| 252 | { |
| 253 | return get_admin_url(); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Get the core plugin url |
| 258 | * |
| 259 | * @return string |
| 260 | */ |
| 261 | public function get_core_plugin_url() |
| 262 | { |
| 263 | return KIRKI_CORE_PLUGIN_URL; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Get the rest url |
| 268 | * |
| 269 | * @return string |
| 270 | */ |
| 271 | public function get_rest_url() |
| 272 | { |
| 273 | return rest_url(); |
| 274 | } |
| 275 | } |