PluginProbe
Content Blocks (Custom Post Widget) / 3.4.3
Content Blocks (Custom Post Widget) v3.4.3
3.4.3 trunk 1.0 1.1 1.1.1 1.2 1.2.1 1.4 1.5 1.6 1.7 1.8 1.8.1 1.8.3 1.8.4 1.8.5 1.8.6 1.9 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 All 88 releases
custom-post-widget / shortcode.php

shortcode.php in Content Blocks (Custom Post Widget) 3.4.3, at shortcode.php

103 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) exit;
4
5 // Add the ability to display the content block in a reqular post using a shortcode
6 function custom_post_widget_shortcode ( $atts ) {
7 $params = shortcode_atts ( array (
8 'id' => '',
9 'slug' => '',
10 'class' => 'content_block',
11 'suppress_content_filters' => 'no',
12 'featured_image' => 'no',
13 'featured_image_size' => 'medium',
14 'title' => 'no',
15 'title_tag' => 'h3',
16 'markup' => 'div',
17 'template' => ''
18 ), $atts );
19
20 // Sanitize and escape attributes
21 $id = sanitize_text_field ( $params['id'] );
22 $slug = sanitize_text_field ( $params['slug'] );
23 $class = sanitize_text_field ( $params['class'] );
24 $suppress_content_filters = sanitize_text_field ( $params['suppress_content_filters'] );
25 $featured_image = sanitize_text_field ( $params['featured_image'] );
26 $featured_image_size = sanitize_text_field ( $params['featured_image_size'] );
27 $title = sanitize_text_field ( $params['title'] );
28 $title_tag = strtolower( sanitize_text_field( $params['title_tag'] ) );
29 $allowed_tags = array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'span', 'div' ); // Add or remove allowed HTML tags as needed
30 if ( ! in_array( $title_tag, $allowed_tags ) ) {
31 $title_tag = 'h3'; // Default to 'h3' if the specified tag is not allowed
32 }
33 $markup = strtolower( sanitize_text_field( $params['markup'] ) );
34 $allowed_markup_tags = array( 'div', 'section', 'article', 'aside', 'header', 'footer', 'main', 'span', 'p' );
35 if ( ! in_array( $markup, $allowed_markup_tags, true ) ) {
36 $markup = 'div';
37 }
38 $template = sanitize_text_field ( $params['template'] );
39
40 if ( $slug ) {
41 $block = get_page_by_path ( $slug, OBJECT, 'content_block' );
42 if ( $block ) {
43 $id = $block->ID;
44 }
45 }
46
47 $content = "";
48
49 // Attempt to load a template file
50 if ( $template != '' ) {
51
52 $located = locate_template( $template );
53
54 if ( ! empty( $located ) ) {
55 $template_in_theme_or_parent_theme = (
56 0 === strpos ( realpath ( $located ), realpath ( get_stylesheet_directory() ) ) ||
57 0 === strpos ( realpath ( $located ), realpath ( get_template_directory() ) )
58 );
59
60 if ( $template_in_theme_or_parent_theme ) {
61 require_once( $located );
62 }
63 }
64
65 }
66
67 if ( $id != "" ) {
68 $args = array (
69 'post__in' => array ( $id ),
70 'post_type' => 'content_block',
71 );
72
73 $content_post = get_posts ( $args );
74
75 foreach ( $content_post as $post ) :
76
77 if ( isset( $template_in_theme_or_parent_theme ) && $template_in_theme_or_parent_theme === true ) {
78 // Template-based content
79 $content .= call_user_func ( 'shortcode_template', $post );
80
81 } else {
82 // Standard format content
83 $content .= '<' . tag_escape ( $markup ) . ' class="'. esc_attr ( $class ) .'" id="custom_post_widget-' . esc_attr ( $id ) . '">';
84 if ( $title === 'yes' ) {
85 $content .= '<' . tag_escape ( $title_tag ) . '>' . esc_html ( $post->post_title ) . '</' . tag_escape ( $title_tag ) . '>';
86 }
87 if ( $featured_image === 'yes' ) {
88 $content .= get_the_post_thumbnail ( $post->ID, esc_attr ( $featured_image_size ) );
89 }
90 if ( $suppress_content_filters === 'no' ) {
91 $content .= apply_filters ( 'the_content', $post->post_content );
92 } else {
93 $content .= $post->post_content;
94 }
95 $content .= '</' . tag_escape ( $markup ) . '>';
96 }
97 endforeach;
98 }
99
100 return $content;
101 }
102 add_shortcode ( 'content_block', 'custom_post_widget_shortcode' );
103