pluginName = $pluginName; $this->version = $version; $this->loader = new Loader(); $this->settings = $settings; $this->filterPostTypes = array_merge( $this->filterPostTypes, $this->settings['hiddenPostTypes'] ?? [ 'attachment' ] ); $this->defineHooks(); $this->loader->run(); foreach ( get_categories() as $category ) { $this->categoryList[$category->term_id] = $category; } if ( !empty( $this->settings['loadThumbs'] ) ) { $this->transistentThumbnailUrls = get_transient( 'aqb_thumbnails' ) ?: []; } } private function defineHooks(): void { $this->loader->addAction( 'wp_enqueue_scripts', $this, 'enqueueStyles' ); $this->loader->addAction( 'wp_enqueue_scripts', $this, 'enqueueScripts' ); $this->loader->addAction( 'admin_enqueue_scripts', $this, 'enqueueStyles' ); $this->loader->addAction( 'admin_enqueue_scripts', $this, 'enqueueScripts' ); $this->loader->addAction( 'elementor/editor/before_enqueue_styles', $this, 'enqueueStyles' ); $this->loader->addAction( 'elementor/editor/before_enqueue_scripts', $this, 'enqueueScripts', 99999 ); // embed to footer on admin $this->loader->addAction( 'admin_print_footer_scripts', $this, 'renderSidebar' ); // embed to footer on website $this->loader->addAction( 'wp_footer', $this, 'renderSidebar' ); $this->loader->addAction( 'set_current_user', $this, 'fixElementorLanguage', 11 ); $this->loader->addAction( 'wp_ajax_aqbRenamePost', $this, 'renamePost' ); } public function setPostTypes(): void { $this->postTypes = get_post_types( [], 'object' ); } public function setFilteredPostTypes(): void { foreach ( $this->postTypes as $postType ) { if ( in_array( $postType->name, $this->filterPostTypes ) ) { continue; } $this->filteredPostTypes[] = $postType; } } /** * Get cache list from swift and writes to $cacheList */ public function initCacheList(): void { if ( !$this->hasSwift() ) { return; } $this->cacheList = class_exists( 'Swift_Performance' ) ? \Swift_Performance::cache_status()['files'] : \Swift_Performance_Lite::cache_status()['files']; } private function hasSwift(): bool { return class_exists( 'Swift_Performance' ) || class_exists( 'Swift_Performance_Lite' ); } /** * @param \WP_Post $post */ public function getPostTitle( $post ): string { if ( !empty( $post->post_title ) ) { return $post->post_title; } if ( !empty( $post->post_name ) ) { return $post->post_name; } return $post->ID; } /** * @param \WP_Post $post * @param \WP_Post_Type $postType * @param int $lastParent * @param int $margin */ public function getMarginStyle( $post, $postType, &$lastParent, &$margin = 0 ): string { $style = ''; if ( empty( $post->post_parent ) ) { $margin = 0; } else if ( $post->post_parent !== $lastParent ) { $margin += 10; $lastParent = $post->post_parent; } if ( !empty( $margin ) && $postType->hierarchical ) { $style = ' style="margin-left:' . $margin . 'px;" '; } return $style; } public function getPostsByPostType( $postType ): array { global $wpdb; $countPostType = 0; $categories = []; if ( $this->isWpmlActive() ) { do_action( 'wpml_switch_language', 'all' ); } $categoryCount = []; if ( $postType->hierarchical ) { $cacheKey = 'aqb_' . $postType->name; $pages = wp_cache_get( $cacheKey ); if ( empty( $pages ) ) { if ( $this->isWpmlActive() ) { // phpcs:disable WordPress.DB.DirectDatabaseQuery $pages = $wpdb->get_results( $wpdb->prepare( " SELECT $wpdb->posts.ID, $wpdb->posts.post_title, $wpdb->posts.post_name, $wpdb->posts.post_status, $wpdb->posts.post_type, language_code FROM $wpdb->posts LEFT OUTER JOIN {$wpdb->prefix}icl_translations ON $wpdb->posts.ID = element_id AND element_type = %s WHERE $wpdb->posts.post_type = %s AND $wpdb->posts.post_status NOT IN ('auto-draft') ORDER BY `post_parent` ASC, menu_order ASC ", 'post_' . $postType->name, $postType->name ), OBJECT ); // phpcs:enable WordPress.DB.DirectDatabaseQuery } else { // phpcs:disable WordPress.DB.DirectDatabaseQuery $pages = $wpdb->get_results( $wpdb->prepare( " SELECT $wpdb->posts.ID, $wpdb->posts.post_title, $wpdb->posts.post_name, $wpdb->posts.post_status, $wpdb->posts.post_type FROM $wpdb->posts WHERE $wpdb->posts.post_type = %s AND $wpdb->posts.post_status NOT IN ('auto-draft') ORDER BY `post_parent` ASC, menu_order ASC ", $postType->name ), OBJECT ); // phpcs:enable WordPress.DB.DirectDatabaseQuery } wp_cache_set( $cacheKey, $pages ); } $categories = [ 'none' => $pages, ]; $countPostType += count( $pages ); } else { $cacheKey = 'aqb_' . $postType->name; $allPosts = wp_cache_get( $cacheKey ); if ( empty( $allPosts ) ) { if ( $this->isWpmlActive() ) { // phpcs:disable WordPress.DB.DirectDatabaseQuery $allPosts = $wpdb->get_results( $wpdb->prepare( " SELECT $wpdb->posts.ID, $wpdb->posts.post_title, $wpdb->posts.post_name, $wpdb->posts.post_status, $wpdb->posts.post_type, GROUP_CONCAT($wpdb->term_relationships.term_taxonomy_id) as post_category, language_code FROM $wpdb->posts LEFT OUTER JOIN $wpdb->term_relationships on $wpdb->posts.ID = $wpdb->term_relationships.object_id LEFT OUTER JOIN {$wpdb->prefix}icl_translations ON $wpdb->posts.ID = element_id AND element_type = %s WHERE $wpdb->posts.post_type = %s AND $wpdb->posts.post_status NOT IN ('auto-draft') GROUP BY $wpdb->posts.ID ORDER BY menu_order ASC ", 'post_' . $postType->name, $postType->name ), OBJECT ); // phpcs:enable WordPress.DB.DirectDatabaseQuery } else { // phpcs:disable WordPress.DB.DirectDatabaseQuery $allPosts = $wpdb->get_results( $wpdb->prepare( " SELECT $wpdb->posts.ID, $wpdb->posts.post_title, $wpdb->posts.post_name, $wpdb->posts.post_status, $wpdb->posts.post_type, GROUP_CONCAT($wpdb->term_relationships.term_taxonomy_id) as post_category FROM $wpdb->posts LEFT OUTER JOIN $wpdb->term_relationships on $wpdb->posts.ID = $wpdb->term_relationships.object_id WHERE $wpdb->posts.post_type = %s AND $wpdb->posts.post_status NOT IN ('auto-draft') GROUP BY $wpdb->posts.ID ORDER BY menu_order ASC ", $postType->name ), OBJECT ); // phpcs:enable WordPress.DB.DirectDatabaseQuery } wp_cache_set( $cacheKey, $allPosts ); } $templateTypesByPostId = $this->getTemplateTypesByPostId( $postType, $allPosts ); foreach ( $allPosts as $post ) { $postCategories = explode( ',', $post->post_category ?? '' ); foreach ( $postCategories as $postCategory ) { if ( $postType->name === 'elementor_library' ) { $categoryName = $templateTypesByPostId[$post->ID] ?? 'unknown'; } elseif ( empty( $postCategory ) || empty( $this->categoryList[$postCategory] ) ) { $categoryName = 'none'; } else { $categoryName = $this->categoryList[$postCategory]->name; } $categories[$categoryName][] = $post; ksort( $categories ); $categoryCount[$categoryName] = !empty( $categoryCount[$categoryName] ) ? $categoryCount[$categoryName] + 1 : 1; } $countPostType += 1; } } return [ 'count' => $countPostType, 'categories' => $categories, 'categoryCount' => $categoryCount, ]; } private function getTemplateTypesByPostId( $postType, $allPosts ): array { global $wpdb; if ( $postType->name !== 'elementor_library' || empty( $allPosts ) ) { return []; } $postIds = array_map( function ( $post ) { return $post->ID; }, $allPosts ); $postIdString = implode( ',', $postIds ); $cacheKey = 'aqb_templates_' . implode( '_', $postIds ); $templateTypes = wp_cache_get( $cacheKey ); if ( empty( $templateTypes ) ) { // phpcs:disable WordPress.DB.DirectDatabaseQuery $templateTypes = $wpdb->get_results( $wpdb->prepare( "SELECT $wpdb->postmeta.post_id, `meta_value` FROM $wpdb->postmeta WHERE post_id IN (%s) AND meta_key = '_elementor_template_type'", $postIdString ), OBJECT ); // phpcs:enable WordPress.DB.DirectDatabaseQuery wp_cache_set( $cacheKey, $templateTypes ); } $templateTypesByPostId = []; foreach ( $templateTypes as $templateType ) { $templateTypesByPostId[$templateType->post_id] = $templateType->meta_value; } return $templateTypesByPostId; } /** * Returns post-edit-link, and info if elementor is available * * @param \WP_Post_Type $postType * @param \WP_Post $post */ public function getPostTypeInfo( $postType, $post ): array { $noElementor = false; $noView = false; $link = admin_url() . 'post.php?post=' . $post->ID; switch ( $postType->name ) { case 'wpcf7': case 'wpcf7_contact_form': $link = admin_url() . 'admin.php?page=wpcf7&post=' . $post->ID; $noElementor = true; break; case 'attachment': $noElementor = true; break; case 'elementor_font': case 'elebee-global-css': case 'custom-css': case 'postgalleryslider': case 'acf-field-group': $noElementor = true; $noView = true; break; } if ( !defined( 'ELEMENTOR_VERSION' ) ) { $noElementor = true; } return [ 'link' => $link, 'noElementor' => $noElementor, 'noView' => $noView, ]; } /** * Rename a post * Typically called via admin-ajax */ public function renamePost(): void { if ( !current_user_can( 'view_admin_quickbar' ) ) { wp_die( 'no permission' ); } $postid = filter_input( INPUT_POST, 'postid', FILTER_VALIDATE_INT ); $title = esc_attr( filter_input( INPUT_POST, 'title' ) ); $result = [ 'ID' => $postid, 'post_title' => $title, ]; if ( empty( $postid ) || empty( $title ) ) { wp_die( json_encode( $result ) ); } wp_update_post( $result ); wp_die( json_encode( $result ) ); } /** * Fix wrong language in elementor */ public function fixElementorLanguage() { global $current_user; $userLocale = get_user_meta( get_current_user_id(), 'locale', true ); $current_user->locale = $userLocale; } public function enqueueStyles() { wp_enqueue_style( $this->pluginName, AdminQuickbar_URL . '/Admin/css/admin-quickbar-admin.min.css', [], $this->version, 'all' ); if ( !is_admin() ) { wp_enqueue_style( 'dashicons' ); } } public function enqueueScripts() { wp_enqueue_script( $this->pluginName, AdminQuickbar_URL . '/Admin/js/build.min.js', [ 'jquery' ], $this->version, true ); wp_localize_script( $this->pluginName, 'aqbLocalize', [ 'ajaxUrl' => admin_url( 'admin-ajax.php' ) ] ); } }