class-fl-builder-revisions.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Handles the revisions UI for the builder.
  4. *
  5. * @since 2.0
  6. */
  7. final class FLBuilderRevisions {
  8. /**
  9. * Initialize hooks.
  10. *
  11. * @since 2.0
  12. * @return void
  13. */
  14. static public function init() {
  15. add_filter( 'fl_builder_ui_js_config', __CLASS__ . '::ui_js_config' );
  16. add_filter( 'fl_builder_main_menu', __CLASS__ . '::main_menu_config' );
  17. }
  18. /**
  19. * Adds revision data to the UI JS config.
  20. *
  21. * @since 2.0
  22. * @param array $config
  23. * @return array
  24. */
  25. static public function ui_js_config( $config ) {
  26. $config['revisions'] = self::get_config( $config['postId'] );
  27. return $config;
  28. }
  29. /**
  30. * Gets the revision config for a post.
  31. *
  32. * @since 2.0
  33. * @param int $post_id
  34. * @return array
  35. */
  36. static public function get_config( $post_id ) {
  37. $revisions = wp_get_post_revisions( $post_id, array(
  38. 'numberposts' => apply_filters( 'fl_builder_revisions_number', 25 ),
  39. ) );
  40. $current_time = current_time( 'timestamp' );
  41. $config = array(
  42. 'posts' => array(),
  43. 'authors' => array(),
  44. );
  45. if ( count( $revisions ) > 1 ) {
  46. foreach ( $revisions as $revision ) {
  47. if ( ! current_user_can( 'read_post', $revision->ID ) ) {
  48. continue;
  49. }
  50. if ( wp_is_post_autosave( $revision ) ) {
  51. continue;
  52. }
  53. $timestamp = strtotime( $revision->post_date );
  54. $config['posts'][] = array(
  55. 'id' => $revision->ID,
  56. 'author' => $revision->post_author,
  57. 'date' => array(
  58. 'published' => date( 'F j', $timestamp ),
  59. 'diff' => human_time_diff( $timestamp, $current_time ),
  60. ),
  61. );
  62. if ( ! isset( $config['authors'][ $revision->post_author ] ) ) {
  63. $config['authors'][ $revision->post_author ] = array(
  64. 'name' => get_the_author_meta( 'display_name', $revision->post_author ),
  65. 'avatar' => sprintf( '<img height="30" width="30" class="avatar avatar-30 photo" src="%s" />', esc_url( get_avatar_url( $revision->post_author, 30 ) ) ),
  66. );
  67. }
  68. }
  69. }
  70. return $config;
  71. }
  72. /**
  73. * Adds revision data to the main menu config.
  74. *
  75. * @since 2.0
  76. * @param array $config
  77. * @return array
  78. */
  79. static public function main_menu_config( $config ) {
  80. $config['main']['items'][35] = array(
  81. 'label' => __( 'Revisions', 'fl-builder' ),
  82. 'type' => 'view',
  83. 'view' => 'revisions',
  84. );
  85. $config['revisions'] = array(
  86. 'name' => __( 'Revisions', 'fl-builder' ),
  87. 'isShowing' => false,
  88. 'isRootView' => false,
  89. 'items' => array(),
  90. );
  91. return $config;
  92. }
  93. /**
  94. * Renders the layout for a revision preview in the builder.
  95. *
  96. * @since 2.0
  97. * @param int $revision_id
  98. * @return array
  99. */
  100. static public function render_preview( $revision_id ) {
  101. FLBuilderModel::set_post_id( $revision_id );
  102. return FLBuilderAJAXLayout::render();
  103. }
  104. /**
  105. * Restores the current layout to a revision with the specified ID.
  106. *
  107. * @since 2.0
  108. * @param int $revision_id
  109. * @return array
  110. */
  111. static public function restore( $revision_id ) {
  112. $data = FLBuilderModel::get_layout_data( 'published', $revision_id );
  113. FLBuilderModel::update_layout_data( $data );
  114. return array(
  115. 'layout' => FLBuilderAJAXLayout::render(),
  116. 'config' => FLBuilderUISettingsForms::get_node_js_config(),
  117. );
  118. }
  119. }
  120. FLBuilderRevisions::init();