settings = getShortcodeSettings(); $this->pluginname = $this->settings['block']['blockname']; add_action( 'admin_enqueue_scripts', [$this, 'enqueueGutenberg'] ); add_action( 'init', [$this, 'initGutenberg'] ); add_action('admin_head', [$this, 'setMCEConfig']); add_filter('mce_external_plugins', [$this, 'addMCEButtons']); add_shortcode( 'downloads', [ $this, 'shortcodeOutput' ]); add_shortcode( 'download', [ $this, 'shortcodeOutput' ]); } public function shortcodeOutput( $atts ) { $atts = shortcode_atts([ 'category' => '', "cat" => '', "tags" => '', "type" => 'category', // category, document "format" => 'liste', // table, liste "htmlpre" => '', "htmlpost" => '', "htmlitempre" => '', "htmlitempost" => '', "search_application" => false, "search_image" => false, "search_video" => false, "search_audio" => false, "search_text" => false, "showsize" => false, "showcreated" => false, "showexcerpt" => false, "showcontent" => false, "errormsg" => '', "orderby" => 'title', "sort" => 'asc' ], $atts, 'downloads'); $output = ''; $sp = ' '; $category = esc_attr($atts['category']); $tags = esc_attr($atts['tags']); $format = esc_attr($atts['format']); $type = esc_attr($atts['type']); $htmlpre = esc_attr($atts['htmlpre']); $htmlpost = esc_attr($atts['htmlpost']); $htmlitempre = esc_attr($atts['htmlitempre']); $htmlitempost = esc_attr($atts['htmlitempost']); $errormsg = esc_attr($atts['errormsg']); $orderby = esc_attr($atts['orderby']); $sort = esc_attr($atts['sort']); $search_application = filter_var($atts['search_application'], FILTER_VALIDATE_BOOLEAN); $search_image = filter_var($atts['search_image'], FILTER_VALIDATE_BOOLEAN); $search_video = filter_var($atts['search_video'], FILTER_VALIDATE_BOOLEAN); $search_audio = filter_var($atts['search_audio'], FILTER_VALIDATE_BOOLEAN); $search_text = filter_var($atts['search_text'], FILTER_VALIDATE_BOOLEAN); $showsize = filter_var($atts['showsize'], FILTER_VALIDATE_BOOLEAN); $showcreated = filter_var($atts['showcreated'], FILTER_VALIDATE_BOOLEAN); $showexcerpt = filter_var($atts['showexcerpt'], FILTER_VALIDATE_BOOLEAN); $showcontent = filter_var($atts['showcontent'], FILTER_VALIDATE_BOOLEAN); $orderby = !empty($orderby) && in_array(strtolower($orderby), array('title', 'date')) ? strtolower($orderby) : 'title'; $sort = !empty($sort) && in_array(strtoupper($sort), array('ASC', 'DESC')) ? strtoupper($sort) : 'ASC'; $type = in_array($type, array('category', 'document')) ? $type : 'category'; $category = get_term_by('slug', $category, 'attachment_' . $type); $atts = array('post_type' => 'attachment', 'post_status' => 'any', 'posts_per_page' => -1, 'orderby' => $orderby, 'order' => $sort, 'tax_query' => array(), 'suppress_filters' => true); if ($category) { $catquery = array( 'taxonomy' => 'attachment_' . $type, 'field' => 'id', // can be slug or id - a CPT-onomy term's ID is the same as its post ID 'terms' => $category->term_id, 'include_children' => false ); $atts['tax_query'][] = $catquery; } if ($tags) { $tagquery = array( 'taxonomy' => 'attachment_tag', 'field' => 'slug', 'terms' => explode(',', $tags), ); $atts['tax_query'][] = $tagquery; } $mimetype = array(); if ($search_application == true) { $mimetype[] = 'application'; } if ($search_video == true) { $mimetype[] = 'video'; } if ($search_image == true) { $mimetype[] = 'image'; } if ($search_audio == true) { $mimetype[] = 'audio'; } if ($search_text == true) { $mimetype[] = 'text'; } if (!empty($mimetype)) { $atts['post_mime_type'] = $mimetype; } else { $atts['post_mime_type'] = "application"; } $files = get_posts($atts); if ($files) { $prehtml = ''; $posthtml = ''; if ($format == 'table') { $prehtml = '
' . $errormsg . '
'; } else { $output .= '' . __('Dowloads: Es konnten keine Dateien gefunden werden.', 'rrze-downloads') . '
'; } } return $output; } public function isGutenberg(){ $postID = get_the_ID(); if ($postID && !use_block_editor_for_post($postID)){ return false; } return true; } public function initGutenberg() { if (! $this->isGutenberg()){ return; } // register js-script to inject php config to call gutenberg lib $editor_script = $this->settings['block']['blockname'] . '-block'; $js = '../assets/js/' . $editor_script . '.js'; wp_register_script( $editor_script, plugins_url( $js, __FILE__ ), array( 'RRZE-Gutenberg', ), NULL ); wp_localize_script( $editor_script, $this->settings['block']['blockname'] . 'Config', $this->settings ); // register block register_block_type( $this->settings['block']['blocktype'], array( 'editor_script' => $editor_script, 'render_callback' => [$this, 'shortcodeOutput'], 'attributes' => $this->settings ) ); } public function enqueueGutenberg(){ if (! $this->isGutenberg()){ return; } // include gutenberg lib wp_enqueue_script( 'RRZE-Gutenberg', plugins_url( '../assets/js/gutenberg.js', __FILE__ ), array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor' ), NULL ); } public function setMCEConfig(){ $shortcode = ''; foreach($this->settings as $att => $details){ if ($att != 'block'){ $shortcode .= ' ' . $att . '=""'; } } $shortcode = '[' . $this->pluginname . ' ' . $shortcode . ']'; ?>