class-fl-builder-wp-blocks.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Beaver Builder support for WordPress blocks.
  4. *
  5. * @since 2.1
  6. */
  7. final class FLBuilderWPBlocks {
  8. /**
  9. * @since 2.1
  10. * @return void
  11. */
  12. static public function init() {
  13. add_action( 'init', __CLASS__ . '::setup' );
  14. }
  15. /**
  16. * @since 2.1
  17. * @return void
  18. */
  19. static public function setup() {
  20. if ( ! function_exists( 'register_block_type' ) ) {
  21. return;
  22. }
  23. // Actions
  24. add_action( 'enqueue_block_editor_assets', __CLASS__ . '::enqueue_block_editor_assets' );
  25. // Block Files
  26. require_once FL_BUILDER_DIR . 'classes/class-fl-builder-wp-blocks-layout.php';
  27. }
  28. /**
  29. * Enqueues scripts and styles for the block editor.
  30. *
  31. * @since 2.1
  32. * @return void
  33. */
  34. static public function enqueue_block_editor_assets() {
  35. global $post;
  36. if ( ! is_object( $post ) ) {
  37. return;
  38. } elseif ( ! in_array( $post->post_type, FLBuilderModel::get_post_types() ) ) {
  39. return;
  40. }
  41. $branding = FLBuilderModel::get_branding();
  42. $post_type_object = get_post_type_object( $post->post_type );
  43. $post_type_name = $post_type_object->labels->singular_name;
  44. $min = ( ! FLBuilder::is_debug() ) ? '.min' : '';
  45. wp_enqueue_style(
  46. 'fl-builder-wp-editor',
  47. FL_BUILDER_URL . 'css/build/wp-editor.bundle' . $min . '.css',
  48. array(),
  49. FL_BUILDER_VERSION
  50. );
  51. wp_enqueue_script(
  52. 'fl-builder-wp-editor',
  53. FL_BUILDER_URL . 'js/build/wp-editor.bundle' . $min . '.js',
  54. array( 'wp-editor' ),
  55. FL_BUILDER_VERSION
  56. );
  57. wp_localize_script( 'fl-builder-wp-editor', 'FLBuilderConfig', array(
  58. 'builder' => array(
  59. 'access' => FLBuilderUserAccess::current_user_can( 'builder_access' ),
  60. 'enabled' => FLBuilderModel::is_builder_enabled( $post->ID ),
  61. 'nonce' => wp_create_nonce( 'fl_ajax_update' ),
  62. 'unrestricted' => FLBuilderUserAccess::current_user_can( 'unrestricted_editing' ),
  63. ),
  64. 'post' => array(
  65. 'id' => $post->ID,
  66. ),
  67. 'strings' => array(
  68. 'active' => sprintf( _x( '%1$s is currently active for this %2$s.', '%1$s branded builder name. %2$s post type name.', 'fl-builder' ), $branding, strtolower( $post_type_name ) ),
  69. 'convert' => sprintf( _x( 'Convert to %s', '%s branded builder name.', 'fl-builder' ), $branding ),
  70. 'description' => sprintf( _x( '%s lets you drag and drop your layout on the frontend.', '%s branded builder name.', 'fl-builder' ), $branding ),
  71. 'editor' => __( 'Use Standard Editor', 'fl-builder' ),
  72. 'launch' => sprintf( _x( 'Launch %s', '%s branded builder name.', 'fl-builder' ), $branding ),
  73. 'title' => $branding,
  74. 'view' => sprintf( _x( 'View %s', '%s post type name.', 'fl-builder' ), $post_type_name ),
  75. 'warning' => __( 'Switching to the native WordPress editor will disable your Beaver Builder layout until it is enabled again. Any edits made in the WordPress editor will not be converted to your Page Builded layout. Do you want to continue?', 'fl-builder' ),
  76. ),
  77. 'urls' => array(
  78. 'edit' => FLBuilderModel::get_edit_url( $post->ID ),
  79. 'view' => get_permalink( $post->ID ),
  80. ),
  81. ) );
  82. }
  83. }
  84. FLBuilderWPBlocks::init();