| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This class declares and handles the shortcodes. |
| 5 |
*/ |
| 6 |
// Exit if called directly |
| 7 |
if ( !defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
if ( !class_exists( 'Basepress_Shortcodes' ) ) { |
| 11 |
class Basepress_Shortcodes { |
| 12 |
/** |
| 13 |
* basepress_shortcodes constructor. |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
*/ |
| 17 |
public function __construct() { |
| 18 |
// register basepress shortcode |
| 19 |
add_shortcode( 'basepress', array($this, 'shortcodes_router') ); |
| 20 |
// register articles list shortcode |
| 21 |
add_shortcode( 'basepress-articles', array($this, 'articles_list_shortcodes') ); |
| 22 |
//Add shortcode to bottom of entry page if not present |
| 23 |
add_filter( 'the_content', array($this, 'add_products'), 10 ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Add shortcode to bottom of entry page if not present |
| 28 |
* |
| 29 |
* @since 2.7.0 |
| 30 |
* |
| 31 |
* @param $content |
| 32 |
* @return string |
| 33 |
*/ |
| 34 |
public function add_products( $content ) { |
| 35 |
global $basepress_utils, $post; |
| 36 |
if ( apply_filters( 'basepress_skip_auto_shortcode', false ) ) { |
| 37 |
return $content; |
| 38 |
} |
| 39 |
$entry_page = $basepress_utils->get_option( 'entry_page' ); |
| 40 |
$entry_page = apply_filters( 'basepress_entry_page', $entry_page ); |
| 41 |
if ( empty( $entry_page ) || empty( $post ) ) { |
| 42 |
return $content; |
| 43 |
} |
| 44 |
if ( isset( $post->ID ) && (int) $entry_page != (int) $post->ID ) { |
| 45 |
return $content; |
| 46 |
} |
| 47 |
$has_block = ( function_exists( 'has_block' ) ? has_block( 'basepress-kb/products-block' ) : false ); |
| 48 |
if ( !has_shortcode( $content, 'basepress' ) && !$has_block ) { |
| 49 |
$content = rtrim( $content, PHP_EOL ); |
| 50 |
$content .= PHP_EOL . "[basepress]"; |
| 51 |
} |
| 52 |
return $content; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Calls the shortcode function according to the short code attributes |
| 57 |
* If there are not attributes it will generate the product list |
| 58 |
* |
| 59 |
* @since 1.0.0 |
| 60 |
* |
| 61 |
* @param $atts |
| 62 |
* @param $content |
| 63 |
* @param $tag |
| 64 |
* @return string |
| 65 |
*/ |
| 66 |
public function shortcodes_router( $atts, $content, $tag ) { |
| 67 |
global $post, $basepress_shortcode_editor; |
| 68 |
if ( empty( $atts ) ) { |
| 69 |
return $this->show_products(); |
| 70 |
} else { |
| 71 |
return; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
public function articles_list_shortcodes( $atts ) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Generates the products page calling the products.php template in the theme |
| 81 |
* |
| 82 |
* @since 1.0.0 |
| 83 |
* |
| 84 |
* @updaed 2.1.0 |
| 85 |
* |
| 86 |
* @return string |
| 87 |
*/ |
| 88 |
public function show_products() { |
| 89 |
global $basepress_utils; |
| 90 |
$products = $basepress_utils->get_products(); |
| 91 |
if ( empty( $products ) ) { |
| 92 |
//We may not get any product if they are restricted to the public by the content restriction |
| 93 |
//We should not output the "no products message" in that case so we will run a new query not affected by Content restriction |
| 94 |
if ( $basepress_utils->get_option( 'activate_restrictions' ) ) { |
| 95 |
$terms_args = array( |
| 96 |
'taxonomy' => 'knowledgebase_cat', |
| 97 |
'hide_empty' => true, |
| 98 |
'parent' => 0, |
| 99 |
'fields' => 'ids', |
| 100 |
); |
| 101 |
$products_terms = get_terms( $terms_args ); |
| 102 |
if ( empty( $products_terms ) ) { |
| 103 |
return $basepress_utils->no_products_message(); |
| 104 |
} else { |
| 105 |
return ''; |
| 106 |
} |
| 107 |
} |
| 108 |
return $basepress_utils->no_products_message(); |
| 109 |
} |
| 110 |
$products_template = $basepress_utils->get_theme_file_path( 'products.php' ); |
| 111 |
ob_start(); |
| 112 |
if ( $products_template ) { |
| 113 |
include $products_template; |
| 114 |
} |
| 115 |
return ob_get_clean(); |
| 116 |
} |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
// End class |
| 121 |
new basepress_shortcodes(); |
| 122 |
} |