module.php
144 lines
| 1 | <?php |
| 2 | namespace Auxin\Plugin\CoreElements\Elementor\Modules\ThemeBuilder; |
| 3 | |
| 4 | use Elementor\Core\Base\Module AS Module_Base; |
| 5 | use Elementor\Core\Base\Document; |
| 6 | use Elementor\Elements_Manager; |
| 7 | use Auxin\Plugin\CoreElements\Elementor\Modules\ThemeBuilder\Theme_Document as Theme_Document; |
| 8 | use Elementor\Plugin; |
| 9 | |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | exit; // Exit if accessed directly |
| 12 | } |
| 13 | |
| 14 | class Module extends Module_Base { |
| 15 | |
| 16 | public static function is_preview() { |
| 17 | return Plugin::instance()->preview->is_preview_mode() || is_preview(); |
| 18 | } |
| 19 | |
| 20 | public function get_name() { |
| 21 | return 'theme-builder'; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @return Classes\Preview_Manager |
| 26 | */ |
| 27 | public function get_preview_manager() { |
| 28 | return $this->get_component( 'preview' ); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @param $post_id |
| 33 | * |
| 34 | * @return Theme_Document |
| 35 | */ |
| 36 | public function get_document( $post_id ) { |
| 37 | $document = null; |
| 38 | |
| 39 | try { |
| 40 | $document = Plugin::instance()->documents->get( $post_id ); |
| 41 | } catch ( \Exception $e ) { |
| 42 | // Do nothing. |
| 43 | unset( $e ); |
| 44 | } |
| 45 | |
| 46 | // if ( ! empty( $document ) && ! $document instanceof Theme_Document ) { |
| 47 | // $document = null; |
| 48 | // } |
| 49 | |
| 50 | return $document; |
| 51 | } |
| 52 | |
| 53 | public function create_new_dialog_types( $types ) { |
| 54 | /** |
| 55 | * @var Theme_Document[] $document_types |
| 56 | */ |
| 57 | foreach ( $types as $type => $label ) { |
| 58 | $document_type = Plugin::instance()->documents->get_document_type( $type ); |
| 59 | $instance = new $document_type(); |
| 60 | |
| 61 | if ( $instance instanceof Theme_Document && 'section' !== $type ) { |
| 62 | $types[ $type ] .= $instance->get_location_label(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return $types; |
| 67 | } |
| 68 | |
| 69 | public function print_location_field() { |
| 70 | $locations = $this->get_locations_manager()->get_locations( [ |
| 71 | 'public' => true, |
| 72 | ] ); |
| 73 | |
| 74 | if ( empty( $locations ) ) { |
| 75 | return; |
| 76 | } |
| 77 | ?> |
| 78 | <div id="elementor-new-template__form__location__wrapper" class="elementor-form-field"> |
| 79 | <label for="elementor-new-template__form__location" class="elementor-form-field__label"> |
| 80 | <?php echo esc_html__( 'Select a Location', 'auxin-elements' ); ?> |
| 81 | </label> |
| 82 | <div class="elementor-form-field__select__wrapper"> |
| 83 | <select id="elementor-new-template__form__location" class="elementor-form-field__select" name="meta_location"> |
| 84 | <option value=""> |
| 85 | <?php echo esc_html__( 'Select...', 'auxin-elements' ); ?> |
| 86 | </option> |
| 87 | <?php |
| 88 | |
| 89 | foreach ( $locations as $location => $settings ) { |
| 90 | echo sprintf( '<option value="%1$s">%2$s</option>', esc_attr( $location ), esc_html( $settings['label'] ) ); |
| 91 | } |
| 92 | ?> |
| 93 | </select> |
| 94 | </div> |
| 95 | </div> |
| 96 | <?php |
| 97 | } |
| 98 | |
| 99 | public function print_post_type_field() { |
| 100 | $post_types = get_post_types( [ |
| 101 | 'exclude_from_search' => false, |
| 102 | ], 'objects' ); |
| 103 | |
| 104 | unset( $post_types['product'] ); |
| 105 | |
| 106 | if ( empty( $post_types ) ) { |
| 107 | return; |
| 108 | } |
| 109 | ?> |
| 110 | <div id="elementor-new-template__form__post-type__wrapper" class="elementor-form-field"> |
| 111 | <label for="elementor-new-template__form__post-type" class="elementor-form-field__label"> |
| 112 | <?php echo esc_html__( 'Select Post Type', 'auxin-elements' ); ?> |
| 113 | </label> |
| 114 | <div class="elementor-form-field__select__wrapper"> |
| 115 | <select id="elementor-new-template__form__post-type" class="elementor-form-field__select" name="_elementor_template_sub_type"> |
| 116 | <option value=""> |
| 117 | <?php echo esc_html__( 'Select', 'auxin-elements' ); ?>... |
| 118 | </option> |
| 119 | <?php |
| 120 | |
| 121 | foreach ( $post_types as $post_type => $post_type_config ) { |
| 122 | $doc_type = Plugin::instance()->documents->get_document_type( $post_type ); |
| 123 | $doc_name = ( new $doc_type() )->get_name(); |
| 124 | |
| 125 | if ( 'post' === $doc_name || 'page' === $doc_name ) { |
| 126 | echo sprintf( '<option value="%1$s">%2$s</option>', esc_attr( $post_type ), esc_html( $post_type_config->labels->singular_name ) ); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // 404. |
| 131 | echo sprintf( '<option value="%1$s">%2$s</option>', 'not_found404', esc_html__( '404 Page', 'auxin-elements' ) ); |
| 132 | |
| 133 | ?> |
| 134 | </select> |
| 135 | </div> |
| 136 | </div> |
| 137 | <?php |
| 138 | } |
| 139 | |
| 140 | public function __construct() { |
| 141 | $this->add_component( 'preview', new Classes\Preview_Manager() ); |
| 142 | } |
| 143 | } |
| 144 |