Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.17 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Categories Search
  4. Plugin URL: https://invidgroup.com/
  5. Description: Categories Search
  6. Version: 0.1
  7. Author: INVID
  8. Author URL: https://invidgroup.com/
  9. License: GPL2
  10. */
  11. include ('functions.php');
  12.  
  13. define("CATEGORIES_SEARCH_PAGE_ID", "categories_searchId");
  14.  
  15. define("CATEGORIES_CONTENT_TYPE", "Content Type");
  16. define("CATEGORIES_CONTENT_TYPE_ID", "categories_content");
  17.  
  18. /*define("CATEGORIES_PRODUCTS", "Products");
  19. define("CATEGORIES_PRODUCTS_ID", "categories_product"); */
  20.  
  21. define("CATEGORIES_CATEGORIES", "Categories");
  22. define("CATEGORIES_CATEGORIES_ID", "categories_categories");
  23.  
  24. define("CATEGORIES_FILTER", "Filter");
  25. define("CATEGORIES_NOT_FOUND", "Sorry, there's no match in your search criteria. Try again.");
  26.  
  27. function init() {
  28.     add_option(CATEGORIES_CONTENT_TYPE_ID . '_' . ICL_LANGUAGE_CODE, '');
  29.     /*add_option(CATEGORIES_PRODUCTS_ID . '_' . ICL_LANGUAGE_CODE, ''); */
  30.     add_option(CATEGORIES_CATEGORIES_ID . '_' . ICL_LANGUAGE_CODE, '');
  31.     add_option(CATEGORIES_SEARCH_PAGE_ID . '_' . ICL_LANGUAGE_CODE, '');
  32. }
  33.  
  34. function cat_translate($type) {
  35.     if (ICL_LANGUAGE_CODE == 'es') {
  36.         if ($type == CATEGORIES_CONTENT_TYPE) return "Tipo de Contenido";
  37.         /* elseif ($type == CATEGORIES_PRODUCTS) return "Productos";  */
  38.         elseif ($type == CATEGORIES_CATEGORIES) return "Categorias";
  39.         elseif ($type == CATEGORIES_FILTER) return "Filtro";
  40.         elseif ($type == CATEGORIES_NOT_FOUND) return "Lo sentimos, no hay coincidencia en sus criterios de búsqueda. Inténtalo de nuevo.";
  41.     }
  42.     return $type;
  43. }
  44.  
  45. function categories_add_admin_page() {
  46.     add_menu_page(
  47.         __( 'Categories Search', 'categories-plugin' ),
  48.         __( 'Categories Search', 'categories-plugin' ),
  49.         'manage_options',
  50.         'categories-plugin',
  51.         'categories_admin_page',
  52.         '',
  53.         1
  54.     );
  55. }
  56.  
  57. function categories_admin_page() { 
  58.  
  59.     $pageContent = "";
  60.     categories_admin_page_save();
  61.     $pageContent .= "<form method=\"post\">";
  62.    
  63.     $pageContent .= categories_admin_page_select_site();
  64.    
  65.     $pageContent .= "<div style=\"display: flex;border-top: 1px solid grey;margin-right:20px;\">\n";
  66.     $pageContent .= categories_admin_page_select_items(CATEGORIES_CONTENT_TYPE);
  67.     $pageContent .= categories_admin_page_select_items(CATEGORIES_CATEGORIES, true);
  68.     /*$pageContent .= categories_admin_page_select_items(CATEGORIES_PRODUCTS); */
  69.     $pageContent .= "</div>";
  70.    
  71.     $pageContent .= "<input type=\"hidden\" name=\"category_update\" value=\"update\">";
  72.     $pageContent .= "<button style=\"float: right;margin: 10px;\" type=\"submit\" class=\"button button-primary\">" . __( 'Save All', 'categories-plugin' ) . "</button>";
  73.     $pageContent .= "</form>";
  74.    
  75.     print $pageContent;
  76.    
  77. }
  78.  
  79. function categories_admin_page_save() {
  80.     if ($_POST['category_update'] == 'update') {
  81.         $contentType = array();
  82.         /*$products = array();*/
  83.         $categories = array();
  84.        
  85.         foreach ($_POST as $opt=>$val) {
  86.             if (strpos($opt, CATEGORIES_CONTENT_TYPE_ID) !== FALSE) {
  87.                 $id = str_replace(CATEGORIES_CONTENT_TYPE_ID . '_', '', $opt);
  88.                 $contentType[$id] = true;
  89.             } elseif (strpos($opt, CATEGORIES_PRODUCTS_ID) !== FALSE) {
  90.                 $id = str_replace(CATEGORIES_PRODUCTS_ID . '_', '', $opt);
  91.                 $products[$id] = true;
  92.             } elseif (strpos($opt, CATEGORIES_CATEGORIES_ID) !== FALSE) {
  93.                 $id = str_replace(CATEGORIES_CATEGORIES_ID . '_', '', $opt);
  94.                 $categories[$id] = true;
  95.             }
  96.         }
  97.        
  98.         update_option(CATEGORIES_CONTENT_TYPE_ID . '_' . ICL_LANGUAGE_CODE, $contentType);
  99.         /*product update_option(CATEGORIES_PRODUCTS_ID . '_' . ICL_LANGUAGE_CODE, $products); */
  100.         update_option(CATEGORIES_CATEGORIES_ID . '_' . ICL_LANGUAGE_CODE, $categories);
  101.         update_option(CATEGORIES_SEARCH_PAGE_ID . '_' . ICL_LANGUAGE_CODE, $_POST['page_id']);
  102.        
  103.     }
  104. }
  105.  
  106. function categories_admin_page_select_items($type, $borders = false) {
  107.    
  108.     if ($type == CATEGORIES_CONTENT_TYPE) $id = CATEGORIES_CONTENT_TYPE_ID;
  109.     /* else if ($type == CATEGORIES_PRODUCTS) $id = CATEGORIES_PRODUCTS_ID; */
  110.     else $id = CATEGORIES_CATEGORIES_ID;
  111.    
  112.     $content = "";
  113.     $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;' : '') . "\">";
  114.     $content .= "<h1 style=\"text-align: center;\">" . cat_translate($type) .":</h1>";
  115.    
  116.     $values = get_option($id . '_' . ICL_LANGUAGE_CODE);
  117.     foreach(categories_loadAll() as $category) {
  118.         $content .= "<span style=\"display: block;\"><input name=\"${id}_${category['id']}\" type=\"checkbox\" " . ($values[$category['id']] ? 'checked=checked' : '') . "/> <b>${category['name']}</b></span>";
  119.     }
  120.  
  121.     $content .= "</div>";
  122.     return $content;   
  123. }
  124.  
  125. function categories_admin_page_select_site() {
  126.     $postID = get_option(CATEGORIES_SEARCH_PAGE_ID . '_' . ICL_LANGUAGE_CODE);
  127.     $pages = "<div style=\"margin: 10px;\">";
  128.     $pages .= "<b>Resources Page</b>: " . wp_dropdown_pages(array('echo' => 0, 'selected' => $postID));
  129.     $pages .= "</div>";
  130.     return $pages;
  131. }
  132.  
  133. add_action( 'admin_menu', 'categories_add_admin_page' );
  134.  
  135. add_action('template_redirect', 'categories_get_post_page_id');
  136. function categories_get_post_page_id() {
  137.     global $categoriesCurrentPage;
  138.     $categoriesCurrentPage = get_the_ID();
  139. }
  140.  
  141. add_action('get_footer', 'categories_add_search_criteria');
  142. function categories_add_search_criteria() {
  143.     global $categoriesCurrentPage;
  144.     if ($categoriesCurrentPage == get_option(CATEGORIES_SEARCH_PAGE_ID . '_' . ICL_LANGUAGE_CODE)) {
  145.        
  146.         $postContent = ob_get_contents();
  147.         ob_get_clean();
  148.         ob_start();
  149.        
  150.         $search = "";        
  151.         $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>";
  152.         $search .= "<div id=\"search-criteria\" style=\"float: left; width: 25%;\">";
  153.         $search .= categories_add_search_criteria_category(CATEGORIES_CONTENT_TYPE, CATEGORIES_CONTENT_TYPE_ID);
  154.         $search .= "<hr />";
  155.         $search .= categories_add_search_criteria_category(CATEGORIES_CATEGORIES, CATEGORIES_CATEGORIES_ID);
  156.         /*$search .= "<hr />";*/
  157.          /*$search .= categories_add_search_criteria_category(CATEGORIES_PRODUCTS, CATEGORIES_PRODUCTS_ID, true); */
  158.         $search .= "</div>";
  159.         $search .= "<div class=\"no-results\"><span><img src=\"" . plugins_url() . "/categories-search/GOAT.svg\" /></span><span>" . cat_translate(CATEGORIES_NOT_FOUND) ."</span></div>";
  160.         $postContent = str_replace("<div class=\"fusion-posts-container", $search . "<div style=\"float: right; width: 75%;\" class=\"fusion-posts-container", $postContent);
  161.         print $postContent;
  162.         categories_hook_js();
  163.         categories_search_criteria_css();
  164.     }
  165. }
  166.  
  167. function categories_add_search_criteria_category($name, $type, $multi = false) {
  168.     $data = get_option($type . '_' . ICL_LANGUAGE_CODE);
  169.     $page = "";
  170.     $page .= "<div class=\"criteria ${type}\">";
  171.     $page .= "<b>" . cat_translate($name) ."</b> ";
  172.     foreach($data as $cat=>$_) {
  173.         $checkmark = $multi ? "<span class=\"checkmark\"><div class=\"border\"></div><div class=\"stem\"></div><div class=\"kick\"></div></span>" : "";
  174.         $page .= "<span class=\"cat-${cat} " . ($multi ? 'select-multiple' : '') . "\" catId=\"${cat}\" onclick=\"javascript:hideExcept(this, '${cat}');\">" . $checkmark . getCategoryNameByID($cat) . "</span>";
  175.         $page .= ' ';
  176.         // $page .= ' | ';
  177.     }
  178.     // $page = substr($page, 0, -2);
  179.     $page .= "</div>";
  180.     return $page;
  181. }
  182.  
  183. add_action( 'fusion_blog_shortcode_after_loop', 'categories_add_ids_to_blog_posts' );
  184. function categories_add_ids_to_blog_posts() {
  185.     global $categoriesCurrentPage;
  186.     if ($categoriesCurrentPage == get_option(CATEGORIES_SEARCH_PAGE_ID . '_' . ICL_LANGUAGE_CODE)) {
  187.         global $post;
  188.         $postCategories = wp_get_post_categories($post->ID);
  189.         print "<div class=\"categories_post\" style=\"display: none;\">" . implode(",", $postCategories) ."</div>";
  190.     }  
  191. }
  192.  
  193. function categories_hook_js() {
  194.     global $categoriesCurrentPage;
  195.     if ($categoriesCurrentPage == get_option(CATEGORIES_SEARCH_PAGE_ID . '_' . ICL_LANGUAGE_CODE)) {
  196.         ?>
  197.         <script type="text/javascript" >
  198.        
  199.         function hideExcept(what, key) {
  200.             if (!jQuery(what).hasClass('select-multiple')) {
  201.                 jQuery(what).parent().children().each(function() {jQuery(this).removeClass("cat-selected");});
  202.                 jQuery(what).addClass("cat-selected");
  203.             } else {
  204.                 if (jQuery(what).hasClass('cat-selected')) jQuery(what).removeClass("cat-selected");
  205.                 else jQuery(what).addClass("cat-selected");
  206.             }
  207.            
  208.             if (jQuery(what).parent().hasClass("categories_content")) {
  209.                 jQuery("#search-criteria > .categories_categories").children().each(function() {jQuery(this).removeClass("cat-selected");});
  210.                 jQuery("#search-criteria > .categories_product").children().each(function() {jQuery(this).removeClass("cat-selected");});
  211.             } else if (jQuery(what).parent().hasClass("categories_categories")) {
  212.                 jQuery("#search-criteria > .categories_product").children().each(function() {jQuery(this).removeClass("cat-selected");});
  213.             }
  214.            
  215.             search();
  216.         }
  217.        
  218.         function search() {
  219.             jQuery(".no-results").hide();
  220.             var toShow = [];
  221.             var searchCTP = jQuery(".categories_content").children(".cat-selected").attr("catId");
  222.             var searchCAT = jQuery(".categories_categories").children(".cat-selected").attr("catId");
  223.             var searchPRD = [];
  224.             jQuery(".categories_product").children(".cat-selected").each(function() {searchPRD.push(jQuery(this).attr("catId"));})
  225.             jQuery(".categories_post").each(function() {
  226.                 var hide = false;
  227.                 var categories = jQuery(this).text().split(",");
  228.                 if (searchCTP != null && !categories.includes(searchCTP)) {
  229.                     hide = true;
  230.                 }
  231.                 if (!hide && searchCAT != null && !categories.includes(searchCAT)) {
  232.                     hide = true;
  233.                 }
  234.                 if (!hide && searchPRD.length > 0) {
  235.                     var cnt = 0;
  236.                     for (var i=0; i<searchPRD.length; i++) {
  237.                         if (categories.includes(searchPRD[i])) {
  238.                             cnt++;
  239.                             break;
  240.                         }
  241.                     }
  242.                     if (cnt == 0) hide = true;     
  243.                 }
  244.                
  245.                 var parent = jQuery(this).parent().parent();
  246.                 jQuery(parent).fadeOut();
  247.                 if (!hide) {
  248.                     toShow.push(parent);
  249.                 }
  250.             });
  251.             setTimeout(function(){
  252.                 jQuery(".fusion-blog-layout-grid").isotope({transitionDuration: 0});
  253.                 for (var i=0; i<toShow.length; i++) {jQuery(toShow[i]).show();}
  254.                 if (toShow.length == 0) jQuery(".no-results").show();
  255.                 jQuery(".fusion-blog-layout-grid").isotope({transitionDuration: 0});
  256.             }, 500);
  257.         }
  258.        
  259.         jQuery(document).ready(function() {
  260.             if (typeof(Array.prototype.includes)=="undefined") {
  261.                 Array.prototype.includes = function(x) {
  262.                     if (this.length == 0) return false;
  263.                     for (var i=0; i<this.length; i++) {
  264.                         if (this[i] == x) return true;
  265.                     }
  266.                     return false;
  267.                 }
  268.             }
  269.            
  270.             var maxHeight = 0;
  271.             jQuery(".categories_post").each(function() {
  272.                 if (jQuery(this).parent().height() > maxHeight)
  273.                     maxHeight = jQuery(this).parent().height();
  274.             });
  275.             jQuery(".categories_post").each(function() {
  276.                 jQuery(this).parent().height(maxHeight);
  277.             });
  278.             setTimeout(function(){ jQuery(".fusion-blog-layout-grid").isotope(); }, 500);
  279.            
  280.             jQuery(".hamburger").click(function() {
  281.                 jQuery(this).toggleClass("is-active");
  282.                 var control = jQuery(this).attr("aria-controls");
  283.                 jQuery("#" + control).toggle();
  284.             });
  285.         });
  286.        
  287.         jQuery(window).on('resize', function(event){
  288.             if (jQuery(window).width() > <? echo Avada()->settings->get( 'content_break_point' ); ?>) {
  289.                 jQuery("#search-criteria").show();
  290.             } else {
  291.                 jQuery(".hamburger").each(function() {jQuery(this).removeClass("is-active");});
  292.                 jQuery("#search-criteria").hide();
  293.             }
  294.         });
  295.         </script>
  296.         <?
  297.     }
  298. }
  299.  
  300. function categories_search_criteria_css() {
  301.     global $categoriesCurrentPage;
  302.     if ($categoriesCurrentPage == get_option(CATEGORIES_SEARCH_PAGE_ID . '_' . ICL_LANGUAGE_CODE)) {
  303.         wp_enqueue_style( 'style', plugins_url() . "/categories-search/hamburgers.css" );
  304.         ?>
  305.         <style>
  306.             #search-criteria {
  307.                 margin-top: 10px;
  308.                 margin-bottom: 20px;
  309.                 /*background: white;*/
  310.                 padding: 10px;
  311.             }
  312.            
  313.             #search-criteria .criteria {
  314.                 margin-top: 5px;
  315.             }
  316.            
  317.             #search-criteria .criteria span {
  318.                 cursor: pointer !important;
  319.                 padding: 5px;
  320.                 padding-left: 15px;
  321.                 border-radius: 5px;
  322.                 /*background: lightgrey;*/
  323.                 display: block;
  324.                 color: #6a8bc6;
  325.             }
  326.            
  327.             #search-criteria .criteria span:hover {
  328.                 background-color: rgba(22, 145, 191, 0.1);
  329.             }
  330.            
  331.             .select-multiple {
  332.                 margin-bottom: 5px;
  333.                
  334.             }
  335.            
  336.             .cat-selected {
  337.                 background-color: rgba(22, 145, 191, 0.5) !important;
  338.             }
  339.            
  340.             .checkmark {
  341.                 display:inline-block !important;
  342.                 width: 22px;
  343.                 height: 17px;
  344.                 margin-right: 5px;
  345.                 -ms-transform: rotate(45deg); /* IE 9 */
  346.                 -webkit-transform: rotate(45deg); /* Chrome, Safari, Opera */
  347.                 transform: rotate(45deg);
  348.             }
  349.  
  350.             .checkmark .on {
  351.               background: green;
  352.               border: 1px solid green !important;
  353.             }
  354.  
  355.             .checkmark .border {
  356.                 position: absolute;
  357.                 width:22px;
  358.                 height:22px;
  359.                 border: 1px solid #6a8bc6;
  360.                 border-radius:5px;
  361.                 left:0;
  362.                 top:0;
  363.                 -ms-transform: rotate(45deg); /* IE 9 */
  364.                 -webkit-transform: rotate(45deg); /* Chrome, Safari, Opera */
  365.                 transform: rotate(45deg);
  366.             }
  367.  
  368.             .checkmark .stem {
  369.                 position: absolute;
  370.                 width:3px;
  371.                 height:9px;
  372.                 /*background-color:#fff;*/
  373.                 left:11px;
  374.                 top:6px;
  375.             }
  376.            
  377.             .select-multiple.cat-selected .checkmark .stem, .select-multiple.cat-selected .checkmark .kick {
  378.                 background-color:#fff;
  379.             }
  380.  
  381.             .checkmark .kick {
  382.                 position: absolute;
  383.                 width:3px;
  384.                 height:3px;
  385.                 /*background-color:#fff;*/
  386.                 left:8px;
  387.                 top:12px;
  388.             }
  389.            
  390.             #search-filter {
  391.                 display: none;
  392.             }
  393.            
  394.             .fusion-post-content-wrapper div h2 a {
  395.                 overflow: hidden;
  396.                 display: -webkit-box;
  397.                 -webkit-line-clamp: 4;
  398.                 -webkit-box-orient: vertical;
  399.             }
  400.            
  401.             .no-results {
  402.                 display: none;
  403.                 float: right;
  404.                 width: 75%;
  405.             }
  406.            
  407.             .no-results span {
  408.                 display: block;
  409.                 text-align: center;
  410.                 font-weight: bold;
  411.             }
  412.            
  413.             @media only screen and (max-width: <? echo Avada()->settings->get( 'content_break_point' ); ?>px) {
  414.                 #search-criteria {
  415.                     width: 100% !important;
  416.                     display: none;
  417.                 }
  418.                
  419.                 .no-results {
  420.                     width: 100%;
  421.                 }
  422.                
  423.                 #search-filter {
  424.                     display: block;
  425.                     width: 100%;
  426.                 }
  427.                 .fusion-posts-container {
  428.                     width: 100% !important;
  429.                 }  
  430.  
  431.                 .hamburger-label {
  432.                     font-weight: 600;
  433.                     display: inline-block;
  434.                     margin-left: 5px;
  435.                     vertical-align: middle;
  436.                     text-transform: uppercase;
  437.                     margin: -15px 0px 0px 15px;
  438.                     font-size: 24px;
  439.                     color: black;
  440.                 }
  441.             }
  442.            
  443.             #wrapper {
  444.                 margin-top: -10px;
  445.             }
  446.            
  447.             #wrapper header {
  448.                 margin-top: 10px;
  449.             }
  450.            
  451.            
  452.         </style>
  453.         <?
  454.     }
  455. }
  456.  
  457. // if ($_SERVER['REMOTE_ADDR'] == '109.241.249.5') {
  458.     // $debug_tags = array();
  459.     // add_action( 'all', function ( $tag ) {
  460.         // global $debug_tags;
  461.         // if ( in_array( $tag, $debug_tags ) ) {
  462.             // return;
  463.         // }
  464.         // echo "<pre>" . $tag . "</pre>";
  465.         // $debug_tags[] = $tag;
  466.     // } );
  467. // }
  468.  
  469. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement