Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Categories Search
- Plugin URL: https://invidgroup.com/
- Description: Categories Search
- Version: 0.1
- Author: INVID
- Author URL: https://invidgroup.com/
- License: GPL2
- */
- include ('functions.php');
- define("CATEGORIES_SEARCH_PAGE_ID", "categories_searchId");
- define("CATEGORIES_CONTENT_TYPE", "Content Type");
- define("CATEGORIES_CONTENT_TYPE_ID", "categories_content");
- /*define("CATEGORIES_PRODUCTS", "Products");
- define("CATEGORIES_PRODUCTS_ID", "categories_product"); */
- define("CATEGORIES_CATEGORIES", "Categories");
- define("CATEGORIES_CATEGORIES_ID", "categories_categories");
- define("CATEGORIES_FILTER", "Filter");
- define("CATEGORIES_NOT_FOUND", "Sorry, there's no match in your search criteria. Try again.");
- function init() {
- add_option(CATEGORIES_CONTENT_TYPE_ID . '_' . ICL_LANGUAGE_CODE, '');
- /*add_option(CATEGORIES_PRODUCTS_ID . '_' . ICL_LANGUAGE_CODE, ''); */
- add_option(CATEGORIES_CATEGORIES_ID . '_' . ICL_LANGUAGE_CODE, '');
- add_option(CATEGORIES_SEARCH_PAGE_ID . '_' . ICL_LANGUAGE_CODE, '');
- }
- function cat_translate($type) {
- if (ICL_LANGUAGE_CODE == 'es') {
- if ($type == CATEGORIES_CONTENT_TYPE) return "Tipo de Contenido";
- /* elseif ($type == CATEGORIES_PRODUCTS) return "Productos"; */
- elseif ($type == CATEGORIES_CATEGORIES) return "Categorias";
- elseif ($type == CATEGORIES_FILTER) return "Filtro";
- elseif ($type == CATEGORIES_NOT_FOUND) return "Lo sentimos, no hay coincidencia en sus criterios de búsqueda. Inténtalo de nuevo.";
- }
- return $type;
- }
- function categories_add_admin_page() {
- add_menu_page(
- __( 'Categories Search', 'categories-plugin' ),
- __( 'Categories Search', 'categories-plugin' ),
- 'manage_options',
- 'categories-plugin',
- 'categories_admin_page',
- '',
- 1
- );
- }
- function categories_admin_page() {
- $pageContent = "";
- categories_admin_page_save();
- $pageContent .= "<form method=\"post\">";
- $pageContent .= categories_admin_page_select_site();
- $pageContent .= "<div style=\"display: flex;border-top: 1px solid grey;margin-right:20px;\">\n";
- $pageContent .= categories_admin_page_select_items(CATEGORIES_CONTENT_TYPE);
- $pageContent .= categories_admin_page_select_items(CATEGORIES_CATEGORIES, true);
- /*$pageContent .= categories_admin_page_select_items(CATEGORIES_PRODUCTS); */
- $pageContent .= "</div>";
- $pageContent .= "<input type=\"hidden\" name=\"category_update\" value=\"update\">";
- $pageContent .= "<button style=\"float: right;margin: 10px;\" type=\"submit\" class=\"button button-primary\">" . __( 'Save All', 'categories-plugin' ) . "</button>";
- $pageContent .= "</form>";
- print $pageContent;
- }
- function categories_admin_page_save() {
- if ($_POST['category_update'] == 'update') {
- $contentType = array();
- /*$products = array();*/
- $categories = array();
- foreach ($_POST as $opt=>$val) {
- if (strpos($opt, CATEGORIES_CONTENT_TYPE_ID) !== FALSE) {
- $id = str_replace(CATEGORIES_CONTENT_TYPE_ID . '_', '', $opt);
- $contentType[$id] = true;
- } elseif (strpos($opt, CATEGORIES_PRODUCTS_ID) !== FALSE) {
- $id = str_replace(CATEGORIES_PRODUCTS_ID . '_', '', $opt);
- $products[$id] = true;
- } elseif (strpos($opt, CATEGORIES_CATEGORIES_ID) !== FALSE) {
- $id = str_replace(CATEGORIES_CATEGORIES_ID . '_', '', $opt);
- $categories[$id] = true;
- }
- }
- update_option(CATEGORIES_CONTENT_TYPE_ID . '_' . ICL_LANGUAGE_CODE, $contentType);
- /*product update_option(CATEGORIES_PRODUCTS_ID . '_' . ICL_LANGUAGE_CODE, $products); */
- update_option(CATEGORIES_CATEGORIES_ID . '_' . ICL_LANGUAGE_CODE, $categories);
- update_option(CATEGORIES_SEARCH_PAGE_ID . '_' . ICL_LANGUAGE_CODE, $_POST['page_id']);
- }
- }
- function categories_admin_page_select_items($type, $borders = false) {
- if ($type == CATEGORIES_CONTENT_TYPE) $id = CATEGORIES_CONTENT_TYPE_ID;
- /* else if ($type == CATEGORIES_PRODUCTS) $id = CATEGORIES_PRODUCTS_ID; */
- else $id = CATEGORIES_CATEGORIES_ID;
- $content = "";
- $content .= "<div id=\"" . $id . "\" style=\"padding: 20px;flex: 1; border-bottom: 1px solid grey;" . ($borders ? 'border-left:1px solid grey; border-right: 1px solid grey;' : '') . "\">";
- $content .= "<h1 style=\"text-align: center;\">" . cat_translate($type) .":</h1>";
- $values = get_option($id . '_' . ICL_LANGUAGE_CODE);
- foreach(categories_loadAll() as $category) {
- $content .= "<span style=\"display: block;\"><input name=\"${id}_${category['id']}\" type=\"checkbox\" " . ($values[$category['id']] ? 'checked=checked' : '') . "/> <b>${category['name']}</b></span>";
- }
- $content .= "</div>";
- return $content;
- }
- function categories_admin_page_select_site() {
- $postID = get_option(CATEGORIES_SEARCH_PAGE_ID . '_' . ICL_LANGUAGE_CODE);
- $pages = "<div style=\"margin: 10px;\">";
- $pages .= "<b>Resources Page</b>: " . wp_dropdown_pages(array('echo' => 0, 'selected' => $postID));
- $pages .= "</div>";
- return $pages;
- }
- add_action( 'admin_menu', 'categories_add_admin_page' );
- add_action('template_redirect', 'categories_get_post_page_id');
- function categories_get_post_page_id() {
- global $categoriesCurrentPage;
- $categoriesCurrentPage = get_the_ID();
- }
- add_action('get_footer', 'categories_add_search_criteria');
- function categories_add_search_criteria() {
- global $categoriesCurrentPage;
- if ($categoriesCurrentPage == get_option(CATEGORIES_SEARCH_PAGE_ID . '_' . ICL_LANGUAGE_CODE)) {
- $postContent = ob_get_contents();
- ob_get_clean();
- ob_start();
- $search = "";
- $search .= "<div id=\"search-filter\" class=\"hamburger hamburger--elastic\" tabindex=\"0\" aria-label=\"Filter\" role=\"button\" aria-controls=\"search-criteria\"><div class=\"hamburger-box\"><div class=\"hamburger-inner\"></div></div><div class=\"hamburger-label\">" . cat_translate(CATEGORIES_FILTER) . "</div></div>";
- $search .= "<div id=\"search-criteria\" style=\"float: left; width: 25%;\">";
- $search .= categories_add_search_criteria_category(CATEGORIES_CONTENT_TYPE, CATEGORIES_CONTENT_TYPE_ID);
- $search .= "<hr />";
- $search .= categories_add_search_criteria_category(CATEGORIES_CATEGORIES, CATEGORIES_CATEGORIES_ID);
- /*$search .= "<hr />";*/
- /*$search .= categories_add_search_criteria_category(CATEGORIES_PRODUCTS, CATEGORIES_PRODUCTS_ID, true); */
- $search .= "</div>";
- $search .= "<div class=\"no-results\"><span><img src=\"" . plugins_url() . "/categories-search/GOAT.svg\" /></span><span>" . cat_translate(CATEGORIES_NOT_FOUND) ."</span></div>";
- $postContent = str_replace("<div class=\"fusion-posts-container", $search . "<div style=\"float: right; width: 75%;\" class=\"fusion-posts-container", $postContent);
- print $postContent;
- categories_hook_js();
- categories_search_criteria_css();
- }
- }
- function categories_add_search_criteria_category($name, $type, $multi = false) {
- $data = get_option($type . '_' . ICL_LANGUAGE_CODE);
- $page = "";
- $page .= "<div class=\"criteria ${type}\">";
- $page .= "<b>" . cat_translate($name) ."</b> ";
- foreach($data as $cat=>$_) {
- $checkmark = $multi ? "<span class=\"checkmark\"><div class=\"border\"></div><div class=\"stem\"></div><div class=\"kick\"></div></span>" : "";
- $page .= "<span class=\"cat-${cat} " . ($multi ? 'select-multiple' : '') . "\" catId=\"${cat}\" onclick=\"javascript:hideExcept(this, '${cat}');\">" . $checkmark . getCategoryNameByID($cat) . "</span>";
- $page .= ' ';
- // $page .= ' | ';
- }
- // $page = substr($page, 0, -2);
- $page .= "</div>";
- return $page;
- }
- add_action( 'fusion_blog_shortcode_after_loop', 'categories_add_ids_to_blog_posts' );
- function categories_add_ids_to_blog_posts() {
- global $categoriesCurrentPage;
- if ($categoriesCurrentPage == get_option(CATEGORIES_SEARCH_PAGE_ID . '_' . ICL_LANGUAGE_CODE)) {
- global $post;
- $postCategories = wp_get_post_categories($post->ID);
- print "<div class=\"categories_post\" style=\"display: none;\">" . implode(",", $postCategories) ."</div>";
- }
- }
- function categories_hook_js() {
- global $categoriesCurrentPage;
- if ($categoriesCurrentPage == get_option(CATEGORIES_SEARCH_PAGE_ID . '_' . ICL_LANGUAGE_CODE)) {
- ?>
- <script type="text/javascript" >
- function hideExcept(what, key) {
- if (!jQuery(what).hasClass('select-multiple')) {
- jQuery(what).parent().children().each(function() {jQuery(this).removeClass("cat-selected");});
- jQuery(what).addClass("cat-selected");
- } else {
- if (jQuery(what).hasClass('cat-selected')) jQuery(what).removeClass("cat-selected");
- else jQuery(what).addClass("cat-selected");
- }
- if (jQuery(what).parent().hasClass("categories_content")) {
- jQuery("#search-criteria > .categories_categories").children().each(function() {jQuery(this).removeClass("cat-selected");});
- jQuery("#search-criteria > .categories_product").children().each(function() {jQuery(this).removeClass("cat-selected");});
- } else if (jQuery(what).parent().hasClass("categories_categories")) {
- jQuery("#search-criteria > .categories_product").children().each(function() {jQuery(this).removeClass("cat-selected");});
- }
- search();
- }
- function search() {
- jQuery(".no-results").hide();
- var toShow = [];
- var searchCTP = jQuery(".categories_content").children(".cat-selected").attr("catId");
- var searchCAT = jQuery(".categories_categories").children(".cat-selected").attr("catId");
- var searchPRD = [];
- jQuery(".categories_product").children(".cat-selected").each(function() {searchPRD.push(jQuery(this).attr("catId"));})
- jQuery(".categories_post").each(function() {
- var hide = false;
- var categories = jQuery(this).text().split(",");
- if (searchCTP != null && !categories.includes(searchCTP)) {
- hide = true;
- }
- if (!hide && searchCAT != null && !categories.includes(searchCAT)) {
- hide = true;
- }
- if (!hide && searchPRD.length > 0) {
- var cnt = 0;
- for (var i=0; i<searchPRD.length; i++) {
- if (categories.includes(searchPRD[i])) {
- cnt++;
- break;
- }
- }
- if (cnt == 0) hide = true;
- }
- var parent = jQuery(this).parent().parent();
- jQuery(parent).fadeOut();
- if (!hide) {
- toShow.push(parent);
- }
- });
- setTimeout(function(){
- jQuery(".fusion-blog-layout-grid").isotope({transitionDuration: 0});
- for (var i=0; i<toShow.length; i++) {jQuery(toShow[i]).show();}
- if (toShow.length == 0) jQuery(".no-results").show();
- jQuery(".fusion-blog-layout-grid").isotope({transitionDuration: 0});
- }, 500);
- }
- jQuery(document).ready(function() {
- if (typeof(Array.prototype.includes)=="undefined") {
- Array.prototype.includes = function(x) {
- if (this.length == 0) return false;
- for (var i=0; i<this.length; i++) {
- if (this[i] == x) return true;
- }
- return false;
- }
- }
- var maxHeight = 0;
- jQuery(".categories_post").each(function() {
- if (jQuery(this).parent().height() > maxHeight)
- maxHeight = jQuery(this).parent().height();
- });
- jQuery(".categories_post").each(function() {
- jQuery(this).parent().height(maxHeight);
- });
- setTimeout(function(){ jQuery(".fusion-blog-layout-grid").isotope(); }, 500);
- jQuery(".hamburger").click(function() {
- jQuery(this).toggleClass("is-active");
- var control = jQuery(this).attr("aria-controls");
- jQuery("#" + control).toggle();
- });
- });
- jQuery(window).on('resize', function(event){
- if (jQuery(window).width() > <? echo Avada()->settings->get( 'content_break_point' ); ?>) {
- jQuery("#search-criteria").show();
- } else {
- jQuery(".hamburger").each(function() {jQuery(this).removeClass("is-active");});
- jQuery("#search-criteria").hide();
- }
- });
- </script>
- <?
- }
- }
- function categories_search_criteria_css() {
- global $categoriesCurrentPage;
- if ($categoriesCurrentPage == get_option(CATEGORIES_SEARCH_PAGE_ID . '_' . ICL_LANGUAGE_CODE)) {
- wp_enqueue_style( 'style', plugins_url() . "/categories-search/hamburgers.css" );
- ?>
- <style>
- #search-criteria {
- margin-top: 10px;
- margin-bottom: 20px;
- /*background: white;*/
- padding: 10px;
- }
- #search-criteria .criteria {
- margin-top: 5px;
- }
- #search-criteria .criteria span {
- cursor: pointer !important;
- padding: 5px;
- padding-left: 15px;
- border-radius: 5px;
- /*background: lightgrey;*/
- display: block;
- color: #6a8bc6;
- }
- #search-criteria .criteria span:hover {
- background-color: rgba(22, 145, 191, 0.1);
- }
- .select-multiple {
- margin-bottom: 5px;
- }
- .cat-selected {
- background-color: rgba(22, 145, 191, 0.5) !important;
- }
- .checkmark {
- display:inline-block !important;
- width: 22px;
- height: 17px;
- margin-right: 5px;
- -ms-transform: rotate(45deg); /* IE 9 */
- -webkit-transform: rotate(45deg); /* Chrome, Safari, Opera */
- transform: rotate(45deg);
- }
- .checkmark .on {
- background: green;
- border: 1px solid green !important;
- }
- .checkmark .border {
- position: absolute;
- width:22px;
- height:22px;
- border: 1px solid #6a8bc6;
- border-radius:5px;
- left:0;
- top:0;
- -ms-transform: rotate(45deg); /* IE 9 */
- -webkit-transform: rotate(45deg); /* Chrome, Safari, Opera */
- transform: rotate(45deg);
- }
- .checkmark .stem {
- position: absolute;
- width:3px;
- height:9px;
- /*background-color:#fff;*/
- left:11px;
- top:6px;
- }
- .select-multiple.cat-selected .checkmark .stem, .select-multiple.cat-selected .checkmark .kick {
- background-color:#fff;
- }
- .checkmark .kick {
- position: absolute;
- width:3px;
- height:3px;
- /*background-color:#fff;*/
- left:8px;
- top:12px;
- }
- #search-filter {
- display: none;
- }
- .fusion-post-content-wrapper div h2 a {
- overflow: hidden;
- display: -webkit-box;
- -webkit-line-clamp: 4;
- -webkit-box-orient: vertical;
- }
- .no-results {
- display: none;
- float: right;
- width: 75%;
- }
- .no-results span {
- display: block;
- text-align: center;
- font-weight: bold;
- }
- @media only screen and (max-width: <? echo Avada()->settings->get( 'content_break_point' ); ?>px) {
- #search-criteria {
- width: 100% !important;
- display: none;
- }
- .no-results {
- width: 100%;
- }
- #search-filter {
- display: block;
- width: 100%;
- }
- .fusion-posts-container {
- width: 100% !important;
- }
- .hamburger-label {
- font-weight: 600;
- display: inline-block;
- margin-left: 5px;
- vertical-align: middle;
- text-transform: uppercase;
- margin: -15px 0px 0px 15px;
- font-size: 24px;
- color: black;
- }
- }
- #wrapper {
- margin-top: -10px;
- }
- #wrapper header {
- margin-top: 10px;
- }
- </style>
- <?
- }
- }
- // if ($_SERVER['REMOTE_ADDR'] == '109.241.249.5') {
- // $debug_tags = array();
- // add_action( 'all', function ( $tag ) {
- // global $debug_tags;
- // if ( in_array( $tag, $debug_tags ) ) {
- // return;
- // }
- // echo "<pre>" . $tag . "</pre>";
- // $debug_tags[] = $tag;
- // } );
- // }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement