| 1 |
<?php |
| 2 |
/** |
| 3 |
* Term Transformer Class file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\Transformer; |
| 9 |
|
| 10 |
/** |
| 11 |
* Term Transformer Class. |
| 12 |
*/ |
| 13 |
class Term extends Base { |
| 14 |
/** |
| 15 |
* Transforms the WP_Term object to an OrderedCollection. |
| 16 |
* |
| 17 |
* @see \Activitypub\Activity\Base_Object |
| 18 |
* |
| 19 |
* @return \Activitypub\Activity\Base_Object|\WP_Error The OrderedCollection or WP_Error on failure. |
| 20 |
*/ |
| 21 |
public function to_object() { |
| 22 |
$base_object = new \Activitypub\Activity\Base_Object(); |
| 23 |
$base_object->{'@context'} = 'https://www.w3.org/ns/activitystreams'; |
| 24 |
$base_object->set_type( 'OrderedCollection' ); |
| 25 |
$base_object->set_id( $this->get_id() ); |
| 26 |
$base_object->set_url( $this->get_url() ); |
| 27 |
|
| 28 |
return $base_object; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Get the OrderedCollection ID. |
| 33 |
* |
| 34 |
* @return string The OrderedCollection ID. |
| 35 |
*/ |
| 36 |
public function to_id() { |
| 37 |
return $this->get_id(); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Returns the stable ID of the Term. |
| 42 |
* |
| 43 |
* Uses term_id query parameter to ensure the ID remains stable |
| 44 |
* even if the term slug is changed. |
| 45 |
* |
| 46 |
* @return string The Term's stable ID. |
| 47 |
*/ |
| 48 |
public function get_id() { |
| 49 |
return \add_query_arg( 'term_id', $this->item->term_id, \home_url( '/' ) ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Returns the URL of the Term. |
| 54 |
* |
| 55 |
* @return string The Term's URL (term link). |
| 56 |
*/ |
| 57 |
public function get_url() { |
| 58 |
$term_link = \get_term_link( $this->item ); |
| 59 |
|
| 60 |
if ( \is_wp_error( $term_link ) ) { |
| 61 |
return ''; |
| 62 |
} |
| 63 |
|
| 64 |
return \esc_url( $term_link ); |
| 65 |
} |
| 66 |
} |
| 67 |
|