Embedpress_Document.php
284 lines
| 1 | <?php |
| 2 | |
| 3 | namespace EmbedPress\Elementor\Widgets; |
| 4 | |
| 5 | |
| 6 | use \Elementor\Controls_Manager as Controls_Manager; |
| 7 | use \Elementor\Modules\DynamicTags\Module as TagsModule; |
| 8 | use \Elementor\Widget_Base as Widget_Base; |
| 9 | use \Elementor\Plugin; |
| 10 | |
| 11 | ( defined( 'ABSPATH' ) ) or die( "No direct script access allowed." ); |
| 12 | |
| 13 | class Embedpress_Document extends Widget_Base |
| 14 | { |
| 15 | |
| 16 | public function get_name() |
| 17 | { |
| 18 | return 'embedpres_document'; |
| 19 | } |
| 20 | |
| 21 | public function get_title() |
| 22 | { |
| 23 | return esc_html__( 'EmbedPress Document', 'embedoress' ); |
| 24 | } |
| 25 | |
| 26 | public function get_categories() |
| 27 | { |
| 28 | return ['embedpress']; |
| 29 | } |
| 30 | |
| 31 | public function get_custom_help_url() |
| 32 | { |
| 33 | return 'https://embedpress.com/documentation'; |
| 34 | } |
| 35 | |
| 36 | public function get_icon() |
| 37 | { |
| 38 | return 'icon-pdf'; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Get widget keywords. |
| 43 | * |
| 44 | * Retrieve the list of keywords the widget belongs to. |
| 45 | * |
| 46 | * @return array Widget keywords. |
| 47 | * @since 2.5.5 |
| 48 | * @access public |
| 49 | * |
| 50 | */ |
| 51 | public function get_keywords() |
| 52 | { |
| 53 | return ['embedpress', 'pdf', 'doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx', 'embedpress-document']; |
| 54 | } |
| 55 | |
| 56 | protected function _register_controls() |
| 57 | { |
| 58 | |
| 59 | /** |
| 60 | * EmbedPress Content Settings |
| 61 | */ |
| 62 | $this->start_controls_section( |
| 63 | 'embedpress_document_content_settings', |
| 64 | [ |
| 65 | 'label' => esc_html__( 'Content Settings', 'embedpress' ), |
| 66 | ] |
| 67 | ); |
| 68 | |
| 69 | $this->add_control( |
| 70 | 'embedpress_document_type', |
| 71 | [ |
| 72 | 'label' => __( 'Document Type', 'embedpress' ), |
| 73 | 'type' => Controls_Manager::SELECT, |
| 74 | 'default' => 'file', |
| 75 | 'options' => [ |
| 76 | 'file' => __( 'File', 'embedpress' ), |
| 77 | 'url' => __( 'URL', 'embedpress' ) |
| 78 | ], |
| 79 | ] |
| 80 | ); |
| 81 | |
| 82 | $this->add_control( |
| 83 | 'embedpress_document_Uploader', |
| 84 | [ |
| 85 | |
| 86 | 'label' => __( 'Upload File', 'embedpress' ), |
| 87 | 'type' => Controls_Manager::MEDIA, |
| 88 | 'dynamic' => [ |
| 89 | 'active' => true, |
| 90 | 'categories' => [ |
| 91 | TagsModule::MEDIA_CATEGORY, |
| 92 | ], |
| 93 | ], |
| 94 | 'media_type' => [ |
| 95 | 'application/pdf', |
| 96 | 'application/msword', |
| 97 | 'application/vnd.ms-powerpoint', |
| 98 | 'application/vnd.ms-excel', |
| 99 | 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
| 100 | 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
| 101 | 'application/vnd.openxmlformats-officedocument.presentationml.presentation' |
| 102 | ], |
| 103 | 'description' => __( 'Upload a file or pick one from your media library for embed. Supported File Type: PDF, DOC/DOCX, PPT/PPTX, XLS/XLSX etc.', |
| 104 | 'embedpress' ), |
| 105 | 'condition' => [ |
| 106 | 'embedpress_document_type' => 'file' |
| 107 | ], |
| 108 | ] |
| 109 | ); |
| 110 | |
| 111 | $this->add_control( |
| 112 | 'embedpress_document_file_link', |
| 113 | [ |
| 114 | 'label' => __( 'URL', 'embedpress' ), |
| 115 | 'type' => Controls_Manager::URL, |
| 116 | 'placeholder' => __( 'https://your-link.com/file.pdf', 'embedpress' ), |
| 117 | 'show_external' => false, |
| 118 | 'default' => [ |
| 119 | 'url' => '' |
| 120 | ], |
| 121 | 'condition' => [ |
| 122 | 'embedpress_document_type' => 'url' |
| 123 | ], |
| 124 | ] |
| 125 | ); |
| 126 | |
| 127 | $this->add_control( |
| 128 | 'embedpress_elementor_document_width', |
| 129 | [ |
| 130 | 'label' => __( 'Width', 'embedpress' ), |
| 131 | 'type' => Controls_Manager::SLIDER, |
| 132 | 'separator' => 'before', |
| 133 | 'default' => [ |
| 134 | 'unit' => 'px', |
| 135 | 'size' => 600, |
| 136 | ], |
| 137 | 'range' => [ |
| 138 | 'px' => [ |
| 139 | 'min' => 6, |
| 140 | 'max' => 1000, |
| 141 | ], |
| 142 | ], |
| 143 | 'selectors' => [ |
| 144 | '{{WRAPPER}} .embedpress-document-embed iframe' => 'width: {{SIZE}}{{UNIT}} !important;', |
| 145 | '{{WRAPPER}} .embedpress-document-embed .pdfobject-container' => 'width: {{SIZE}}{{UNIT}} !important;', |
| 146 | ], |
| 147 | ] |
| 148 | ); |
| 149 | |
| 150 | $this->add_control( |
| 151 | 'embedpress_elementor_document_height', |
| 152 | [ |
| 153 | 'label' => __( 'Height', 'embedpress' ), |
| 154 | 'type' => Controls_Manager::SLIDER, |
| 155 | 'default' => [ |
| 156 | 'unit' => 'px', |
| 157 | 'size' => 600, |
| 158 | ], |
| 159 | 'range' => [ |
| 160 | 'px' => [ |
| 161 | 'min' => 6, |
| 162 | 'max' => 1000, |
| 163 | ], |
| 164 | ], |
| 165 | 'selectors' => [ |
| 166 | '{{WRAPPER}} .embedpress-document-embed iframe' => 'height: {{SIZE}}{{UNIT}};', |
| 167 | '{{WRAPPER}} .embedpress-document-embed .pdfobject-container' => 'height: {{SIZE}}{{UNIT}};', |
| 168 | ], |
| 169 | ] |
| 170 | ); |
| 171 | |
| 172 | $this->add_control( |
| 173 | 'embedpress_elementor_document_align', |
| 174 | [ |
| 175 | 'label' => __( 'Alignment', 'embedpress' ), |
| 176 | 'type' => Controls_Manager::CHOOSE, |
| 177 | 'options' => [ |
| 178 | 'left' => [ |
| 179 | 'title' => __( 'Left', 'embedpress' ), |
| 180 | 'icon' => 'eicon-text-align-left', |
| 181 | ], |
| 182 | 'center' => [ |
| 183 | 'title' => __( 'Center', 'embedpress' ), |
| 184 | 'icon' => 'eicon-text-align-center', |
| 185 | ], |
| 186 | 'right' => [ |
| 187 | 'title' => __( 'Right', 'embedpress' ), |
| 188 | 'icon' => 'eicon-text-align-right', |
| 189 | ] |
| 190 | ], |
| 191 | 'default' => 'center', |
| 192 | ] |
| 193 | ); |
| 194 | |
| 195 | $this->add_control( |
| 196 | 'embedpress_document_powered_by', |
| 197 | [ |
| 198 | 'label' => __( 'Powered By', 'embedpress' ), |
| 199 | 'type' => Controls_Manager::SWITCHER, |
| 200 | 'label_on' => __( 'Show', 'embedpress' ), |
| 201 | 'label_off' => __( 'Hide', 'embedpress' ), |
| 202 | 'return_value' => 'yes', |
| 203 | 'default' => apply_filters( 'embedpress_document_powered_by_control', 'yes' ), |
| 204 | ] |
| 205 | ); |
| 206 | |
| 207 | |
| 208 | $this->end_controls_section(); |
| 209 | } |
| 210 | |
| 211 | private function is_pdf( $url ) |
| 212 | { |
| 213 | $arr = explode( '.', $url ); |
| 214 | return end( $arr ) === 'pdf'; |
| 215 | } |
| 216 | |
| 217 | protected function render() |
| 218 | { |
| 219 | $settings = $this->get_settings(); |
| 220 | $url = $this->get_file_url(); |
| 221 | $id = 'embedpress-pdf-' . $this->get_id(); |
| 222 | $dimension = "width: {$settings['embedpress_elementor_document_width']['size']}px;height: {$settings['embedpress_elementor_document_height']['size']}px"; |
| 223 | $this->add_render_attribute( 'embedpres-pdf-render', [ |
| 224 | 'class' => ['embedpress-embed-document-pdf', $id], |
| 225 | 'data-emid' => $id |
| 226 | ] ); |
| 227 | $this->add_render_attribute( 'embedpress-document', [ |
| 228 | 'class' => ['embedpress-document-embed'] |
| 229 | ] ); |
| 230 | ?> |
| 231 | <div <?php echo $this->get_render_attribute_string( 'embedpress-document' ); ?>> |
| 232 | <?php if ( $url != '' ) { |
| 233 | if ( $this->is_pdf( $url ) ) { |
| 234 | $this->add_render_attribute( 'embedpres-pdf-render', 'data-emsrc', $url ); |
| 235 | ?> |
| 236 | <div <?php echo $this->get_render_attribute_string( 'embedpres-pdf-render' ); ?>></div> |
| 237 | <?php |
| 238 | |
| 239 | if ( Plugin::$instance->editor->is_edit_mode() ) { |
| 240 | $this->render_editor_script( $id, $url ); |
| 241 | } |
| 242 | |
| 243 | } else { |
| 244 | $view_link = 'https://docs.google.com/viewer?url=' . $url . '&embedded=true'; |
| 245 | ?> |
| 246 | <iframe allowfullscreen="true" |
| 247 | mozallowfullscreen="true" webkitallowfullscreen="true" style="<?php echo $dimension; ?>" src="<?php echo $view_link; ?>"/> |
| 248 | <?php |
| 249 | |
| 250 | } |
| 251 | if ( $settings[ 'embedpress_document_powered_by' ] === 'yes' ) { |
| 252 | printf( '<p class="embedpress-el-powered">%s</p>', __( 'Powered By EmbedPress', 'embedpress' ) ); |
| 253 | } |
| 254 | } ?> |
| 255 | |
| 256 | </div> |
| 257 | |
| 258 | <?php |
| 259 | } |
| 260 | |
| 261 | private function get_file_url() |
| 262 | { |
| 263 | $settings = $this->get_settings(); |
| 264 | return $settings[ 'embedpress_document_type' ] === 'url' ? $settings[ 'embedpress_document_file_link' ][ 'url' ] : $settings[ 'embedpress_document_Uploader' ][ 'url' ]; |
| 265 | } |
| 266 | |
| 267 | protected function render_editor_script( $id, $url ) |
| 268 | { |
| 269 | ?> |
| 270 | <script> |
| 271 | (function ($) { |
| 272 | 'use strict'; |
| 273 | $(document).ready(function () { |
| 274 | var selector = $('.embedpress-embed-document-pdf'); |
| 275 | if (selector.length) { |
| 276 | PDFObject.embed("<?php echo $url; ?>", "<?php echo '.' . $id; ?>"); |
| 277 | } |
| 278 | }); |
| 279 | })(jQuery); |
| 280 | </script> |
| 281 | <?php |
| 282 | } |
| 283 | } |
| 284 |