| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong |
| 4 |
namespace Yoast\WP\SEO\Editors\Framework\Site; |
| 5 |
|
| 6 |
use WP_Taxonomy; |
| 7 |
use WP_Term; |
| 8 |
|
| 9 |
/** |
| 10 |
* The Term_Site_Information class. |
| 11 |
*/ |
| 12 |
class Term_Site_Information extends Base_Site_Information { |
| 13 |
|
| 14 |
/** |
| 15 |
* The taxonomy. |
| 16 |
* |
| 17 |
* @var WP_Taxonomy|false |
| 18 |
*/ |
| 19 |
private $taxonomy; |
| 20 |
|
| 21 |
/** |
| 22 |
* The term. |
| 23 |
* |
| 24 |
* @var WP_Term|string|false |
| 25 |
*/ |
| 26 |
private $term; |
| 27 |
|
| 28 |
/** |
| 29 |
* Sets the term for the information object and retrieves its taxonomy. |
| 30 |
* |
| 31 |
* @param WP_Term|string|false $term The term. |
| 32 |
* |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
public function set_term( $term ) { |
| 36 |
$this->term = $term; |
| 37 |
$this->taxonomy = \get_taxonomy( $term->taxonomy ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Returns term specific site information together with the generic site information. |
| 42 |
* |
| 43 |
* @return array<string, string|string[]> |
| 44 |
*/ |
| 45 |
public function get_site_information(): array { |
| 46 |
$data = [ |
| 47 |
'search_url' => $this->search_url(), |
| 48 |
'post_edit_url' => $this->edit_url(), |
| 49 |
'base_url' => $this->base_url_for_js(), |
| 50 |
]; |
| 51 |
|
| 52 |
return \array_merge_recursive( $data, parent::get_site_information() ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Returns term specific site information together with the generic site information. |
| 57 |
* |
| 58 |
* @return array<string, array<string, string>> |
| 59 |
*/ |
| 60 |
public function get_legacy_site_information(): array { |
| 61 |
$data = [ |
| 62 |
'metabox' => [ |
| 63 |
'search_url' => $this->search_url(), |
| 64 |
'post_edit_url' => $this->edit_url(), |
| 65 |
'base_url' => $this->base_url_for_js(), |
| 66 |
], |
| 67 |
]; |
| 68 |
|
| 69 |
return \array_merge_recursive( $data, parent::get_legacy_site_information() ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Returns the url to search for keyword for the taxonomy. |
| 74 |
* |
| 75 |
* @return string |
| 76 |
*/ |
| 77 |
private function search_url(): string { |
| 78 |
return \admin_url( 'edit-tags.php?taxonomy=' . $this->term->taxonomy . '&seo_kw_filter={keyword}' ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Returns the url to edit the taxonomy. |
| 83 |
* |
| 84 |
* @return string |
| 85 |
*/ |
| 86 |
private function edit_url(): string { |
| 87 |
return \admin_url( 'term.php?action=edit&taxonomy=' . $this->term->taxonomy . '&tag_ID={id}' ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Returns a base URL for use in the JS, takes permalink structure into account. |
| 92 |
* |
| 93 |
* @return string |
| 94 |
*/ |
| 95 |
private function base_url_for_js(): string { |
| 96 |
$base_url = \home_url( '/', null ); |
| 97 |
if ( ! $this->options_helper->get( 'stripcategorybase', false ) ) { |
| 98 |
if ( $this->taxonomy->rewrite ) { |
| 99 |
$base_url = \trailingslashit( $base_url . $this->taxonomy->rewrite['slug'] ); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return $base_url; |
| 104 |
} |
| 105 |
} |
| 106 |
|