class-foogallery-divi-module.php
127 lines
| 1 | <?php |
| 2 | /** |
| 3 | * FooGallery Divi Builder module. |
| 4 | * |
| 5 | * @package FooGallery |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | if ( class_exists( 'ET_Builder_Module' ) && ! class_exists( 'FooGallery_Divi_Module' ) ) { |
| 13 | |
| 14 | /** |
| 15 | * Lets Divi users select and render an existing FooGallery gallery. |
| 16 | */ |
| 17 | class FooGallery_Divi_Module extends ET_Builder_Module { |
| 18 | |
| 19 | /** |
| 20 | * Configure the module. |
| 21 | * |
| 22 | * @return void |
| 23 | */ |
| 24 | public function init() { |
| 25 | $this->name = esc_html__( 'FooGallery', 'foogallery' ); |
| 26 | $this->plural = esc_html__( 'FooGalleries', 'foogallery' ); |
| 27 | $this->slug = 'foogallery_divi_gallery'; |
| 28 | $this->vb_support = 'partial'; |
| 29 | |
| 30 | $this->settings_modal_toggles = array( |
| 31 | 'general' => array( |
| 32 | 'toggles' => array( |
| 33 | 'main_content' => esc_html__( 'Gallery', 'foogallery' ), |
| 34 | ), |
| 35 | ), |
| 36 | ); |
| 37 | |
| 38 | $this->advanced_fields = array( |
| 39 | 'background' => false, |
| 40 | 'button' => false, |
| 41 | 'fonts' => false, |
| 42 | 'text' => false, |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Build the gallery choices shown by Divi. |
| 48 | * |
| 49 | * @return string[] |
| 50 | */ |
| 51 | public static function get_gallery_options() { |
| 52 | $options = array( |
| 53 | '' => esc_html__( 'Select a gallery', 'foogallery' ), |
| 54 | ); |
| 55 | |
| 56 | $galleries = foogallery_get_all_galleries(); |
| 57 | |
| 58 | if ( empty( $galleries ) ) { |
| 59 | return $options; |
| 60 | } |
| 61 | |
| 62 | foreach ( $galleries as $gallery ) { |
| 63 | $name = $gallery->name; |
| 64 | |
| 65 | if ( empty( $name ) ) { |
| 66 | /* translators: %d: Gallery post ID. */ |
| 67 | $name = sprintf( esc_html__( 'Gallery #%d', 'foogallery' ), $gallery->ID ); |
| 68 | } |
| 69 | |
| 70 | $options[ (string) $gallery->ID ] = wp_strip_all_tags( $name ); |
| 71 | } |
| 72 | |
| 73 | return $options; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Define the module settings. |
| 78 | * |
| 79 | * @return array |
| 80 | */ |
| 81 | public function get_fields() { |
| 82 | return array( |
| 83 | 'gallery_id' => array( |
| 84 | 'label' => esc_html__( 'Gallery', 'foogallery' ), |
| 85 | 'type' => 'select', |
| 86 | 'option_category' => 'basic_option', |
| 87 | 'options' => self::get_gallery_options(), |
| 88 | 'toggle_slug' => 'main_content', |
| 89 | 'description' => esc_html__( 'Choose an existing FooGallery gallery to display.', 'foogallery' ), |
| 90 | ), |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Render the selected gallery. |
| 96 | * |
| 97 | * @param array $attrs Module attributes. |
| 98 | * @param string $content Module content. |
| 99 | * @param string $render_slug Module slug used during rendering. |
| 100 | * |
| 101 | * @return string |
| 102 | */ |
| 103 | public function render( $attrs, $content, $render_slug ) { |
| 104 | $gallery_id = isset( $this->props['gallery_id'] ) ? absint( $this->props['gallery_id'] ) : 0; |
| 105 | |
| 106 | if ( 0 === $gallery_id ) { |
| 107 | if ( is_admin() || ( function_exists( 'et_core_is_fb_enabled' ) && et_core_is_fb_enabled() ) ) { |
| 108 | return '<p>' . esc_html__( 'Please select a gallery to display.', 'foogallery' ) . '</p>'; |
| 109 | } |
| 110 | |
| 111 | return ''; |
| 112 | } |
| 113 | |
| 114 | ob_start(); |
| 115 | foogallery_render_gallery( $gallery_id ); |
| 116 | $gallery_output = ob_get_clean(); |
| 117 | |
| 118 | return sprintf( |
| 119 | '<div%1$s class="%2$s">%3$s</div>', |
| 120 | $this->module_id(), |
| 121 | $this->module_classname( $render_slug ), |
| 122 | $gallery_output // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- FooGallery renders its own escaped gallery markup. |
| 123 | ); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 |