class-fl-builder-admin-posts.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Handles logic for the post edit screen.
  4. *
  5. * @since 1.0
  6. */
  7. final class FLBuilderAdminPosts {
  8. /**
  9. * Initialize hooks.
  10. *
  11. * @since 1.8
  12. * @return void
  13. */
  14. static public function init() {
  15. /* Actions */
  16. add_action( 'current_screen', __CLASS__ . '::init_rendering' );
  17. /* Filters */
  18. add_filter( 'redirect_post_location', __CLASS__ . '::redirect_post_location' );
  19. add_filter( 'page_row_actions', __CLASS__ . '::render_row_actions_link' );
  20. add_filter( 'post_row_actions', __CLASS__ . '::render_row_actions_link' );
  21. }
  22. /**
  23. * WordPress doesn't have a "right way" to get the current
  24. * post type being edited and the new editor doesn't make
  25. * this any easier. This method attempts to fix that.
  26. *
  27. * @since 2.1
  28. * @return void
  29. */
  30. static public function get_post_type() {
  31. global $post, $typenow, $current_screen;
  32. if ( is_object( $post ) && $post->post_type ) {
  33. return $post->post_type;
  34. } elseif ( $typenow ) {
  35. return $typenow;
  36. } elseif ( is_object( $current_screen ) && $current_screen->post_type ) {
  37. return $current_screen->post_type;
  38. } elseif ( isset( $_REQUEST['post_type'] ) ) {
  39. return sanitize_key( $_REQUEST['post_type'] );
  40. }
  41. return null;
  42. }
  43. /**
  44. * Sets the body class, loads assets and renders the UI
  45. * if we are on a post type that supports the builder.
  46. *
  47. * @since 1.0
  48. * @return void
  49. */
  50. static public function init_rendering() {
  51. global $pagenow;
  52. if ( in_array( $pagenow, array( 'post.php', 'post-new.php' ) ) ) {
  53. $render_ui = apply_filters( 'fl_builder_render_admin_edit_ui', true );
  54. $post_types = FLBuilderModel::get_post_types();
  55. if ( $render_ui && in_array( self::get_post_type(), $post_types ) ) {
  56. add_filter( 'admin_body_class', __CLASS__ . '::body_class', 99 );
  57. add_action( 'admin_enqueue_scripts', __CLASS__ . '::styles_scripts' );
  58. add_action( 'edit_form_after_title', __CLASS__ . '::render' );
  59. }
  60. }
  61. }
  62. /**
  63. * Enqueues the CSS/JS for the post edit screen.
  64. *
  65. * @since 1.0
  66. * @return void
  67. */
  68. static public function styles_scripts() {
  69. global $wp_version;
  70. // Styles
  71. wp_enqueue_style( 'fl-builder-admin-posts', FL_BUILDER_URL . 'css/fl-builder-admin-posts.css', array(), FL_BUILDER_VERSION );
  72. // Legacy WP Styles (3.7 and below)
  73. if ( version_compare( $wp_version, '3.7', '<=' ) ) {
  74. wp_enqueue_style( 'fl-builder-admin-posts-legacy', FL_BUILDER_URL . 'css/fl-builder-admin-posts-legacy.css', array(), FL_BUILDER_VERSION );
  75. }
  76. // Scripts
  77. wp_enqueue_script( 'json2' );
  78. wp_enqueue_script( 'fl-builder-admin-posts', FL_BUILDER_URL . 'js/fl-builder-admin-posts.js', array(), FL_BUILDER_VERSION );
  79. }
  80. /**
  81. * Adds classes to the post edit screen body class.
  82. *
  83. * @since 1.0
  84. * @param string $classes The existing body classes.
  85. * @return string The body classes.
  86. */
  87. static public function body_class( $classes = '' ) {
  88. global $wp_version;
  89. // Builder body class
  90. if ( FLBuilderModel::is_builder_enabled() ) {
  91. $classes .= ' fl-builder-enabled';
  92. }
  93. // Pre WP 3.8 body class
  94. if ( version_compare( $wp_version, '3.8', '<' ) ) {
  95. $classes .= ' fl-pre-wp-3-8';
  96. }
  97. return $classes;
  98. }
  99. /**
  100. * Renders the HTML for the post edit screen.
  101. *
  102. * @since 1.0
  103. * @return void
  104. */
  105. static public function render() {
  106. global $post;
  107. $post_type_obj = get_post_type_object( $post->post_type );
  108. $post_type_name = strtolower( $post_type_obj->labels->singular_name );
  109. $enabled = FLBuilderModel::is_builder_enabled();
  110. include FL_BUILDER_DIR . 'includes/admin-posts.php';
  111. }
  112. /**
  113. * Renders the action link for post listing pages.
  114. *
  115. * @since 1.0
  116. * @param array $actions
  117. * @return array The array of action data.
  118. */
  119. static public function render_row_actions_link( $actions = array() ) {
  120. global $post;
  121. if ( 'trash' != $post->post_status && current_user_can( 'edit_post', $post->ID ) && wp_check_post_lock( $post->ID ) === false ) {
  122. $is_post_editable = (bool) apply_filters( 'fl_builder_is_post_editable', true, $post );
  123. $user_access = FLBuilderUserAccess::current_user_can( 'builder_access' );
  124. $post_types = FLBuilderModel::get_post_types();
  125. if ( in_array( $post->post_type, $post_types ) && $is_post_editable && $user_access ) {
  126. $enabled = get_post_meta( $post->ID, '_fl_builder_enabled', true );
  127. $dot = ' <span style="color:' . ( $enabled ? '#6bc373' : '#d9d9d9' ) . '; font-size:18px;">&bull;</span>';
  128. $actions['fl-builder'] = '<a href="' . FLBuilderModel::get_edit_url() . '">' . FLBuilderModel::get_branding() . $dot . '</a>';
  129. }
  130. }
  131. return $actions;
  132. }
  133. /**
  134. * Where to redirect this post on save.
  135. *
  136. * @since 1.0
  137. * @param string $location
  138. * @return string The location to redirect this post on save.
  139. */
  140. static public function redirect_post_location( $location ) {
  141. if ( isset( $_POST['fl-builder-redirect'] ) ) {
  142. $location = FLBuilderModel::get_edit_url( absint( $_POST['fl-builder-redirect'] ) );
  143. }
  144. return $location;
  145. }
  146. }
  147. FLBuilderAdminPosts::init();