PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/futureupdater/methods/future-revision-post-meta.php

https://gitlab.com/boldface/boldface-future-updater
PHP | 198 lines | 58 code | 17 blank | 123 comment | 3 complexity | d17a8bbc9c3371cfe4391c1ed47b07ca MD5 | raw file
  1. <?php
  2. /**
  3. * Future Revision Post Meta API
  4. *
  5. * @package Future Updater
  6. */
  7. namespace Boldface\FutureUpdater\Methods;
  8. defined( 'ABSPATH' ) or die();
  9. /**
  10. * Class for interacting with the Future Revision Post Meta API
  11. */
  12. class future_revision_post_meta {
  13. /**
  14. * @var Post ID
  15. *
  16. * @access protected
  17. * @since 0.1
  18. */
  19. protected $id;
  20. /**
  21. * @var Meta data key
  22. *
  23. * @access protected
  24. * @since 0.1
  25. */
  26. protected $key = '_future_updater';
  27. /**
  28. * @var Meta data value
  29. *
  30. * @access protected
  31. * @since 0.1
  32. */
  33. protected $value;
  34. /**
  35. * Constructor
  36. *
  37. * @param int $id Post ID
  38. *
  39. * @access public
  40. * @since 0.1
  41. */
  42. public function __construct( $id ) {
  43. $this->id = $id;
  44. $this->value = $this->get();
  45. }
  46. /**
  47. * Return the value of a key
  48. *
  49. * @param string $key Name of the key
  50. *
  51. * @access public
  52. * @since 0.1
  53. *
  54. * @return Value of key
  55. */
  56. public function value( $key ) {
  57. if( $this->has( $key ) ) {
  58. return $this->value[ $key ];
  59. }
  60. }
  61. /**
  62. * Whether the key exists
  63. *
  64. * @param string $key Name of the key
  65. *
  66. * @access public
  67. * @since 0.1
  68. *
  69. * @return bool Whether the key exists
  70. */
  71. public function has( $key ) {
  72. if( isset( $this->value[ $key ] ) ) {
  73. return true;
  74. }
  75. return false;
  76. }
  77. /**
  78. * Add a key value pair
  79. *
  80. * @param string $key Name of the key
  81. * @param mixed $value Value
  82. *
  83. * @access public
  84. * @since 0.1
  85. */
  86. public function add( $key, $value ) {
  87. $this->value[ $key ] = $value;
  88. $this->update_post_meta();
  89. }
  90. /**
  91. * Remove a key value pair
  92. *
  93. * @param string $key Name of the key
  94. *
  95. * @access public
  96. * @since 0.1
  97. */
  98. public function remove( $key ) {
  99. unset( $this->value[ $key ] );
  100. $this->update_post_meta();
  101. }
  102. /**
  103. * Return the keys
  104. *
  105. * @access public
  106. * @since 0.2
  107. *
  108. * @return array Array of keys
  109. */
  110. public function keys() {
  111. return array_keys( $this->value );
  112. }
  113. /**
  114. * Update post meta
  115. *
  116. * @access protected
  117. * @since 0.1
  118. */
  119. protected function update_post_meta() {
  120. \update_post_meta( $this->id, $this->key, \maybe_serialize( $this->value ) );
  121. }
  122. /**
  123. * Get all the post meta
  124. *
  125. * @access public
  126. * @since 0.1
  127. */
  128. public function get() {
  129. $value = \maybe_unserialize( \get_post_meta( $this->id, $this->key, true ) );
  130. return is_array( $value ) ? $value : [];
  131. }
  132. /**
  133. * Return the number of post meta elements
  134. *
  135. * @access public
  136. * @since 0.1
  137. *
  138. * @return int Number of post meta eletments
  139. */
  140. public function count() {
  141. return (int) count( $this->value );
  142. }
  143. /**
  144. * Return the latest key
  145. *
  146. * @access public
  147. * @since 0.1
  148. *
  149. * @return The latest key
  150. */
  151. public function latest() {
  152. if( ! is_array( $this->value ) ) return false;
  153. $this->sort();
  154. reset( $this->value );
  155. return key( $this->value );
  156. }
  157. /**
  158. * Return the sorted array
  159. *
  160. * @param string $order Whether to sort into ascending or descending order
  161. *
  162. * @access public
  163. * @since 0.1
  164. *
  165. * @return array|bool Return the sorted array or false if not sortable
  166. */
  167. public function sort( $order = 'ASC' ) {
  168. return is_array( $this->value ) ?
  169. strtoupper( $order === 'DESC' ) ? krsort( $this->value ): ksort( $this->value ) :
  170. false;
  171. }
  172. /**
  173. * Deletes all future revision post meta
  174. *
  175. * @access public
  176. * @since 0.2
  177. */
  178. public function delete() {
  179. \delete_post_meta( $this->id, $this->key );
  180. }
  181. }