| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Exceptions\Indexable; |
| 4 |
|
| 5 |
use Throwable; |
| 6 |
|
| 7 |
/** |
| 8 |
* Exception thrown when building an indexable fails because of an unexpected error. |
| 9 |
* |
| 10 |
* Wraps the original throwable and carries the identity of the object that could not be built, so the |
| 11 |
* failing object can be reported all the way to the REST response and the user interface. |
| 12 |
*/ |
| 13 |
class Indexing_Failed_Exception extends Indexable_Exception { |
| 14 |
|
| 15 |
/** |
| 16 |
* The object ID of the indexable that failed to build, or null for id-less object types |
| 17 |
* (e.g. home-page, system-page, date-archive, post-type-archive). |
| 18 |
* |
| 19 |
* @var int|null |
| 20 |
*/ |
| 21 |
private $object_id; |
| 22 |
|
| 23 |
/** |
| 24 |
* The object type of the indexable that failed to build. |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
private $object_type; |
| 29 |
|
| 30 |
/** |
| 31 |
* The object sub type of the indexable that failed to build. |
| 32 |
* |
| 33 |
* @var string|null |
| 34 |
*/ |
| 35 |
private $object_sub_type; |
| 36 |
|
| 37 |
/** |
| 38 |
* Constructs the exception. |
| 39 |
* |
| 40 |
* @param int|null $object_id The object ID of the indexable that failed to build, or null for id-less object types. |
| 41 |
* @param string $object_type The object type of the indexable that failed to build. |
| 42 |
* @param string|null $object_sub_type The object sub type of the indexable that failed to build. |
| 43 |
* @param Throwable $previous The error that caused the failure. |
| 44 |
*/ |
| 45 |
public function __construct( $object_id, $object_type, $object_sub_type, Throwable $previous ) { |
| 46 |
$this->object_id = $object_id; |
| 47 |
$this->object_type = $object_type; |
| 48 |
$this->object_sub_type = $object_sub_type; |
| 49 |
|
| 50 |
parent::__construct( |
| 51 |
\sprintf( |
| 52 |
/* translators: 1: description of the failing object (e.g. "post #123"); 2: underlying error message. */ |
| 53 |
\__( 'Yoast SEO could not build the indexable for %1$s: %2$s', 'wordpress-seo' ), |
| 54 |
$this->get_object_description(), |
| 55 |
$previous->getMessage(), |
| 56 |
), |
| 57 |
0, |
| 58 |
$previous, |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Gets the object ID of the indexable that failed to build. |
| 64 |
* |
| 65 |
* @return int|null The object ID, or null for id-less object types. |
| 66 |
*/ |
| 67 |
public function get_object_id() { |
| 68 |
return $this->object_id; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Gets the object type of the indexable that failed to build. |
| 73 |
* |
| 74 |
* @return string The object type. |
| 75 |
*/ |
| 76 |
public function get_object_type() { |
| 77 |
return $this->object_type; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Gets the object sub type of the indexable that failed to build. |
| 82 |
* |
| 83 |
* @return string|null The object sub type. |
| 84 |
*/ |
| 85 |
public function get_object_sub_type() { |
| 86 |
return $this->object_sub_type; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Describes the failing object as specifically as its indexable allows. |
| 91 |
* |
| 92 |
* Id-less object types fall back to the sub type when present (e.g. "system-page (404)", |
| 93 |
* "post-type-archive (book)") and to the bare object type otherwise (e.g. "home-page"). |
| 94 |
* |
| 95 |
* @return string The object description, e.g. "post #123", "system-page (404)" or "home-page". |
| 96 |
*/ |
| 97 |
public function get_object_description() { |
| 98 |
if ( $this->object_id !== null ) { |
| 99 |
return \sprintf( '%1$s #%2$d', $this->object_type, $this->object_id ); |
| 100 |
} |
| 101 |
|
| 102 |
if ( $this->object_sub_type !== null ) { |
| 103 |
return \sprintf( '%1$s (%2$s)', $this->object_type, $this->object_sub_type ); |
| 104 |
} |
| 105 |
|
| 106 |
return $this->object_type; |
| 107 |
} |
| 108 |
} |
| 109 |
|