@@ -6,132 +6,132 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Cursor implements \Serializable, \JsonSerializable { |
| 8 | 8 | |
| 9 | - private $position; |
|
| 10 | - private $element; |
|
| 11 | - private $direction; |
|
| 12 | - |
|
| 13 | - /** |
|
| 14 | - * @throws Exception when one of the parameters has an invalid value. |
|
| 15 | - */ |
|
| 16 | - public function __construct( $position, $element, $direction ) { |
|
| 17 | - $this->set_position( $position ); |
|
| 18 | - $this->set_element( $element ); |
|
| 19 | - $this->set_direction( $direction ); |
|
| 20 | - } |
|
| 21 | - |
|
| 22 | - public function get_position() { |
|
| 23 | - return $this->position; |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - public function set_position( $value ) { |
|
| 27 | - $this->position = $value; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - public function get_element() { |
|
| 31 | - return $this->element; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @throws Exception when one of the parameters has an invalid value. |
|
| 36 | - */ |
|
| 37 | - public function set_element( $value ) { |
|
| 38 | - if ( ! in_array( $value, array( 'INCLUDED', 'EXCLUDED' ), true ) ) { |
|
| 39 | - throw new Exception( "Invalid value, only 'INCLUDED' or 'EXCLUDED' accepted." ); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - $this->element = $value; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - public function get_direction() { |
|
| 46 | - return $this->direction; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @throws Exception when one of the parameters has an invalid value. |
|
| 51 | - */ |
|
| 52 | - public function set_direction( $value ) { |
|
| 53 | - if ( ! in_array( $value, array( 'ASCENDING', 'DESCENDING' ), true ) ) { |
|
| 54 | - throw new Exception( "Invalid value, only 'ASCENDING' or 'DESCENDING' accepted." ); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - $this->direction = $value; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - public function to_base64_string() { |
|
| 61 | - // The requirement for cursors is to be obfuscated. |
|
| 62 | - // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
|
| 63 | - return base64_encode( wp_json_encode( $this ) ); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - public static function from_base64_string( $value ) { |
|
| 67 | - if ( ! is_string( $value ) ) { |
|
| 68 | - return self::empty_cursor(); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - try { |
|
| 72 | - // The requirement for cursors is to be obfuscated. |
|
| 73 | - // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
|
| 74 | - $json_string = base64_decode( $value, true ); |
|
| 75 | - $json_array = json_decode( $json_string, true ); |
|
| 76 | - |
|
| 77 | - return self::from_array( $json_array ); |
|
| 78 | - } catch ( Exception $e ) { |
|
| 79 | - return self::empty_cursor(); |
|
| 80 | - } |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * @throws Exception when one of the parameters is not accepted. |
|
| 85 | - */ |
|
| 86 | - public static function from_array( $data ) { |
|
| 87 | - return new Cursor( $data['position'], $data['element'], $data['direction'] ); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - public static function empty_cursor() { |
|
| 91 | - return new self( null, 'INCLUDED', 'ASCENDING' ); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - public function __serialize() { |
|
| 95 | - return array( |
|
| 96 | - 'position' => $this->position, |
|
| 97 | - 'element' => $this->element, |
|
| 98 | - 'direction' => $this->direction, |
|
| 99 | - ); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Controls how the object is represented during PHP serialization. |
|
| 104 | - * |
|
| 105 | - * @return string The PHP serialized representation of the object. |
|
| 106 | - */ |
|
| 107 | - public function serialize() { |
|
| 108 | - // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
|
| 109 | - return serialize( $this->__serialize() ); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * @throws Exception when unserialization fails. |
|
| 114 | - */ |
|
| 115 | - public function unserialize( $data ) { |
|
| 116 | - // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize |
|
| 117 | - $this->__unserialize( unserialize( $data ) ); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * @throws Exception when unserialization fails. |
|
| 122 | - */ |
|
| 123 | - public function __unserialize( array $data ) { |
|
| 124 | - $this->set_position( $data['position'] ); |
|
| 125 | - $this->set_element( $data['element'] ); |
|
| 126 | - $this->set_direction( $data['direction'] ); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - public function jsonSerialize() { |
|
| 130 | - return array( |
|
| 131 | - 'position' => $this->position, |
|
| 132 | - 'element' => $this->element, |
|
| 133 | - 'direction' => $this->direction, |
|
| 134 | - ); |
|
| 135 | - } |
|
| 9 | + private $position; |
|
| 10 | + private $element; |
|
| 11 | + private $direction; |
|
| 12 | + |
|
| 13 | + /** |
|
| 14 | + * @throws Exception when one of the parameters has an invalid value. |
|
| 15 | + */ |
|
| 16 | + public function __construct( $position, $element, $direction ) { |
|
| 17 | + $this->set_position( $position ); |
|
| 18 | + $this->set_element( $element ); |
|
| 19 | + $this->set_direction( $direction ); |
|
| 20 | + } |
|
| 21 | + |
|
| 22 | + public function get_position() { |
|
| 23 | + return $this->position; |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + public function set_position( $value ) { |
|
| 27 | + $this->position = $value; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + public function get_element() { |
|
| 31 | + return $this->element; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @throws Exception when one of the parameters has an invalid value. |
|
| 36 | + */ |
|
| 37 | + public function set_element( $value ) { |
|
| 38 | + if ( ! in_array( $value, array( 'INCLUDED', 'EXCLUDED' ), true ) ) { |
|
| 39 | + throw new Exception( "Invalid value, only 'INCLUDED' or 'EXCLUDED' accepted." ); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + $this->element = $value; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + public function get_direction() { |
|
| 46 | + return $this->direction; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @throws Exception when one of the parameters has an invalid value. |
|
| 51 | + */ |
|
| 52 | + public function set_direction( $value ) { |
|
| 53 | + if ( ! in_array( $value, array( 'ASCENDING', 'DESCENDING' ), true ) ) { |
|
| 54 | + throw new Exception( "Invalid value, only 'ASCENDING' or 'DESCENDING' accepted." ); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + $this->direction = $value; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + public function to_base64_string() { |
|
| 61 | + // The requirement for cursors is to be obfuscated. |
|
| 62 | + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
|
| 63 | + return base64_encode( wp_json_encode( $this ) ); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + public static function from_base64_string( $value ) { |
|
| 67 | + if ( ! is_string( $value ) ) { |
|
| 68 | + return self::empty_cursor(); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + try { |
|
| 72 | + // The requirement for cursors is to be obfuscated. |
|
| 73 | + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
|
| 74 | + $json_string = base64_decode( $value, true ); |
|
| 75 | + $json_array = json_decode( $json_string, true ); |
|
| 76 | + |
|
| 77 | + return self::from_array( $json_array ); |
|
| 78 | + } catch ( Exception $e ) { |
|
| 79 | + return self::empty_cursor(); |
|
| 80 | + } |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * @throws Exception when one of the parameters is not accepted. |
|
| 85 | + */ |
|
| 86 | + public static function from_array( $data ) { |
|
| 87 | + return new Cursor( $data['position'], $data['element'], $data['direction'] ); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + public static function empty_cursor() { |
|
| 91 | + return new self( null, 'INCLUDED', 'ASCENDING' ); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + public function __serialize() { |
|
| 95 | + return array( |
|
| 96 | + 'position' => $this->position, |
|
| 97 | + 'element' => $this->element, |
|
| 98 | + 'direction' => $this->direction, |
|
| 99 | + ); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Controls how the object is represented during PHP serialization. |
|
| 104 | + * |
|
| 105 | + * @return string The PHP serialized representation of the object. |
|
| 106 | + */ |
|
| 107 | + public function serialize() { |
|
| 108 | + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
|
| 109 | + return serialize( $this->__serialize() ); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * @throws Exception when unserialization fails. |
|
| 114 | + */ |
|
| 115 | + public function unserialize( $data ) { |
|
| 116 | + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize |
|
| 117 | + $this->__unserialize( unserialize( $data ) ); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * @throws Exception when unserialization fails. |
|
| 122 | + */ |
|
| 123 | + public function __unserialize( array $data ) { |
|
| 124 | + $this->set_position( $data['position'] ); |
|
| 125 | + $this->set_element( $data['element'] ); |
|
| 126 | + $this->set_direction( $data['direction'] ); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + public function jsonSerialize() { |
|
| 130 | + return array( |
|
| 131 | + 'position' => $this->position, |
|
| 132 | + 'element' => $this->element, |
|
| 133 | + 'direction' => $this->direction, |
|
| 134 | + ); |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | 137 | } |
@@ -13,17 +13,17 @@ discard block |
||
| 13 | 13 | /** |
| 14 | 14 | * @throws Exception when one of the parameters has an invalid value. |
| 15 | 15 | */ |
| 16 | - public function __construct( $position, $element, $direction ) { |
|
| 17 | - $this->set_position( $position ); |
|
| 18 | - $this->set_element( $element ); |
|
| 19 | - $this->set_direction( $direction ); |
|
| 16 | + public function __construct($position, $element, $direction) { |
|
| 17 | + $this->set_position($position); |
|
| 18 | + $this->set_element($element); |
|
| 19 | + $this->set_direction($direction); |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | public function get_position() { |
| 23 | 23 | return $this->position; |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | - public function set_position( $value ) { |
|
| 26 | + public function set_position($value) { |
|
| 27 | 27 | $this->position = $value; |
| 28 | 28 | } |
| 29 | 29 | |
@@ -34,9 +34,9 @@ discard block |
||
| 34 | 34 | /** |
| 35 | 35 | * @throws Exception when one of the parameters has an invalid value. |
| 36 | 36 | */ |
| 37 | - public function set_element( $value ) { |
|
| 38 | - if ( ! in_array( $value, array( 'INCLUDED', 'EXCLUDED' ), true ) ) { |
|
| 39 | - throw new Exception( "Invalid value, only 'INCLUDED' or 'EXCLUDED' accepted." ); |
|
| 37 | + public function set_element($value) { |
|
| 38 | + if ( ! in_array($value, array('INCLUDED', 'EXCLUDED'), true)) { |
|
| 39 | + throw new Exception("Invalid value, only 'INCLUDED' or 'EXCLUDED' accepted."); |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | $this->element = $value; |
@@ -49,9 +49,9 @@ discard block |
||
| 49 | 49 | /** |
| 50 | 50 | * @throws Exception when one of the parameters has an invalid value. |
| 51 | 51 | */ |
| 52 | - public function set_direction( $value ) { |
|
| 53 | - if ( ! in_array( $value, array( 'ASCENDING', 'DESCENDING' ), true ) ) { |
|
| 54 | - throw new Exception( "Invalid value, only 'ASCENDING' or 'DESCENDING' accepted." ); |
|
| 52 | + public function set_direction($value) { |
|
| 53 | + if ( ! in_array($value, array('ASCENDING', 'DESCENDING'), true)) { |
|
| 54 | + throw new Exception("Invalid value, only 'ASCENDING' or 'DESCENDING' accepted."); |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | $this->direction = $value; |
@@ -60,22 +60,22 @@ discard block |
||
| 60 | 60 | public function to_base64_string() { |
| 61 | 61 | // The requirement for cursors is to be obfuscated. |
| 62 | 62 | // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
| 63 | - return base64_encode( wp_json_encode( $this ) ); |
|
| 63 | + return base64_encode(wp_json_encode($this)); |
|
| 64 | 64 | } |
| 65 | 65 | |
| 66 | - public static function from_base64_string( $value ) { |
|
| 67 | - if ( ! is_string( $value ) ) { |
|
| 66 | + public static function from_base64_string($value) { |
|
| 67 | + if ( ! is_string($value)) { |
|
| 68 | 68 | return self::empty_cursor(); |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | try { |
| 72 | 72 | // The requirement for cursors is to be obfuscated. |
| 73 | 73 | // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode |
| 74 | - $json_string = base64_decode( $value, true ); |
|
| 75 | - $json_array = json_decode( $json_string, true ); |
|
| 74 | + $json_string = base64_decode($value, true); |
|
| 75 | + $json_array = json_decode($json_string, true); |
|
| 76 | 76 | |
| 77 | - return self::from_array( $json_array ); |
|
| 78 | - } catch ( Exception $e ) { |
|
| 77 | + return self::from_array($json_array); |
|
| 78 | + } catch (Exception $e) { |
|
| 79 | 79 | return self::empty_cursor(); |
| 80 | 80 | } |
| 81 | 81 | } |
@@ -83,12 +83,12 @@ discard block |
||
| 83 | 83 | /** |
| 84 | 84 | * @throws Exception when one of the parameters is not accepted. |
| 85 | 85 | */ |
| 86 | - public static function from_array( $data ) { |
|
| 87 | - return new Cursor( $data['position'], $data['element'], $data['direction'] ); |
|
| 86 | + public static function from_array($data) { |
|
| 87 | + return new Cursor($data['position'], $data['element'], $data['direction']); |
|
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | public static function empty_cursor() { |
| 91 | - return new self( null, 'INCLUDED', 'ASCENDING' ); |
|
| 91 | + return new self(null, 'INCLUDED', 'ASCENDING'); |
|
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | public function __serialize() { |
@@ -106,24 +106,24 @@ discard block |
||
| 106 | 106 | */ |
| 107 | 107 | public function serialize() { |
| 108 | 108 | // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
| 109 | - return serialize( $this->__serialize() ); |
|
| 109 | + return serialize($this->__serialize()); |
|
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | /** |
| 113 | 113 | * @throws Exception when unserialization fails. |
| 114 | 114 | */ |
| 115 | - public function unserialize( $data ) { |
|
| 115 | + public function unserialize($data) { |
|
| 116 | 116 | // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize |
| 117 | - $this->__unserialize( unserialize( $data ) ); |
|
| 117 | + $this->__unserialize(unserialize($data)); |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | /** |
| 121 | 121 | * @throws Exception when unserialization fails. |
| 122 | 122 | */ |
| 123 | - public function __unserialize( array $data ) { |
|
| 124 | - $this->set_position( $data['position'] ); |
|
| 125 | - $this->set_element( $data['element'] ); |
|
| 126 | - $this->set_direction( $data['direction'] ); |
|
| 123 | + public function __unserialize(array $data) { |
|
| 124 | + $this->set_position($data['position']); |
|
| 125 | + $this->set_element($data['element']); |
|
| 126 | + $this->set_direction($data['direction']); |
|
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | public function jsonSerialize() { |
@@ -4,48 +4,48 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Cursor_Sort { |
| 6 | 6 | |
| 7 | - private static $sort_property_to_colname = array( |
|
| 8 | - 'date_modified_gmt' => 'post_modified_gmt', |
|
| 9 | - 'term_name' => 'name', |
|
| 10 | - ); |
|
| 11 | - /** |
|
| 12 | - * @var false|string |
|
| 13 | - */ |
|
| 14 | - private $sort_property; |
|
| 15 | - /** |
|
| 16 | - * @var mixed |
|
| 17 | - */ |
|
| 18 | - private $sort_colname; |
|
| 19 | - /** |
|
| 20 | - * @var string |
|
| 21 | - */ |
|
| 22 | - private $sort_direction; |
|
| 23 | - |
|
| 24 | - public function __construct( $sort ) { |
|
| 25 | - $this->sort_property = substr( $sort, 1 ); |
|
| 26 | - $this->sort_colname = isset( self::$sort_property_to_colname[ $this->sort_property ] ) ? self::$sort_property_to_colname[ $this->sort_property ] : $this->sort_property; |
|
| 27 | - $this->sort_direction = substr( $sort, 0, 1 ) === '+' ? 'ASC' : 'DESC'; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @return false|string |
|
| 32 | - */ |
|
| 33 | - public function get_sort_property() { |
|
| 34 | - return $this->sort_property; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @return false|mixed|string |
|
| 39 | - */ |
|
| 40 | - public function get_sort_colname() { |
|
| 41 | - return $this->sort_colname; |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @return string |
|
| 46 | - */ |
|
| 47 | - public function get_sort_direction() { |
|
| 48 | - return $this->sort_direction; |
|
| 49 | - } |
|
| 7 | + private static $sort_property_to_colname = array( |
|
| 8 | + 'date_modified_gmt' => 'post_modified_gmt', |
|
| 9 | + 'term_name' => 'name', |
|
| 10 | + ); |
|
| 11 | + /** |
|
| 12 | + * @var false|string |
|
| 13 | + */ |
|
| 14 | + private $sort_property; |
|
| 15 | + /** |
|
| 16 | + * @var mixed |
|
| 17 | + */ |
|
| 18 | + private $sort_colname; |
|
| 19 | + /** |
|
| 20 | + * @var string |
|
| 21 | + */ |
|
| 22 | + private $sort_direction; |
|
| 23 | + |
|
| 24 | + public function __construct( $sort ) { |
|
| 25 | + $this->sort_property = substr( $sort, 1 ); |
|
| 26 | + $this->sort_colname = isset( self::$sort_property_to_colname[ $this->sort_property ] ) ? self::$sort_property_to_colname[ $this->sort_property ] : $this->sort_property; |
|
| 27 | + $this->sort_direction = substr( $sort, 0, 1 ) === '+' ? 'ASC' : 'DESC'; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @return false|string |
|
| 32 | + */ |
|
| 33 | + public function get_sort_property() { |
|
| 34 | + return $this->sort_property; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @return false|mixed|string |
|
| 39 | + */ |
|
| 40 | + public function get_sort_colname() { |
|
| 41 | + return $this->sort_colname; |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @return string |
|
| 46 | + */ |
|
| 47 | + public function get_sort_direction() { |
|
| 48 | + return $this->sort_direction; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | 51 | } |
@@ -20,10 +20,10 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | private $sort_direction; |
| 22 | 22 | |
| 23 | - public function __construct( $sort ) { |
|
| 24 | - $this->sort_property = substr( $sort, 1 ); |
|
| 25 | - $this->sort_colname = isset( self::$sort_property_to_colname[ $this->sort_property ] ) ? self::$sort_property_to_colname[ $this->sort_property ] : $this->sort_property; |
|
| 26 | - $this->sort_direction = substr( $sort, 0, 1 ) === '+' ? 'ASC' : 'DESC'; |
|
| 23 | + public function __construct($sort) { |
|
| 24 | + $this->sort_property = substr($sort, 1); |
|
| 25 | + $this->sort_colname = isset(self::$sort_property_to_colname[$this->sort_property]) ? self::$sort_property_to_colname[$this->sort_property] : $this->sort_property; |
|
| 26 | + $this->sort_direction = substr($sort, 0, 1) === '+' ? 'ASC' : 'DESC'; |
|
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | /** |
@@ -4,70 +4,70 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Page implements \Serializable, \JsonSerializable { |
| 6 | 6 | |
| 7 | - /** |
|
| 8 | - * @var mixed |
|
| 9 | - */ |
|
| 10 | - private $items; |
|
| 7 | + /** |
|
| 8 | + * @var mixed |
|
| 9 | + */ |
|
| 10 | + private $items; |
|
| 11 | 11 | |
| 12 | - private $self; |
|
| 13 | - private $first; |
|
| 14 | - private $prev; |
|
| 15 | - private $next; |
|
| 16 | - private $last; |
|
| 12 | + private $self; |
|
| 13 | + private $first; |
|
| 14 | + private $prev; |
|
| 15 | + private $next; |
|
| 16 | + private $last; |
|
| 17 | 17 | |
| 18 | - public function __construct( $items, $self, $first, $prev, $next, $last ) { |
|
| 19 | - $this->items = $items; |
|
| 20 | - $this->self = $self; |
|
| 21 | - $this->first = $first; |
|
| 22 | - $this->prev = $prev; |
|
| 23 | - $this->next = $next; |
|
| 24 | - $this->last = $last; |
|
| 25 | - } |
|
| 18 | + public function __construct( $items, $self, $first, $prev, $next, $last ) { |
|
| 19 | + $this->items = $items; |
|
| 20 | + $this->self = $self; |
|
| 21 | + $this->first = $first; |
|
| 22 | + $this->prev = $prev; |
|
| 23 | + $this->next = $next; |
|
| 24 | + $this->last = $last; |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - public function __serialize() { |
|
| 28 | - return array( |
|
| 29 | - 'self' => $this->self, |
|
| 30 | - 'first' => $this->first, |
|
| 31 | - 'prev' => $this->prev, |
|
| 32 | - 'next' => $this->next, |
|
| 33 | - 'last' => $this->last, |
|
| 34 | - 'items' => $this->items, |
|
| 35 | - ); |
|
| 36 | - } |
|
| 27 | + public function __serialize() { |
|
| 28 | + return array( |
|
| 29 | + 'self' => $this->self, |
|
| 30 | + 'first' => $this->first, |
|
| 31 | + 'prev' => $this->prev, |
|
| 32 | + 'next' => $this->next, |
|
| 33 | + 'last' => $this->last, |
|
| 34 | + 'items' => $this->items, |
|
| 35 | + ); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Controls how the object is represented during PHP serialization. |
|
| 40 | - * |
|
| 41 | - * @return string The PHP serialized representation of the object. |
|
| 42 | - */ |
|
| 43 | - public function serialize() { |
|
| 44 | - // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
|
| 45 | - return serialize( $this->__serialize() ); |
|
| 46 | - } |
|
| 38 | + /** |
|
| 39 | + * Controls how the object is represented during PHP serialization. |
|
| 40 | + * |
|
| 41 | + * @return string The PHP serialized representation of the object. |
|
| 42 | + */ |
|
| 43 | + public function serialize() { |
|
| 44 | + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
|
| 45 | + return serialize( $this->__serialize() ); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - public function unserialize( $data ) { |
|
| 49 | - // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize |
|
| 50 | - $this->__unserialize( unserialize( $data ) ); |
|
| 51 | - } |
|
| 48 | + public function unserialize( $data ) { |
|
| 49 | + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize |
|
| 50 | + $this->__unserialize( unserialize( $data ) ); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - public function __unserialize( array $data ) { |
|
| 54 | - $this->self = $data['self']; |
|
| 55 | - $this->first = $data['first']; |
|
| 56 | - $this->prev = $data['prev']; |
|
| 57 | - $this->next = $data['next']; |
|
| 58 | - $this->last = $data['last']; |
|
| 59 | - $this->items = $data['items']; |
|
| 60 | - } |
|
| 53 | + public function __unserialize( array $data ) { |
|
| 54 | + $this->self = $data['self']; |
|
| 55 | + $this->first = $data['first']; |
|
| 56 | + $this->prev = $data['prev']; |
|
| 57 | + $this->next = $data['next']; |
|
| 58 | + $this->last = $data['last']; |
|
| 59 | + $this->items = $data['items']; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - public function jsonSerialize() { |
|
| 63 | - return array( |
|
| 64 | - 'self' => $this->self, |
|
| 65 | - 'first' => $this->first, |
|
| 66 | - 'prev' => $this->prev, |
|
| 67 | - 'next' => $this->next, |
|
| 68 | - 'last' => $this->last, |
|
| 69 | - 'items' => $this->items, |
|
| 70 | - ); |
|
| 71 | - } |
|
| 62 | + public function jsonSerialize() { |
|
| 63 | + return array( |
|
| 64 | + 'self' => $this->self, |
|
| 65 | + 'first' => $this->first, |
|
| 66 | + 'prev' => $this->prev, |
|
| 67 | + 'next' => $this->next, |
|
| 68 | + 'last' => $this->last, |
|
| 69 | + 'items' => $this->items, |
|
| 70 | + ); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | 73 | } |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | private $next; |
| 16 | 16 | private $last; |
| 17 | 17 | |
| 18 | - public function __construct( $items, $self, $first, $prev, $next, $last ) { |
|
| 18 | + public function __construct($items, $self, $first, $prev, $next, $last) { |
|
| 19 | 19 | $this->items = $items; |
| 20 | 20 | $this->self = $self; |
| 21 | 21 | $this->first = $first; |
@@ -42,15 +42,15 @@ discard block |
||
| 42 | 42 | */ |
| 43 | 43 | public function serialize() { |
| 44 | 44 | // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize |
| 45 | - return serialize( $this->__serialize() ); |
|
| 45 | + return serialize($this->__serialize()); |
|
| 46 | 46 | } |
| 47 | 47 | |
| 48 | - public function unserialize( $data ) { |
|
| 48 | + public function unserialize($data) { |
|
| 49 | 49 | // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize |
| 50 | - $this->__unserialize( unserialize( $data ) ); |
|
| 50 | + $this->__unserialize(unserialize($data)); |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | - public function __unserialize( array $data ) { |
|
| 53 | + public function __unserialize(array $data) { |
|
| 54 | 54 | $this->self = $data['self']; |
| 55 | 55 | $this->first = $data['first']; |
| 56 | 56 | $this->prev = $data['prev']; |
@@ -7,52 +7,52 @@ discard block |
||
| 7 | 7 | use WP_REST_Request; |
| 8 | 8 | |
| 9 | 9 | class Term_Query { |
| 10 | - /** |
|
| 11 | - * @var WP_REST_Request |
|
| 12 | - */ |
|
| 13 | - private $request; |
|
| 14 | - /** |
|
| 15 | - * @var mixed |
|
| 16 | - */ |
|
| 17 | - private $position; |
|
| 18 | - /** |
|
| 19 | - * @var mixed |
|
| 20 | - */ |
|
| 21 | - private $element; |
|
| 22 | - /** |
|
| 23 | - * @var mixed |
|
| 24 | - */ |
|
| 25 | - private $direction; |
|
| 26 | - |
|
| 27 | - private $sort; |
|
| 28 | - |
|
| 29 | - private $sortby; |
|
| 30 | - /** |
|
| 31 | - * @var mixed |
|
| 32 | - */ |
|
| 33 | - private $limit; |
|
| 34 | - |
|
| 35 | - /** @var Cursor_Sort $cursor_sort */ |
|
| 36 | - private $cursor_sort; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @param WP_REST_Request $request |
|
| 40 | - * @param Cursor $cursor |
|
| 41 | - */ |
|
| 42 | - public function __construct( $request, $cursor, $cursor_sort, $limit ) { |
|
| 43 | - global $wpdb; |
|
| 44 | - |
|
| 45 | - $this->request = $request; |
|
| 46 | - $this->position = $cursor->get_position(); |
|
| 47 | - $this->element = $cursor->get_element(); |
|
| 48 | - $this->direction = $cursor->get_direction(); |
|
| 49 | - $this->limit = $limit; |
|
| 50 | - $this->cursor_sort = $cursor_sort; |
|
| 51 | - |
|
| 52 | - $this->set_sort(); |
|
| 53 | - |
|
| 54 | - // the `term_name` is required for sort. |
|
| 55 | - $this->sql = " |
|
| 10 | + /** |
|
| 11 | + * @var WP_REST_Request |
|
| 12 | + */ |
|
| 13 | + private $request; |
|
| 14 | + /** |
|
| 15 | + * @var mixed |
|
| 16 | + */ |
|
| 17 | + private $position; |
|
| 18 | + /** |
|
| 19 | + * @var mixed |
|
| 20 | + */ |
|
| 21 | + private $element; |
|
| 22 | + /** |
|
| 23 | + * @var mixed |
|
| 24 | + */ |
|
| 25 | + private $direction; |
|
| 26 | + |
|
| 27 | + private $sort; |
|
| 28 | + |
|
| 29 | + private $sortby; |
|
| 30 | + /** |
|
| 31 | + * @var mixed |
|
| 32 | + */ |
|
| 33 | + private $limit; |
|
| 34 | + |
|
| 35 | + /** @var Cursor_Sort $cursor_sort */ |
|
| 36 | + private $cursor_sort; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @param WP_REST_Request $request |
|
| 40 | + * @param Cursor $cursor |
|
| 41 | + */ |
|
| 42 | + public function __construct( $request, $cursor, $cursor_sort, $limit ) { |
|
| 43 | + global $wpdb; |
|
| 44 | + |
|
| 45 | + $this->request = $request; |
|
| 46 | + $this->position = $cursor->get_position(); |
|
| 47 | + $this->element = $cursor->get_element(); |
|
| 48 | + $this->direction = $cursor->get_direction(); |
|
| 49 | + $this->limit = $limit; |
|
| 50 | + $this->cursor_sort = $cursor_sort; |
|
| 51 | + |
|
| 52 | + $this->set_sort(); |
|
| 53 | + |
|
| 54 | + // the `term_name` is required for sort. |
|
| 55 | + $this->sql = " |
|
| 56 | 56 | SELECT t.term_id as id, |
| 57 | 57 | e.about_jsonld as match_jsonld, |
| 58 | 58 | t.name, |
@@ -66,140 +66,140 @@ discard block |
||
| 66 | 66 | WHERE 1=1 |
| 67 | 67 | "; |
| 68 | 68 | |
| 69 | - $this->cursor(); |
|
| 70 | - $this->has_match(); |
|
| 71 | - $this->term_contains(); |
|
| 72 | - $this->taxonomies(); |
|
| 73 | - $this->sort(); |
|
| 74 | - $this->limit(); |
|
| 75 | - |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - public function get_results() { |
|
| 79 | - global $wpdb; |
|
| 80 | - |
|
| 81 | - // The `sql` is prepared in each delegated function in this class. |
|
| 82 | - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 83 | - $items = $wpdb->get_results( $this->sql ); |
|
| 84 | - |
|
| 85 | - $sort = ( $this->sort === 'ASC' ? SORT_ASC : SORT_DESC ); |
|
| 86 | - array_multisort( array_column( $items, $this->cursor_sort->get_sort_property() ), $sort, $items ); |
|
| 87 | - $items = array_map( array( $this, 'map_item' ), $items ); |
|
| 88 | - |
|
| 89 | - return $items; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - public function map_item( $item ) { |
|
| 93 | - $item->match_name = $this->get_match_name( $item->match_jsonld ); |
|
| 94 | - |
|
| 95 | - return $item; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - private function get_match_name( $jsonld ) { |
|
| 99 | - $data = json_decode( $jsonld, true ); |
|
| 100 | - if ( ! $data || ! array_key_exists( 'name', $data ) ) { |
|
| 101 | - return null; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - return $data['name']; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - private function post_types() { |
|
| 108 | - $post_types = $this->request->has_param( 'post_types' ) |
|
| 109 | - ? (array) $this->request->get_param( 'post_types' ) |
|
| 110 | - : array( 'post', 'page' ); |
|
| 111 | - $value = array_map( 'esc_sql', $post_types ); |
|
| 112 | - $this->sql .= " AND p.post_type IN ( '" . implode( "', '", $value ) . "' )"; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - private function limit() { |
|
| 116 | - $value = is_numeric( $this->limit ) ? $this->limit : 10; |
|
| 117 | - $this->sql .= ' LIMIT ' . esc_sql( $value ); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - private function has_match() { |
|
| 121 | - if ( ! $this->request->has_param( 'has_match' ) ) { |
|
| 122 | - return; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - $value = (bool) $this->request->get_param( 'has_match' ); |
|
| 126 | - |
|
| 127 | - if ( $value ) { |
|
| 128 | - $this->sql .= ' AND e.about_jsonld IS NOT NULL'; |
|
| 129 | - } else { |
|
| 130 | - $this->sql .= ' AND e.about_jsonld IS NULL'; |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - private function sort() { |
|
| 135 | - switch ( $this->direction . '$' . $this->sort ) { |
|
| 136 | - case 'ASCENDING$ASC': |
|
| 137 | - case 'DESCENDING$DESC': |
|
| 138 | - $sort = 'ASC'; |
|
| 139 | - break; |
|
| 140 | - case 'ASCENDING$DESC': |
|
| 141 | - case 'DESCENDING$ASC': |
|
| 142 | - $sort = 'DESC'; |
|
| 143 | - break; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - $this->sql .= ' ORDER BY t.' . $this->sortby . ' ' . $sort; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - private function cursor() { |
|
| 150 | - if ( ! isset( $this->position ) ) { |
|
| 151 | - return; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - switch ( $this->direction . '$' . $this->sort ) { |
|
| 155 | - case 'ASCENDING$ASC': |
|
| 156 | - case 'DESCENDING$DESC': |
|
| 157 | - $condition = '>'; |
|
| 158 | - break; |
|
| 159 | - case 'ASCENDING$DESC': |
|
| 160 | - case 'DESCENDING$ASC': |
|
| 161 | - $condition = '<'; |
|
| 162 | - break; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - $condition .= ( $this->element === 'INCLUDED' ? '=' : '' ); |
|
| 166 | - global $wpdb; |
|
| 167 | - // We control the vars in this method. |
|
| 168 | - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 169 | - $this->sql .= $wpdb->prepare( ' AND t.' . esc_sql( $this->sortby ) . ' ' . $condition . ' %s', $this->position ); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - private function set_sort() { |
|
| 173 | - $sortby_to_col = array( |
|
| 174 | - // sort param col |
|
| 175 | - 'term_name' => 'name', |
|
| 176 | - ); |
|
| 177 | - |
|
| 178 | - $value = $this->request->has_param( 'sort' ) |
|
| 179 | - ? $this->request->get_param( 'sort' ) |
|
| 180 | - : '+term_name'; |
|
| 181 | - |
|
| 182 | - $sortby = substr( $value, 1 ); |
|
| 183 | - $this->sortby = isset( $sortby_to_col[ $sortby ] ) ? $sortby_to_col[ $sortby ] : $sortby; |
|
| 184 | - $this->sort = substr( $value, 0, 1 ) === '+' ? 'ASC' : 'DESC'; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - private function term_contains() { |
|
| 188 | - if ( ! $this->request->has_param( 'term_contains' ) ) { |
|
| 189 | - return; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - global $wpdb; |
|
| 193 | - $value = $this->request->get_param( 'term_contains' ); |
|
| 194 | - $this->sql .= $wpdb->prepare( ' and t . name LIKE %s', '%' . esc_sql( $value ) . '%' ); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - private function taxonomies() { |
|
| 198 | - $taxonomies = $this->request->has_param( 'taxonomies' ) |
|
| 199 | - ? (array) $this->request->get_param( 'taxonomies' ) |
|
| 200 | - : array( 'post_tag', 'category' ); |
|
| 201 | - $value = array_map( 'esc_sql', $taxonomies ); |
|
| 202 | - $this->sql .= " AND tt.taxonomy IN ( '" . implode( "', '", $value ) . "' )"; |
|
| 203 | - } |
|
| 69 | + $this->cursor(); |
|
| 70 | + $this->has_match(); |
|
| 71 | + $this->term_contains(); |
|
| 72 | + $this->taxonomies(); |
|
| 73 | + $this->sort(); |
|
| 74 | + $this->limit(); |
|
| 75 | + |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + public function get_results() { |
|
| 79 | + global $wpdb; |
|
| 80 | + |
|
| 81 | + // The `sql` is prepared in each delegated function in this class. |
|
| 82 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 83 | + $items = $wpdb->get_results( $this->sql ); |
|
| 84 | + |
|
| 85 | + $sort = ( $this->sort === 'ASC' ? SORT_ASC : SORT_DESC ); |
|
| 86 | + array_multisort( array_column( $items, $this->cursor_sort->get_sort_property() ), $sort, $items ); |
|
| 87 | + $items = array_map( array( $this, 'map_item' ), $items ); |
|
| 88 | + |
|
| 89 | + return $items; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + public function map_item( $item ) { |
|
| 93 | + $item->match_name = $this->get_match_name( $item->match_jsonld ); |
|
| 94 | + |
|
| 95 | + return $item; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + private function get_match_name( $jsonld ) { |
|
| 99 | + $data = json_decode( $jsonld, true ); |
|
| 100 | + if ( ! $data || ! array_key_exists( 'name', $data ) ) { |
|
| 101 | + return null; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + return $data['name']; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + private function post_types() { |
|
| 108 | + $post_types = $this->request->has_param( 'post_types' ) |
|
| 109 | + ? (array) $this->request->get_param( 'post_types' ) |
|
| 110 | + : array( 'post', 'page' ); |
|
| 111 | + $value = array_map( 'esc_sql', $post_types ); |
|
| 112 | + $this->sql .= " AND p.post_type IN ( '" . implode( "', '", $value ) . "' )"; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + private function limit() { |
|
| 116 | + $value = is_numeric( $this->limit ) ? $this->limit : 10; |
|
| 117 | + $this->sql .= ' LIMIT ' . esc_sql( $value ); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + private function has_match() { |
|
| 121 | + if ( ! $this->request->has_param( 'has_match' ) ) { |
|
| 122 | + return; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + $value = (bool) $this->request->get_param( 'has_match' ); |
|
| 126 | + |
|
| 127 | + if ( $value ) { |
|
| 128 | + $this->sql .= ' AND e.about_jsonld IS NOT NULL'; |
|
| 129 | + } else { |
|
| 130 | + $this->sql .= ' AND e.about_jsonld IS NULL'; |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + private function sort() { |
|
| 135 | + switch ( $this->direction . '$' . $this->sort ) { |
|
| 136 | + case 'ASCENDING$ASC': |
|
| 137 | + case 'DESCENDING$DESC': |
|
| 138 | + $sort = 'ASC'; |
|
| 139 | + break; |
|
| 140 | + case 'ASCENDING$DESC': |
|
| 141 | + case 'DESCENDING$ASC': |
|
| 142 | + $sort = 'DESC'; |
|
| 143 | + break; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + $this->sql .= ' ORDER BY t.' . $this->sortby . ' ' . $sort; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + private function cursor() { |
|
| 150 | + if ( ! isset( $this->position ) ) { |
|
| 151 | + return; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + switch ( $this->direction . '$' . $this->sort ) { |
|
| 155 | + case 'ASCENDING$ASC': |
|
| 156 | + case 'DESCENDING$DESC': |
|
| 157 | + $condition = '>'; |
|
| 158 | + break; |
|
| 159 | + case 'ASCENDING$DESC': |
|
| 160 | + case 'DESCENDING$ASC': |
|
| 161 | + $condition = '<'; |
|
| 162 | + break; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + $condition .= ( $this->element === 'INCLUDED' ? '=' : '' ); |
|
| 166 | + global $wpdb; |
|
| 167 | + // We control the vars in this method. |
|
| 168 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 169 | + $this->sql .= $wpdb->prepare( ' AND t.' . esc_sql( $this->sortby ) . ' ' . $condition . ' %s', $this->position ); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + private function set_sort() { |
|
| 173 | + $sortby_to_col = array( |
|
| 174 | + // sort param col |
|
| 175 | + 'term_name' => 'name', |
|
| 176 | + ); |
|
| 177 | + |
|
| 178 | + $value = $this->request->has_param( 'sort' ) |
|
| 179 | + ? $this->request->get_param( 'sort' ) |
|
| 180 | + : '+term_name'; |
|
| 181 | + |
|
| 182 | + $sortby = substr( $value, 1 ); |
|
| 183 | + $this->sortby = isset( $sortby_to_col[ $sortby ] ) ? $sortby_to_col[ $sortby ] : $sortby; |
|
| 184 | + $this->sort = substr( $value, 0, 1 ) === '+' ? 'ASC' : 'DESC'; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + private function term_contains() { |
|
| 188 | + if ( ! $this->request->has_param( 'term_contains' ) ) { |
|
| 189 | + return; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + global $wpdb; |
|
| 193 | + $value = $this->request->get_param( 'term_contains' ); |
|
| 194 | + $this->sql .= $wpdb->prepare( ' and t . name LIKE %s', '%' . esc_sql( $value ) . '%' ); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + private function taxonomies() { |
|
| 198 | + $taxonomies = $this->request->has_param( 'taxonomies' ) |
|
| 199 | + ? (array) $this->request->get_param( 'taxonomies' ) |
|
| 200 | + : array( 'post_tag', 'category' ); |
|
| 201 | + $value = array_map( 'esc_sql', $taxonomies ); |
|
| 202 | + $this->sql .= " AND tt.taxonomy IN ( '" . implode( "', '", $value ) . "' )"; |
|
| 203 | + } |
|
| 204 | 204 | |
| 205 | 205 | } |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | * @param WP_REST_Request $request |
| 40 | 40 | * @param Cursor $cursor |
| 41 | 41 | */ |
| 42 | - public function __construct( $request, $cursor, $cursor_sort, $limit ) { |
|
| 42 | + public function __construct($request, $cursor, $cursor_sort, $limit) { |
|
| 43 | 43 | global $wpdb; |
| 44 | 44 | |
| 45 | 45 | $this->request = $request; |
@@ -80,24 +80,24 @@ discard block |
||
| 80 | 80 | |
| 81 | 81 | // The `sql` is prepared in each delegated function in this class. |
| 82 | 82 | // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 83 | - $items = $wpdb->get_results( $this->sql ); |
|
| 83 | + $items = $wpdb->get_results($this->sql); |
|
| 84 | 84 | |
| 85 | - $sort = ( $this->sort === 'ASC' ? SORT_ASC : SORT_DESC ); |
|
| 86 | - array_multisort( array_column( $items, $this->cursor_sort->get_sort_property() ), $sort, $items ); |
|
| 87 | - $items = array_map( array( $this, 'map_item' ), $items ); |
|
| 85 | + $sort = ($this->sort === 'ASC' ? SORT_ASC : SORT_DESC); |
|
| 86 | + array_multisort(array_column($items, $this->cursor_sort->get_sort_property()), $sort, $items); |
|
| 87 | + $items = array_map(array($this, 'map_item'), $items); |
|
| 88 | 88 | |
| 89 | 89 | return $items; |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | - public function map_item( $item ) { |
|
| 93 | - $item->match_name = $this->get_match_name( $item->match_jsonld ); |
|
| 92 | + public function map_item($item) { |
|
| 93 | + $item->match_name = $this->get_match_name($item->match_jsonld); |
|
| 94 | 94 | |
| 95 | 95 | return $item; |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | - private function get_match_name( $jsonld ) { |
|
| 99 | - $data = json_decode( $jsonld, true ); |
|
| 100 | - if ( ! $data || ! array_key_exists( 'name', $data ) ) { |
|
| 98 | + private function get_match_name($jsonld) { |
|
| 99 | + $data = json_decode($jsonld, true); |
|
| 100 | + if ( ! $data || ! array_key_exists('name', $data)) { |
|
| 101 | 101 | return null; |
| 102 | 102 | } |
| 103 | 103 | |
@@ -105,26 +105,26 @@ discard block |
||
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | private function post_types() { |
| 108 | - $post_types = $this->request->has_param( 'post_types' ) |
|
| 109 | - ? (array) $this->request->get_param( 'post_types' ) |
|
| 110 | - : array( 'post', 'page' ); |
|
| 111 | - $value = array_map( 'esc_sql', $post_types ); |
|
| 112 | - $this->sql .= " AND p.post_type IN ( '" . implode( "', '", $value ) . "' )"; |
|
| 108 | + $post_types = $this->request->has_param('post_types') |
|
| 109 | + ? (array) $this->request->get_param('post_types') |
|
| 110 | + : array('post', 'page'); |
|
| 111 | + $value = array_map('esc_sql', $post_types); |
|
| 112 | + $this->sql .= " AND p.post_type IN ( '".implode("', '", $value)."' )"; |
|
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | private function limit() { |
| 116 | - $value = is_numeric( $this->limit ) ? $this->limit : 10; |
|
| 117 | - $this->sql .= ' LIMIT ' . esc_sql( $value ); |
|
| 116 | + $value = is_numeric($this->limit) ? $this->limit : 10; |
|
| 117 | + $this->sql .= ' LIMIT '.esc_sql($value); |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | private function has_match() { |
| 121 | - if ( ! $this->request->has_param( 'has_match' ) ) { |
|
| 121 | + if ( ! $this->request->has_param('has_match')) { |
|
| 122 | 122 | return; |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | - $value = (bool) $this->request->get_param( 'has_match' ); |
|
| 125 | + $value = (bool) $this->request->get_param('has_match'); |
|
| 126 | 126 | |
| 127 | - if ( $value ) { |
|
| 127 | + if ($value) { |
|
| 128 | 128 | $this->sql .= ' AND e.about_jsonld IS NOT NULL'; |
| 129 | 129 | } else { |
| 130 | 130 | $this->sql .= ' AND e.about_jsonld IS NULL'; |
@@ -132,7 +132,7 @@ discard block |
||
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | private function sort() { |
| 135 | - switch ( $this->direction . '$' . $this->sort ) { |
|
| 135 | + switch ($this->direction.'$'.$this->sort) { |
|
| 136 | 136 | case 'ASCENDING$ASC': |
| 137 | 137 | case 'DESCENDING$DESC': |
| 138 | 138 | $sort = 'ASC'; |
@@ -143,15 +143,15 @@ discard block |
||
| 143 | 143 | break; |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | - $this->sql .= ' ORDER BY t.' . $this->sortby . ' ' . $sort; |
|
| 146 | + $this->sql .= ' ORDER BY t.'.$this->sortby.' '.$sort; |
|
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | private function cursor() { |
| 150 | - if ( ! isset( $this->position ) ) { |
|
| 150 | + if ( ! isset($this->position)) { |
|
| 151 | 151 | return; |
| 152 | 152 | } |
| 153 | 153 | |
| 154 | - switch ( $this->direction . '$' . $this->sort ) { |
|
| 154 | + switch ($this->direction.'$'.$this->sort) { |
|
| 155 | 155 | case 'ASCENDING$ASC': |
| 156 | 156 | case 'DESCENDING$DESC': |
| 157 | 157 | $condition = '>'; |
@@ -162,11 +162,11 @@ discard block |
||
| 162 | 162 | break; |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | - $condition .= ( $this->element === 'INCLUDED' ? '=' : '' ); |
|
| 165 | + $condition .= ($this->element === 'INCLUDED' ? '=' : ''); |
|
| 166 | 166 | global $wpdb; |
| 167 | 167 | // We control the vars in this method. |
| 168 | 168 | // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 169 | - $this->sql .= $wpdb->prepare( ' AND t.' . esc_sql( $this->sortby ) . ' ' . $condition . ' %s', $this->position ); |
|
| 169 | + $this->sql .= $wpdb->prepare(' AND t.'.esc_sql($this->sortby).' '.$condition.' %s', $this->position); |
|
| 170 | 170 | } |
| 171 | 171 | |
| 172 | 172 | private function set_sort() { |
@@ -175,31 +175,31 @@ discard block |
||
| 175 | 175 | 'term_name' => 'name', |
| 176 | 176 | ); |
| 177 | 177 | |
| 178 | - $value = $this->request->has_param( 'sort' ) |
|
| 179 | - ? $this->request->get_param( 'sort' ) |
|
| 178 | + $value = $this->request->has_param('sort') |
|
| 179 | + ? $this->request->get_param('sort') |
|
| 180 | 180 | : '+term_name'; |
| 181 | 181 | |
| 182 | - $sortby = substr( $value, 1 ); |
|
| 183 | - $this->sortby = isset( $sortby_to_col[ $sortby ] ) ? $sortby_to_col[ $sortby ] : $sortby; |
|
| 184 | - $this->sort = substr( $value, 0, 1 ) === '+' ? 'ASC' : 'DESC'; |
|
| 182 | + $sortby = substr($value, 1); |
|
| 183 | + $this->sortby = isset($sortby_to_col[$sortby]) ? $sortby_to_col[$sortby] : $sortby; |
|
| 184 | + $this->sort = substr($value, 0, 1) === '+' ? 'ASC' : 'DESC'; |
|
| 185 | 185 | } |
| 186 | 186 | |
| 187 | 187 | private function term_contains() { |
| 188 | - if ( ! $this->request->has_param( 'term_contains' ) ) { |
|
| 188 | + if ( ! $this->request->has_param('term_contains')) { |
|
| 189 | 189 | return; |
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | global $wpdb; |
| 193 | - $value = $this->request->get_param( 'term_contains' ); |
|
| 194 | - $this->sql .= $wpdb->prepare( ' and t . name LIKE %s', '%' . esc_sql( $value ) . '%' ); |
|
| 193 | + $value = $this->request->get_param('term_contains'); |
|
| 194 | + $this->sql .= $wpdb->prepare(' and t . name LIKE %s', '%'.esc_sql($value).'%'); |
|
| 195 | 195 | } |
| 196 | 196 | |
| 197 | 197 | private function taxonomies() { |
| 198 | - $taxonomies = $this->request->has_param( 'taxonomies' ) |
|
| 199 | - ? (array) $this->request->get_param( 'taxonomies' ) |
|
| 200 | - : array( 'post_tag', 'category' ); |
|
| 201 | - $value = array_map( 'esc_sql', $taxonomies ); |
|
| 202 | - $this->sql .= " AND tt.taxonomy IN ( '" . implode( "', '", $value ) . "' )"; |
|
| 198 | + $taxonomies = $this->request->has_param('taxonomies') |
|
| 199 | + ? (array) $this->request->get_param('taxonomies') |
|
| 200 | + : array('post_tag', 'category'); |
|
| 201 | + $value = array_map('esc_sql', $taxonomies); |
|
| 202 | + $this->sql .= " AND tt.taxonomy IN ( '".implode("', '", $value)."' )"; |
|
| 203 | 203 | } |
| 204 | 204 | |
| 205 | 205 | } |
@@ -21,303 +21,303 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class Term_Entity_Match_Rest_Controller extends \WP_REST_Controller { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @var Post_Entity_Match_Service |
|
| 26 | - */ |
|
| 27 | - private $match_service; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Construct |
|
| 31 | - * |
|
| 32 | - * @param $match_service |
|
| 33 | - */ |
|
| 34 | - public function __construct( $match_service ) { |
|
| 35 | - $this->match_service = $match_service; |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - public function register_hooks() { |
|
| 39 | - add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Register the routes for the objects of the controller. |
|
| 44 | - */ |
|
| 45 | - public function register_routes() { |
|
| 46 | - // Get term matches by taxonomy name |
|
| 47 | - register_rest_route( |
|
| 48 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 49 | - '/term-matches', |
|
| 50 | - array( |
|
| 51 | - 'methods' => 'GET', |
|
| 52 | - 'callback' => array( $this, 'get_term_matches' ), |
|
| 53 | - 'args' => array( |
|
| 54 | - 'cursor' => array( |
|
| 55 | - 'type' => 'string', |
|
| 56 | - // 'default' => Cursor::EMPTY_CURSOR_AS_BASE64_STRING, |
|
| 57 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 58 | - // 'sanitize_callback' => array( Cursor::class, 'rest_sanitize_request_arg' ), |
|
| 59 | - ), |
|
| 60 | - 'limit' => array( |
|
| 61 | - 'type' => 'integer', |
|
| 62 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 63 | - 'default' => 10, |
|
| 64 | - 'minimum' => 1, |
|
| 65 | - 'maximum' => 100, |
|
| 66 | - 'sanitize_callback' => 'absint', |
|
| 67 | - ), |
|
| 68 | - 'taxonomies' => array( |
|
| 69 | - 'type' => 'array', |
|
| 70 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 71 | - ), |
|
| 72 | - 'has_match' => array( |
|
| 73 | - 'type' => 'boolean', |
|
| 74 | - 'required' => false, |
|
| 75 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 76 | - ), |
|
| 77 | - 'term_contains' => array( |
|
| 78 | - 'type' => 'string', |
|
| 79 | - 'required' => false, |
|
| 80 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 81 | - ), |
|
| 82 | - 'sort' => array( |
|
| 83 | - 'type' => 'string', |
|
| 84 | - 'required' => 'false', |
|
| 85 | - 'enum' => array( |
|
| 86 | - '+term_name', |
|
| 87 | - '-term_name', |
|
| 88 | - '+entity_name', |
|
| 89 | - '-entity_name', |
|
| 90 | - '+occurrences', |
|
| 91 | - '-occurrences', |
|
| 92 | - ), |
|
| 93 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 94 | - ), |
|
| 95 | - ), |
|
| 96 | - 'permission_callback' => function () { |
|
| 97 | - return current_user_can( 'manage_options' ); |
|
| 98 | - }, |
|
| 99 | - ) |
|
| 100 | - ); |
|
| 101 | - |
|
| 102 | - // Create a new match for a term |
|
| 103 | - register_rest_route( |
|
| 104 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 105 | - '/term-matches/(?P<term_id>\d+)/matches', |
|
| 106 | - array( |
|
| 107 | - 'methods' => 'POST', |
|
| 108 | - 'callback' => array( $this, 'create_term_match' ), |
|
| 109 | - 'args' => array( |
|
| 110 | - 'term_id' => array( |
|
| 111 | - 'required' => true, |
|
| 112 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 113 | - ), |
|
| 114 | - ), |
|
| 115 | - 'permission_callback' => function () { |
|
| 116 | - return current_user_can( 'manage_options' ); |
|
| 117 | - }, |
|
| 118 | - ) |
|
| 119 | - ); |
|
| 120 | - |
|
| 121 | - // Update an existing term match |
|
| 122 | - register_rest_route( |
|
| 123 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 124 | - '/term-matches/(?P<term_id>\d+)/matches/(?P<match_id>\d+)', |
|
| 125 | - array( |
|
| 126 | - 'methods' => 'PUT', |
|
| 127 | - 'callback' => array( $this, 'update_term_match' ), |
|
| 128 | - 'args' => array( |
|
| 129 | - 'term_id' => array( |
|
| 130 | - 'required' => true, |
|
| 131 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 132 | - ), |
|
| 133 | - 'match_id' => array( |
|
| 134 | - 'required' => true, |
|
| 135 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 136 | - ), |
|
| 137 | - ), |
|
| 138 | - 'permission_callback' => function () { |
|
| 139 | - return current_user_can( 'manage_options' ); |
|
| 140 | - }, |
|
| 141 | - ) |
|
| 142 | - ); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - public function get_term_matches( $request ) { |
|
| 146 | - $limit = $request->has_param( 'limit' ) ? $request->get_param( 'limit' ) : 10; |
|
| 147 | - $cursor = $request->has_param( 'cursor' ) |
|
| 148 | - ? Cursor::from_base64_string( $request->get_param( 'cursor' ) ) |
|
| 149 | - : Cursor::empty_cursor(); |
|
| 150 | - $cursor_sort = new Cursor_Sort( |
|
| 151 | - $request->has_param( 'sort' ) ? $request->get_param( 'sort' ) : '+term_name' |
|
| 152 | - ); |
|
| 153 | - |
|
| 154 | - $query = new Term_Query( $request, $cursor, $cursor_sort, $limit + 1 ); |
|
| 155 | - |
|
| 156 | - $results = $query->get_results(); |
|
| 157 | - $items = $cursor->get_direction() === 'ASCENDING' |
|
| 158 | - ? array_slice( $results, 0, min( count( $results ), $limit ) ) |
|
| 159 | - : array_slice( $results, count( $results ) > $limit ? 1 : 0 ); |
|
| 160 | - $position = current( $items )->{$cursor_sort->get_sort_property()}; |
|
| 161 | - $next_position = end( $items )->{$cursor_sort->get_sort_property()}; |
|
| 162 | - |
|
| 163 | - $self = $cursor; |
|
| 164 | - $first = new Cursor( null, 'INCLUDED', 'ASCENDING' ); |
|
| 165 | - $last = new Cursor( null, 'INCLUDED', 'DESCENDING' ); |
|
| 166 | - |
|
| 167 | - return new Page( |
|
| 168 | - $items, |
|
| 169 | - $self->to_base64_string(), |
|
| 170 | - $first->to_base64_string(), |
|
| 171 | - $this->get_prev_cursor_as_base64_string( $cursor, $results, $limit, $position ), |
|
| 172 | - $this->get_next_cursor_as_base64_string( $cursor, $results, $limit, $next_position ), |
|
| 173 | - $last->to_base64_string() |
|
| 174 | - ); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * @param Cursor $cursor |
|
| 179 | - * |
|
| 180 | - * @return string|null |
|
| 181 | - * @throws Exception when one of the called functions throws an exception. |
|
| 182 | - */ |
|
| 183 | - private function get_prev_cursor_as_base64_string( $cursor, $results, $limit, $position ) { |
|
| 184 | - if ( ( $cursor->get_direction() === 'ASCENDING' && $cursor->get_position() !== null ) || ( $cursor->get_direction() === 'DESCENDING' && count( $results ) > $limit ) ) { |
|
| 185 | - $cursor = new Cursor( $position, 'EXCLUDED', 'DESCENDING' ); |
|
| 186 | - |
|
| 187 | - return $cursor->to_base64_string(); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - return null; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * @param Cursor $cursor |
|
| 195 | - * |
|
| 196 | - * @return string|null |
|
| 197 | - * @throws Exception when one of the called functions throws an exception. |
|
| 198 | - */ |
|
| 199 | - private function get_next_cursor_as_base64_string( $cursor, $results, $limit, $position ) { |
|
| 200 | - if ( ( $cursor->get_direction() === 'DESCENDING' && $cursor->get_position() !== null ) || ( $cursor->get_direction() === 'ASCENDING' && count( $results ) > $limit ) ) { |
|
| 201 | - $cursor = new Cursor( $position, 'EXCLUDED', 'ASCENDING' ); |
|
| 202 | - |
|
| 203 | - return $cursor->to_base64_string(); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - return null; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Get the term matches by taxonomy name. |
|
| 211 | - * |
|
| 212 | - * @param $request \WP_REST_Request |
|
| 213 | - * |
|
| 214 | - * @throws \Exception If there was a problem getting the match. |
|
| 215 | - */ |
|
| 216 | - public function get_term_matches_legacy( $request ) { |
|
| 217 | - |
|
| 218 | - $cursor = $request->get_param( 'cursor' ); |
|
| 219 | - if ( $request->has_param( 'limit' ) ) { |
|
| 220 | - $cursor['limit'] = $request->get_param( 'limit' ); |
|
| 221 | - } |
|
| 222 | - if ( $request->has_param( 'sort' ) ) { |
|
| 223 | - $cursor['sort'] = $request->get_param( 'sort' ); |
|
| 224 | - } |
|
| 225 | - if ( $request->has_param( 'taxonomies' ) ) { |
|
| 226 | - $cursor['query']['taxonomies'] = $request->get_param( 'taxonomies' ); |
|
| 227 | - } |
|
| 228 | - if ( $request->has_param( 'has_match' ) ) { |
|
| 229 | - $cursor['query']['has_match'] = $request->get_param( 'has_match' ); |
|
| 230 | - } |
|
| 231 | - if ( $request->has_param( 'term_contains' ) ) { |
|
| 232 | - $cursor['query']['term_contains'] = $request->get_param( 'term_contains' ); |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - // Query. |
|
| 236 | - $taxonomies = isset( $cursor['query']['taxonomies'] ) ? $cursor['query']['taxonomies'] : apply_filters( |
|
| 237 | - 'wl_dashboard__post_entity_match__taxonomies', |
|
| 238 | - array( |
|
| 239 | - 'post_tag', |
|
| 240 | - 'category', |
|
| 241 | - ) |
|
| 242 | - ); |
|
| 243 | - |
|
| 244 | - $has_match = isset( $cursor['query']['has_match'] ) ? $cursor['query']['has_match'] : null; |
|
| 245 | - $term_contains = isset( $cursor['query']['term_contains'] ) ? $cursor['query']['term_contains'] : null; |
|
| 246 | - |
|
| 247 | - $items = $this->match_service->list_items( |
|
| 248 | - array( |
|
| 249 | - // Query |
|
| 250 | - 'taxonomies' => $taxonomies, |
|
| 251 | - 'has_match' => $has_match, |
|
| 252 | - 'term_contains' => $term_contains, |
|
| 253 | - // Cursor-Pagination |
|
| 254 | - 'position' => $cursor['position'], |
|
| 255 | - 'element' => $cursor['element'], |
|
| 256 | - 'direction' => $cursor['direction'], |
|
| 257 | - // `+1` to check if we have other results. |
|
| 258 | - 'limit' => $cursor['limit'] + 1, |
|
| 259 | - 'sort' => $cursor['sort'], |
|
| 260 | - ) |
|
| 261 | - ); |
|
| 262 | - |
|
| 263 | - return new Cursor_Page( |
|
| 264 | - $items, |
|
| 265 | - $cursor['position'], |
|
| 266 | - $cursor['element'], |
|
| 267 | - $cursor['direction'], |
|
| 268 | - $cursor['sort'], |
|
| 269 | - $cursor['limit'], |
|
| 270 | - $cursor['query'] |
|
| 271 | - ); |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * Create a new match for a term. |
|
| 276 | - * |
|
| 277 | - * @param $request \WP_REST_Request |
|
| 278 | - * |
|
| 279 | - * @throws \Exception If there was a problem creating the match. |
|
| 280 | - */ |
|
| 281 | - public function create_term_match( $request ) { |
|
| 282 | - |
|
| 283 | - $term_id = $request->get_param( 'term_id' ); |
|
| 284 | - |
|
| 285 | - // If we dont have a entry on the match table, then add one. |
|
| 286 | - $content_id = Wordpress_Content_Id::create_term( $term_id ); |
|
| 287 | - if ( ! Wordpress_Content_Service::get_instance()->get_entity_id( $content_id ) ) { |
|
| 288 | - $uri = Entity_Uri_Generator::create_uri( $content_id->get_type(), $content_id->get_id() ); |
|
| 289 | - Wordpress_Content_Service::get_instance()->set_entity_id( $content_id, $uri ); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - $match_id = Wordpress_Dataset_Content_Service_Hooks::get_id_or_create( |
|
| 293 | - $term_id, |
|
| 294 | - Object_Type_Enum::TERM |
|
| 295 | - ); |
|
| 296 | - |
|
| 297 | - return $this->match_service->set_jsonld( |
|
| 298 | - $term_id, |
|
| 299 | - Object_Type_Enum::TERM, |
|
| 300 | - $match_id, |
|
| 301 | - $request->get_json_params() |
|
| 302 | - ); |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * Update term match. |
|
| 307 | - * |
|
| 308 | - * @param \WP_REST_Request $request |
|
| 309 | - * |
|
| 310 | - * @return Match_Entry |
|
| 311 | - * |
|
| 312 | - * @throws \Exception If there was a problem updating the match. |
|
| 313 | - */ |
|
| 314 | - public function update_term_match( $request ) { |
|
| 315 | - return $this->match_service->set_jsonld( |
|
| 316 | - $request->get_param( 'term_id' ), |
|
| 317 | - Object_Type_Enum::TERM, |
|
| 318 | - $request->get_param( 'match_id' ), |
|
| 319 | - $request->get_json_params() |
|
| 320 | - ); |
|
| 321 | - } |
|
| 24 | + /** |
|
| 25 | + * @var Post_Entity_Match_Service |
|
| 26 | + */ |
|
| 27 | + private $match_service; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Construct |
|
| 31 | + * |
|
| 32 | + * @param $match_service |
|
| 33 | + */ |
|
| 34 | + public function __construct( $match_service ) { |
|
| 35 | + $this->match_service = $match_service; |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + public function register_hooks() { |
|
| 39 | + add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Register the routes for the objects of the controller. |
|
| 44 | + */ |
|
| 45 | + public function register_routes() { |
|
| 46 | + // Get term matches by taxonomy name |
|
| 47 | + register_rest_route( |
|
| 48 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 49 | + '/term-matches', |
|
| 50 | + array( |
|
| 51 | + 'methods' => 'GET', |
|
| 52 | + 'callback' => array( $this, 'get_term_matches' ), |
|
| 53 | + 'args' => array( |
|
| 54 | + 'cursor' => array( |
|
| 55 | + 'type' => 'string', |
|
| 56 | + // 'default' => Cursor::EMPTY_CURSOR_AS_BASE64_STRING, |
|
| 57 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 58 | + // 'sanitize_callback' => array( Cursor::class, 'rest_sanitize_request_arg' ), |
|
| 59 | + ), |
|
| 60 | + 'limit' => array( |
|
| 61 | + 'type' => 'integer', |
|
| 62 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 63 | + 'default' => 10, |
|
| 64 | + 'minimum' => 1, |
|
| 65 | + 'maximum' => 100, |
|
| 66 | + 'sanitize_callback' => 'absint', |
|
| 67 | + ), |
|
| 68 | + 'taxonomies' => array( |
|
| 69 | + 'type' => 'array', |
|
| 70 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 71 | + ), |
|
| 72 | + 'has_match' => array( |
|
| 73 | + 'type' => 'boolean', |
|
| 74 | + 'required' => false, |
|
| 75 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 76 | + ), |
|
| 77 | + 'term_contains' => array( |
|
| 78 | + 'type' => 'string', |
|
| 79 | + 'required' => false, |
|
| 80 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 81 | + ), |
|
| 82 | + 'sort' => array( |
|
| 83 | + 'type' => 'string', |
|
| 84 | + 'required' => 'false', |
|
| 85 | + 'enum' => array( |
|
| 86 | + '+term_name', |
|
| 87 | + '-term_name', |
|
| 88 | + '+entity_name', |
|
| 89 | + '-entity_name', |
|
| 90 | + '+occurrences', |
|
| 91 | + '-occurrences', |
|
| 92 | + ), |
|
| 93 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 94 | + ), |
|
| 95 | + ), |
|
| 96 | + 'permission_callback' => function () { |
|
| 97 | + return current_user_can( 'manage_options' ); |
|
| 98 | + }, |
|
| 99 | + ) |
|
| 100 | + ); |
|
| 101 | + |
|
| 102 | + // Create a new match for a term |
|
| 103 | + register_rest_route( |
|
| 104 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 105 | + '/term-matches/(?P<term_id>\d+)/matches', |
|
| 106 | + array( |
|
| 107 | + 'methods' => 'POST', |
|
| 108 | + 'callback' => array( $this, 'create_term_match' ), |
|
| 109 | + 'args' => array( |
|
| 110 | + 'term_id' => array( |
|
| 111 | + 'required' => true, |
|
| 112 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 113 | + ), |
|
| 114 | + ), |
|
| 115 | + 'permission_callback' => function () { |
|
| 116 | + return current_user_can( 'manage_options' ); |
|
| 117 | + }, |
|
| 118 | + ) |
|
| 119 | + ); |
|
| 120 | + |
|
| 121 | + // Update an existing term match |
|
| 122 | + register_rest_route( |
|
| 123 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 124 | + '/term-matches/(?P<term_id>\d+)/matches/(?P<match_id>\d+)', |
|
| 125 | + array( |
|
| 126 | + 'methods' => 'PUT', |
|
| 127 | + 'callback' => array( $this, 'update_term_match' ), |
|
| 128 | + 'args' => array( |
|
| 129 | + 'term_id' => array( |
|
| 130 | + 'required' => true, |
|
| 131 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 132 | + ), |
|
| 133 | + 'match_id' => array( |
|
| 134 | + 'required' => true, |
|
| 135 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 136 | + ), |
|
| 137 | + ), |
|
| 138 | + 'permission_callback' => function () { |
|
| 139 | + return current_user_can( 'manage_options' ); |
|
| 140 | + }, |
|
| 141 | + ) |
|
| 142 | + ); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + public function get_term_matches( $request ) { |
|
| 146 | + $limit = $request->has_param( 'limit' ) ? $request->get_param( 'limit' ) : 10; |
|
| 147 | + $cursor = $request->has_param( 'cursor' ) |
|
| 148 | + ? Cursor::from_base64_string( $request->get_param( 'cursor' ) ) |
|
| 149 | + : Cursor::empty_cursor(); |
|
| 150 | + $cursor_sort = new Cursor_Sort( |
|
| 151 | + $request->has_param( 'sort' ) ? $request->get_param( 'sort' ) : '+term_name' |
|
| 152 | + ); |
|
| 153 | + |
|
| 154 | + $query = new Term_Query( $request, $cursor, $cursor_sort, $limit + 1 ); |
|
| 155 | + |
|
| 156 | + $results = $query->get_results(); |
|
| 157 | + $items = $cursor->get_direction() === 'ASCENDING' |
|
| 158 | + ? array_slice( $results, 0, min( count( $results ), $limit ) ) |
|
| 159 | + : array_slice( $results, count( $results ) > $limit ? 1 : 0 ); |
|
| 160 | + $position = current( $items )->{$cursor_sort->get_sort_property()}; |
|
| 161 | + $next_position = end( $items )->{$cursor_sort->get_sort_property()}; |
|
| 162 | + |
|
| 163 | + $self = $cursor; |
|
| 164 | + $first = new Cursor( null, 'INCLUDED', 'ASCENDING' ); |
|
| 165 | + $last = new Cursor( null, 'INCLUDED', 'DESCENDING' ); |
|
| 166 | + |
|
| 167 | + return new Page( |
|
| 168 | + $items, |
|
| 169 | + $self->to_base64_string(), |
|
| 170 | + $first->to_base64_string(), |
|
| 171 | + $this->get_prev_cursor_as_base64_string( $cursor, $results, $limit, $position ), |
|
| 172 | + $this->get_next_cursor_as_base64_string( $cursor, $results, $limit, $next_position ), |
|
| 173 | + $last->to_base64_string() |
|
| 174 | + ); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * @param Cursor $cursor |
|
| 179 | + * |
|
| 180 | + * @return string|null |
|
| 181 | + * @throws Exception when one of the called functions throws an exception. |
|
| 182 | + */ |
|
| 183 | + private function get_prev_cursor_as_base64_string( $cursor, $results, $limit, $position ) { |
|
| 184 | + if ( ( $cursor->get_direction() === 'ASCENDING' && $cursor->get_position() !== null ) || ( $cursor->get_direction() === 'DESCENDING' && count( $results ) > $limit ) ) { |
|
| 185 | + $cursor = new Cursor( $position, 'EXCLUDED', 'DESCENDING' ); |
|
| 186 | + |
|
| 187 | + return $cursor->to_base64_string(); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + return null; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * @param Cursor $cursor |
|
| 195 | + * |
|
| 196 | + * @return string|null |
|
| 197 | + * @throws Exception when one of the called functions throws an exception. |
|
| 198 | + */ |
|
| 199 | + private function get_next_cursor_as_base64_string( $cursor, $results, $limit, $position ) { |
|
| 200 | + if ( ( $cursor->get_direction() === 'DESCENDING' && $cursor->get_position() !== null ) || ( $cursor->get_direction() === 'ASCENDING' && count( $results ) > $limit ) ) { |
|
| 201 | + $cursor = new Cursor( $position, 'EXCLUDED', 'ASCENDING' ); |
|
| 202 | + |
|
| 203 | + return $cursor->to_base64_string(); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + return null; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Get the term matches by taxonomy name. |
|
| 211 | + * |
|
| 212 | + * @param $request \WP_REST_Request |
|
| 213 | + * |
|
| 214 | + * @throws \Exception If there was a problem getting the match. |
|
| 215 | + */ |
|
| 216 | + public function get_term_matches_legacy( $request ) { |
|
| 217 | + |
|
| 218 | + $cursor = $request->get_param( 'cursor' ); |
|
| 219 | + if ( $request->has_param( 'limit' ) ) { |
|
| 220 | + $cursor['limit'] = $request->get_param( 'limit' ); |
|
| 221 | + } |
|
| 222 | + if ( $request->has_param( 'sort' ) ) { |
|
| 223 | + $cursor['sort'] = $request->get_param( 'sort' ); |
|
| 224 | + } |
|
| 225 | + if ( $request->has_param( 'taxonomies' ) ) { |
|
| 226 | + $cursor['query']['taxonomies'] = $request->get_param( 'taxonomies' ); |
|
| 227 | + } |
|
| 228 | + if ( $request->has_param( 'has_match' ) ) { |
|
| 229 | + $cursor['query']['has_match'] = $request->get_param( 'has_match' ); |
|
| 230 | + } |
|
| 231 | + if ( $request->has_param( 'term_contains' ) ) { |
|
| 232 | + $cursor['query']['term_contains'] = $request->get_param( 'term_contains' ); |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + // Query. |
|
| 236 | + $taxonomies = isset( $cursor['query']['taxonomies'] ) ? $cursor['query']['taxonomies'] : apply_filters( |
|
| 237 | + 'wl_dashboard__post_entity_match__taxonomies', |
|
| 238 | + array( |
|
| 239 | + 'post_tag', |
|
| 240 | + 'category', |
|
| 241 | + ) |
|
| 242 | + ); |
|
| 243 | + |
|
| 244 | + $has_match = isset( $cursor['query']['has_match'] ) ? $cursor['query']['has_match'] : null; |
|
| 245 | + $term_contains = isset( $cursor['query']['term_contains'] ) ? $cursor['query']['term_contains'] : null; |
|
| 246 | + |
|
| 247 | + $items = $this->match_service->list_items( |
|
| 248 | + array( |
|
| 249 | + // Query |
|
| 250 | + 'taxonomies' => $taxonomies, |
|
| 251 | + 'has_match' => $has_match, |
|
| 252 | + 'term_contains' => $term_contains, |
|
| 253 | + // Cursor-Pagination |
|
| 254 | + 'position' => $cursor['position'], |
|
| 255 | + 'element' => $cursor['element'], |
|
| 256 | + 'direction' => $cursor['direction'], |
|
| 257 | + // `+1` to check if we have other results. |
|
| 258 | + 'limit' => $cursor['limit'] + 1, |
|
| 259 | + 'sort' => $cursor['sort'], |
|
| 260 | + ) |
|
| 261 | + ); |
|
| 262 | + |
|
| 263 | + return new Cursor_Page( |
|
| 264 | + $items, |
|
| 265 | + $cursor['position'], |
|
| 266 | + $cursor['element'], |
|
| 267 | + $cursor['direction'], |
|
| 268 | + $cursor['sort'], |
|
| 269 | + $cursor['limit'], |
|
| 270 | + $cursor['query'] |
|
| 271 | + ); |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * Create a new match for a term. |
|
| 276 | + * |
|
| 277 | + * @param $request \WP_REST_Request |
|
| 278 | + * |
|
| 279 | + * @throws \Exception If there was a problem creating the match. |
|
| 280 | + */ |
|
| 281 | + public function create_term_match( $request ) { |
|
| 282 | + |
|
| 283 | + $term_id = $request->get_param( 'term_id' ); |
|
| 284 | + |
|
| 285 | + // If we dont have a entry on the match table, then add one. |
|
| 286 | + $content_id = Wordpress_Content_Id::create_term( $term_id ); |
|
| 287 | + if ( ! Wordpress_Content_Service::get_instance()->get_entity_id( $content_id ) ) { |
|
| 288 | + $uri = Entity_Uri_Generator::create_uri( $content_id->get_type(), $content_id->get_id() ); |
|
| 289 | + Wordpress_Content_Service::get_instance()->set_entity_id( $content_id, $uri ); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + $match_id = Wordpress_Dataset_Content_Service_Hooks::get_id_or_create( |
|
| 293 | + $term_id, |
|
| 294 | + Object_Type_Enum::TERM |
|
| 295 | + ); |
|
| 296 | + |
|
| 297 | + return $this->match_service->set_jsonld( |
|
| 298 | + $term_id, |
|
| 299 | + Object_Type_Enum::TERM, |
|
| 300 | + $match_id, |
|
| 301 | + $request->get_json_params() |
|
| 302 | + ); |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * Update term match. |
|
| 307 | + * |
|
| 308 | + * @param \WP_REST_Request $request |
|
| 309 | + * |
|
| 310 | + * @return Match_Entry |
|
| 311 | + * |
|
| 312 | + * @throws \Exception If there was a problem updating the match. |
|
| 313 | + */ |
|
| 314 | + public function update_term_match( $request ) { |
|
| 315 | + return $this->match_service->set_jsonld( |
|
| 316 | + $request->get_param( 'term_id' ), |
|
| 317 | + Object_Type_Enum::TERM, |
|
| 318 | + $request->get_param( 'match_id' ), |
|
| 319 | + $request->get_json_params() |
|
| 320 | + ); |
|
| 321 | + } |
|
| 322 | 322 | |
| 323 | 323 | } |
@@ -31,12 +31,12 @@ discard block |
||
| 31 | 31 | * |
| 32 | 32 | * @param $match_service |
| 33 | 33 | */ |
| 34 | - public function __construct( $match_service ) { |
|
| 34 | + public function __construct($match_service) { |
|
| 35 | 35 | $this->match_service = $match_service; |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | public function register_hooks() { |
| 39 | - add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
|
| 39 | + add_action('rest_api_init', array($this, 'register_routes')); |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | /** |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | '/term-matches', |
| 50 | 50 | array( |
| 51 | 51 | 'methods' => 'GET', |
| 52 | - 'callback' => array( $this, 'get_term_matches' ), |
|
| 52 | + 'callback' => array($this, 'get_term_matches'), |
|
| 53 | 53 | 'args' => array( |
| 54 | 54 | 'cursor' => array( |
| 55 | 55 | 'type' => 'string', |
@@ -93,8 +93,8 @@ discard block |
||
| 93 | 93 | 'validate_callback' => 'rest_validate_request_arg', |
| 94 | 94 | ), |
| 95 | 95 | ), |
| 96 | - 'permission_callback' => function () { |
|
| 97 | - return current_user_can( 'manage_options' ); |
|
| 96 | + 'permission_callback' => function() { |
|
| 97 | + return current_user_can('manage_options'); |
|
| 98 | 98 | }, |
| 99 | 99 | ) |
| 100 | 100 | ); |
@@ -105,15 +105,15 @@ discard block |
||
| 105 | 105 | '/term-matches/(?P<term_id>\d+)/matches', |
| 106 | 106 | array( |
| 107 | 107 | 'methods' => 'POST', |
| 108 | - 'callback' => array( $this, 'create_term_match' ), |
|
| 108 | + 'callback' => array($this, 'create_term_match'), |
|
| 109 | 109 | 'args' => array( |
| 110 | 110 | 'term_id' => array( |
| 111 | 111 | 'required' => true, |
| 112 | 112 | 'validate_callback' => 'rest_validate_request_arg', |
| 113 | 113 | ), |
| 114 | 114 | ), |
| 115 | - 'permission_callback' => function () { |
|
| 116 | - return current_user_can( 'manage_options' ); |
|
| 115 | + 'permission_callback' => function() { |
|
| 116 | + return current_user_can('manage_options'); |
|
| 117 | 117 | }, |
| 118 | 118 | ) |
| 119 | 119 | ); |
@@ -124,7 +124,7 @@ discard block |
||
| 124 | 124 | '/term-matches/(?P<term_id>\d+)/matches/(?P<match_id>\d+)', |
| 125 | 125 | array( |
| 126 | 126 | 'methods' => 'PUT', |
| 127 | - 'callback' => array( $this, 'update_term_match' ), |
|
| 127 | + 'callback' => array($this, 'update_term_match'), |
|
| 128 | 128 | 'args' => array( |
| 129 | 129 | 'term_id' => array( |
| 130 | 130 | 'required' => true, |
@@ -135,41 +135,41 @@ discard block |
||
| 135 | 135 | 'validate_callback' => 'rest_validate_request_arg', |
| 136 | 136 | ), |
| 137 | 137 | ), |
| 138 | - 'permission_callback' => function () { |
|
| 139 | - return current_user_can( 'manage_options' ); |
|
| 138 | + 'permission_callback' => function() { |
|
| 139 | + return current_user_can('manage_options'); |
|
| 140 | 140 | }, |
| 141 | 141 | ) |
| 142 | 142 | ); |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | - public function get_term_matches( $request ) { |
|
| 146 | - $limit = $request->has_param( 'limit' ) ? $request->get_param( 'limit' ) : 10; |
|
| 147 | - $cursor = $request->has_param( 'cursor' ) |
|
| 148 | - ? Cursor::from_base64_string( $request->get_param( 'cursor' ) ) |
|
| 145 | + public function get_term_matches($request) { |
|
| 146 | + $limit = $request->has_param('limit') ? $request->get_param('limit') : 10; |
|
| 147 | + $cursor = $request->has_param('cursor') |
|
| 148 | + ? Cursor::from_base64_string($request->get_param('cursor')) |
|
| 149 | 149 | : Cursor::empty_cursor(); |
| 150 | 150 | $cursor_sort = new Cursor_Sort( |
| 151 | - $request->has_param( 'sort' ) ? $request->get_param( 'sort' ) : '+term_name' |
|
| 151 | + $request->has_param('sort') ? $request->get_param('sort') : '+term_name' |
|
| 152 | 152 | ); |
| 153 | 153 | |
| 154 | - $query = new Term_Query( $request, $cursor, $cursor_sort, $limit + 1 ); |
|
| 154 | + $query = new Term_Query($request, $cursor, $cursor_sort, $limit + 1); |
|
| 155 | 155 | |
| 156 | 156 | $results = $query->get_results(); |
| 157 | 157 | $items = $cursor->get_direction() === 'ASCENDING' |
| 158 | - ? array_slice( $results, 0, min( count( $results ), $limit ) ) |
|
| 159 | - : array_slice( $results, count( $results ) > $limit ? 1 : 0 ); |
|
| 160 | - $position = current( $items )->{$cursor_sort->get_sort_property()}; |
|
| 161 | - $next_position = end( $items )->{$cursor_sort->get_sort_property()}; |
|
| 158 | + ? array_slice($results, 0, min(count($results), $limit)) |
|
| 159 | + : array_slice($results, count($results) > $limit ? 1 : 0); |
|
| 160 | + $position = current($items)->{$cursor_sort->get_sort_property()}; |
|
| 161 | + $next_position = end($items)->{$cursor_sort->get_sort_property()}; |
|
| 162 | 162 | |
| 163 | 163 | $self = $cursor; |
| 164 | - $first = new Cursor( null, 'INCLUDED', 'ASCENDING' ); |
|
| 165 | - $last = new Cursor( null, 'INCLUDED', 'DESCENDING' ); |
|
| 164 | + $first = new Cursor(null, 'INCLUDED', 'ASCENDING'); |
|
| 165 | + $last = new Cursor(null, 'INCLUDED', 'DESCENDING'); |
|
| 166 | 166 | |
| 167 | 167 | return new Page( |
| 168 | 168 | $items, |
| 169 | 169 | $self->to_base64_string(), |
| 170 | 170 | $first->to_base64_string(), |
| 171 | - $this->get_prev_cursor_as_base64_string( $cursor, $results, $limit, $position ), |
|
| 172 | - $this->get_next_cursor_as_base64_string( $cursor, $results, $limit, $next_position ), |
|
| 171 | + $this->get_prev_cursor_as_base64_string($cursor, $results, $limit, $position), |
|
| 172 | + $this->get_next_cursor_as_base64_string($cursor, $results, $limit, $next_position), |
|
| 173 | 173 | $last->to_base64_string() |
| 174 | 174 | ); |
| 175 | 175 | } |
@@ -180,9 +180,9 @@ discard block |
||
| 180 | 180 | * @return string|null |
| 181 | 181 | * @throws Exception when one of the called functions throws an exception. |
| 182 | 182 | */ |
| 183 | - private function get_prev_cursor_as_base64_string( $cursor, $results, $limit, $position ) { |
|
| 184 | - if ( ( $cursor->get_direction() === 'ASCENDING' && $cursor->get_position() !== null ) || ( $cursor->get_direction() === 'DESCENDING' && count( $results ) > $limit ) ) { |
|
| 185 | - $cursor = new Cursor( $position, 'EXCLUDED', 'DESCENDING' ); |
|
| 183 | + private function get_prev_cursor_as_base64_string($cursor, $results, $limit, $position) { |
|
| 184 | + if (($cursor->get_direction() === 'ASCENDING' && $cursor->get_position() !== null) || ($cursor->get_direction() === 'DESCENDING' && count($results) > $limit)) { |
|
| 185 | + $cursor = new Cursor($position, 'EXCLUDED', 'DESCENDING'); |
|
| 186 | 186 | |
| 187 | 187 | return $cursor->to_base64_string(); |
| 188 | 188 | } |
@@ -196,9 +196,9 @@ discard block |
||
| 196 | 196 | * @return string|null |
| 197 | 197 | * @throws Exception when one of the called functions throws an exception. |
| 198 | 198 | */ |
| 199 | - private function get_next_cursor_as_base64_string( $cursor, $results, $limit, $position ) { |
|
| 200 | - if ( ( $cursor->get_direction() === 'DESCENDING' && $cursor->get_position() !== null ) || ( $cursor->get_direction() === 'ASCENDING' && count( $results ) > $limit ) ) { |
|
| 201 | - $cursor = new Cursor( $position, 'EXCLUDED', 'ASCENDING' ); |
|
| 199 | + private function get_next_cursor_as_base64_string($cursor, $results, $limit, $position) { |
|
| 200 | + if (($cursor->get_direction() === 'DESCENDING' && $cursor->get_position() !== null) || ($cursor->get_direction() === 'ASCENDING' && count($results) > $limit)) { |
|
| 201 | + $cursor = new Cursor($position, 'EXCLUDED', 'ASCENDING'); |
|
| 202 | 202 | |
| 203 | 203 | return $cursor->to_base64_string(); |
| 204 | 204 | } |
@@ -213,27 +213,27 @@ discard block |
||
| 213 | 213 | * |
| 214 | 214 | * @throws \Exception If there was a problem getting the match. |
| 215 | 215 | */ |
| 216 | - public function get_term_matches_legacy( $request ) { |
|
| 216 | + public function get_term_matches_legacy($request) { |
|
| 217 | 217 | |
| 218 | - $cursor = $request->get_param( 'cursor' ); |
|
| 219 | - if ( $request->has_param( 'limit' ) ) { |
|
| 220 | - $cursor['limit'] = $request->get_param( 'limit' ); |
|
| 218 | + $cursor = $request->get_param('cursor'); |
|
| 219 | + if ($request->has_param('limit')) { |
|
| 220 | + $cursor['limit'] = $request->get_param('limit'); |
|
| 221 | 221 | } |
| 222 | - if ( $request->has_param( 'sort' ) ) { |
|
| 223 | - $cursor['sort'] = $request->get_param( 'sort' ); |
|
| 222 | + if ($request->has_param('sort')) { |
|
| 223 | + $cursor['sort'] = $request->get_param('sort'); |
|
| 224 | 224 | } |
| 225 | - if ( $request->has_param( 'taxonomies' ) ) { |
|
| 226 | - $cursor['query']['taxonomies'] = $request->get_param( 'taxonomies' ); |
|
| 225 | + if ($request->has_param('taxonomies')) { |
|
| 226 | + $cursor['query']['taxonomies'] = $request->get_param('taxonomies'); |
|
| 227 | 227 | } |
| 228 | - if ( $request->has_param( 'has_match' ) ) { |
|
| 229 | - $cursor['query']['has_match'] = $request->get_param( 'has_match' ); |
|
| 228 | + if ($request->has_param('has_match')) { |
|
| 229 | + $cursor['query']['has_match'] = $request->get_param('has_match'); |
|
| 230 | 230 | } |
| 231 | - if ( $request->has_param( 'term_contains' ) ) { |
|
| 232 | - $cursor['query']['term_contains'] = $request->get_param( 'term_contains' ); |
|
| 231 | + if ($request->has_param('term_contains')) { |
|
| 232 | + $cursor['query']['term_contains'] = $request->get_param('term_contains'); |
|
| 233 | 233 | } |
| 234 | 234 | |
| 235 | 235 | // Query. |
| 236 | - $taxonomies = isset( $cursor['query']['taxonomies'] ) ? $cursor['query']['taxonomies'] : apply_filters( |
|
| 236 | + $taxonomies = isset($cursor['query']['taxonomies']) ? $cursor['query']['taxonomies'] : apply_filters( |
|
| 237 | 237 | 'wl_dashboard__post_entity_match__taxonomies', |
| 238 | 238 | array( |
| 239 | 239 | 'post_tag', |
@@ -241,8 +241,8 @@ discard block |
||
| 241 | 241 | ) |
| 242 | 242 | ); |
| 243 | 243 | |
| 244 | - $has_match = isset( $cursor['query']['has_match'] ) ? $cursor['query']['has_match'] : null; |
|
| 245 | - $term_contains = isset( $cursor['query']['term_contains'] ) ? $cursor['query']['term_contains'] : null; |
|
| 244 | + $has_match = isset($cursor['query']['has_match']) ? $cursor['query']['has_match'] : null; |
|
| 245 | + $term_contains = isset($cursor['query']['term_contains']) ? $cursor['query']['term_contains'] : null; |
|
| 246 | 246 | |
| 247 | 247 | $items = $this->match_service->list_items( |
| 248 | 248 | array( |
@@ -278,15 +278,15 @@ discard block |
||
| 278 | 278 | * |
| 279 | 279 | * @throws \Exception If there was a problem creating the match. |
| 280 | 280 | */ |
| 281 | - public function create_term_match( $request ) { |
|
| 281 | + public function create_term_match($request) { |
|
| 282 | 282 | |
| 283 | - $term_id = $request->get_param( 'term_id' ); |
|
| 283 | + $term_id = $request->get_param('term_id'); |
|
| 284 | 284 | |
| 285 | 285 | // If we dont have a entry on the match table, then add one. |
| 286 | - $content_id = Wordpress_Content_Id::create_term( $term_id ); |
|
| 287 | - if ( ! Wordpress_Content_Service::get_instance()->get_entity_id( $content_id ) ) { |
|
| 288 | - $uri = Entity_Uri_Generator::create_uri( $content_id->get_type(), $content_id->get_id() ); |
|
| 289 | - Wordpress_Content_Service::get_instance()->set_entity_id( $content_id, $uri ); |
|
| 286 | + $content_id = Wordpress_Content_Id::create_term($term_id); |
|
| 287 | + if ( ! Wordpress_Content_Service::get_instance()->get_entity_id($content_id)) { |
|
| 288 | + $uri = Entity_Uri_Generator::create_uri($content_id->get_type(), $content_id->get_id()); |
|
| 289 | + Wordpress_Content_Service::get_instance()->set_entity_id($content_id, $uri); |
|
| 290 | 290 | } |
| 291 | 291 | |
| 292 | 292 | $match_id = Wordpress_Dataset_Content_Service_Hooks::get_id_or_create( |
@@ -311,11 +311,11 @@ discard block |
||
| 311 | 311 | * |
| 312 | 312 | * @throws \Exception If there was a problem updating the match. |
| 313 | 313 | */ |
| 314 | - public function update_term_match( $request ) { |
|
| 314 | + public function update_term_match($request) { |
|
| 315 | 315 | return $this->match_service->set_jsonld( |
| 316 | - $request->get_param( 'term_id' ), |
|
| 316 | + $request->get_param('term_id'), |
|
| 317 | 317 | Object_Type_Enum::TERM, |
| 318 | - $request->get_param( 'match_id' ), |
|
| 318 | + $request->get_param('match_id'), |
|
| 319 | 319 | $request->get_json_params() |
| 320 | 320 | ); |
| 321 | 321 | } |