class-fl-builder-user-templates-admin-edit.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * Logic for the user templates admin edit screen.
  4. *
  5. * @since 1.10
  6. */
  7. final class FLBuilderUserTemplatesAdminEdit {
  8. /**
  9. * Initialize hooks.
  10. *
  11. * @since 1.10
  12. * @return void
  13. */
  14. static public function init() {
  15. /* Actions */
  16. add_action( 'plugins_loaded', __CLASS__ . '::redirect' );
  17. add_action( 'admin_enqueue_scripts', __CLASS__ . '::admin_enqueue_scripts' );
  18. add_action( 'edit_form_after_title', __CLASS__ . '::render_global_node_message' );
  19. add_action( 'add_meta_boxes', __CLASS__ . '::add_meta_boxes', 1 );
  20. /* Filters */
  21. add_filter( 'fl_builder_render_admin_edit_ui', __CLASS__ . '::remove_builder_edit_ui' );
  22. }
  23. /**
  24. * Redirects the post-new.php page to our custom add new page.
  25. *
  26. * @since 1.10
  27. * @return void
  28. */
  29. static public function redirect() {
  30. global $pagenow;
  31. $post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : null;
  32. $args = $_GET;
  33. if ( 'post-new.php' == $pagenow && 'fl-builder-template' == $post_type ) {
  34. $args['page'] = 'fl-builder-add-new';
  35. wp_redirect( admin_url( '/edit.php?' . http_build_query( $args ) ) );
  36. exit;
  37. }
  38. }
  39. /**
  40. * Enqueue scripts and styles for user templates.
  41. *
  42. * @since 1.10
  43. * @return void
  44. */
  45. static public function admin_enqueue_scripts() {
  46. global $pagenow;
  47. global $post;
  48. $screen = get_current_screen();
  49. $slug = 'fl-builder-user-templates-admin-';
  50. $url = VAMTAMEL_B_USER_TEMPLATES_URL;
  51. $version = FL_BUILDER_VERSION;
  52. if ( 'post.php' == $pagenow && 'fl-builder-template' == $screen->post_type ) {
  53. wp_enqueue_style( $slug . 'edit', $url . 'css/' . $slug . 'edit.css', array(), $version );
  54. wp_enqueue_script( $slug . 'edit', $url . 'js/' . $slug . 'edit.js', array(), $version );
  55. wp_localize_script( $slug . 'edit', 'FLBuilderConfig', array(
  56. 'pageTitle' => self::get_page_title(),
  57. 'userTemplateType' => FLBuilderModel::get_user_template_type( $post->ID ),
  58. 'addNewURL' => admin_url( '/edit.php?post_type=fl-builder-template&page=fl-builder-add-new' ),
  59. ) );
  60. }
  61. }
  62. /**
  63. * Returns the page title for the edit screen.
  64. *
  65. * @since 1.10
  66. * @return string
  67. */
  68. static public function get_page_title() {
  69. global $post;
  70. $type = FLBuilderModel::get_user_template_type( $post->ID );
  71. $action = __( 'Edit', 'vamtam-elements-b' );
  72. if ( 'row' == $type ) {
  73. $label = sprintf( _x( '%s Saved Row', '%s is an action like Add, Edit or View.', 'vamtam-elements-b' ), $action );
  74. } elseif ( 'module' == $type ) {
  75. $label = sprintf( _x( '%s Saved Module', '%s is an action like Add, Edit or View.', 'vamtam-elements-b' ), $action );
  76. } else {
  77. $label = sprintf( _x( '%s Template', '%s is an action like Add, Edit or View.', 'vamtam-elements-b' ), $action );
  78. }
  79. return $label;
  80. }
  81. /**
  82. * Renders a notice div for global nodes.
  83. *
  84. * @since 1.0
  85. * @return void
  86. */
  87. static public function render_global_node_message() {
  88. global $pagenow;
  89. global $post;
  90. $screen = get_current_screen();
  91. if ( 'post.php' == $pagenow && 'fl-builder-template' == $screen->post_type ) {
  92. if ( FLBuilderModel::is_post_global_node_template( $post->ID ) ) {
  93. $type = FLBuilderModel::get_user_template_type( $post->ID );
  94. include VAMTAMEL_B_USER_TEMPLATES_DIR . 'includes/admin-edit-global-message.php';
  95. }
  96. }
  97. }
  98. /**
  99. * Callback for adding meta boxes to the user template
  100. * post edit screen.
  101. *
  102. * @since 1.0
  103. * @return void
  104. */
  105. static public function add_meta_boxes() {
  106. add_meta_box(
  107. 'fl-builder-user-template-buttons',
  108. FLBuilderModel::get_branding(),
  109. __CLASS__ . '::render_buttons_meta_box',
  110. 'fl-builder-template',
  111. 'normal',
  112. 'high'
  113. );
  114. }
  115. /**
  116. * Adds custom buttons to the edit screen for launching the builder
  117. * or viewing a template.
  118. *
  119. * @since 1.10
  120. * @return void
  121. */
  122. static public function render_buttons_meta_box() {
  123. global $post;
  124. $type = FLBuilderModel::get_user_template_type( $post->ID );
  125. $edit = sprintf( _x( 'Launch %s', '%s stands for custom branded "Page Builder" name.', 'vamtam-elements-b' ), FLBuilderModel::get_branding() );
  126. $view = __( 'View', 'vamtam-elements-b' );
  127. if ( 'fl-builder-template' == $post->post_type ) {
  128. if ( 'row' == $type ) {
  129. $view = sprintf( _x( '%s Saved Row', '%s is an action like Add, Edit or View.', 'vamtam-elements-b' ), $view );
  130. } elseif ( 'module' == $type ) {
  131. $view = sprintf( _x( '%s Saved Module', '%s is an action like Add, Edit or View.', 'vamtam-elements-b' ), $view );
  132. } else {
  133. $view = sprintf( _x( '%s Template', '%s is an action like Add, Edit or View.', 'vamtam-elements-b' ), $view );
  134. }
  135. } else {
  136. $object = get_post_type_object( $post->post_type );
  137. $view = sprintf( _x( '%1$s %2$s', '%1$s is an action like Add, Edit or View. %2$s is post type label.', 'vamtam-elements-b' ), $view, $object->labels->singular_name );
  138. }
  139. include VAMTAMEL_B_USER_TEMPLATES_DIR . 'includes/admin-edit-buttons.php';
  140. }
  141. /**
  142. * Prevents the standard builder admin edit UI from rendering.
  143. *
  144. * @since 1.10
  145. * @param bool $render_ui
  146. * @return bool
  147. */
  148. static public function remove_builder_edit_ui( $render_ui ) {
  149. return 'fl-builder-template' == FLBuilderAdminPosts::get_post_type() ? false : $render_ui;
  150. }
  151. }
  152. FLBuilderUserTemplatesAdminEdit::init();