class-fl-builder-shortcodes.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Helper class for builder shortcodes
  4. *
  5. * @since 1.7
  6. */
  7. final class FLBuilderShortcodes {
  8. /**
  9. * Adds all shortcodes for the builder.
  10. *
  11. * @since 1.7
  12. * @return void
  13. */
  14. static public function init() {
  15. add_shortcode( 'fl_builder_insert_layout', 'FLBuilderShortcodes::insert_layout' );
  16. }
  17. /**
  18. * Renders a layout with the provided post ID and enqueues the
  19. * necessary styles and scripts.
  20. *
  21. * @since 1.7
  22. * @param array $attrs The shortcode attributes.
  23. * @return string
  24. */
  25. static public function insert_layout( $attrs ) {
  26. $builder_active = in_the_loop() && FLBuilderModel::is_builder_active();
  27. $post_type = isset( $attrs['type'] ) ? $attrs['type'] : get_post_types();
  28. $site_id = isset( $attrs['site'] ) ? absint( $attrs['site'] ) : null;
  29. $args = array(
  30. 'post_type' => $post_type,
  31. 'posts_per_page' => -1,
  32. );
  33. // Build the args array.
  34. if ( isset( $attrs['id'] ) ) {
  35. $args['orderby'] = 'post__in';
  36. $args['ignore_sticky_posts'] = true;
  37. if ( is_numeric( $attrs['id'] ) ) {
  38. $args['post__in'] = array( $attrs['id'] );
  39. } else {
  40. $args['post__in'] = explode( ',', $attrs['id'] );
  41. }
  42. } elseif ( isset( $attrs['slug'] ) ) {
  43. $args['orderby'] = 'name';
  44. $args['name'] = $attrs['slug'];
  45. } else {
  46. return;
  47. }
  48. $render = apply_filters( 'fl_builder_insert_layout_render', true, $attrs, $args );
  49. if ( ! $render ) {
  50. return;
  51. }
  52. // Render and return the layout.
  53. ob_start();
  54. if ( $builder_active ) {
  55. echo '<div class="fl-builder-shortcode-mask-wrap"><div class="fl-builder-shortcode-mask"></div>';
  56. }
  57. FLBuilder::render_query( $args, $site_id );
  58. if ( $builder_active ) {
  59. echo '</div>';
  60. }
  61. return ob_get_clean();
  62. }
  63. }
  64. FLBuilderShortcodes::init();