| 1 |
<?php |
| 2 |
/** |
| 3 |
* The public-facing functionality of the module. |
| 4 |
* |
| 5 |
* @link https://codesupply.co |
| 6 |
* @since 1.0.0 |
| 7 |
* |
| 8 |
* @package Powerkit |
| 9 |
* @subpackage Modules/public |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* The public-facing functionality of the module. |
| 14 |
*/ |
| 15 |
class Powerkit_Basic_Elements_Public extends Powerkit_Module_Public { |
| 16 |
|
| 17 |
/** |
| 18 |
* Initialize |
| 19 |
*/ |
| 20 |
public function initialize() { |
| 21 |
add_action( 'init', array( $this, 'register_custom_shortcodes' ) ); |
| 22 |
|
| 23 |
$this->register_shortcodes(); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Shortcodes custom. |
| 28 |
* |
| 29 |
* @since 1.0.0 |
| 30 |
* @access public |
| 31 |
*/ |
| 32 |
public function register_custom_shortcodes() { |
| 33 |
|
| 34 |
$dir_path = apply_filters( 'powerkit_basic_shortcodes_autoload_path', 'shortcodes' ); |
| 35 |
|
| 36 |
$custom_path = wp_normalize_path( get_template_directory() . '/' . $dir_path ); |
| 37 |
|
| 38 |
if ( file_exists( $custom_path ) ) { |
| 39 |
powerkit_basic_shortcodes_autoload( $custom_path ); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Register Shortcodes |
| 45 |
* |
| 46 |
* @since 1.0.0 |
| 47 |
* @access private |
| 48 |
*/ |
| 49 |
private function register_shortcodes() { |
| 50 |
|
| 51 |
// Get all shortcodes. |
| 52 |
$sections = apply_filters( 'powerkit_basic_shortcodes_ui_args', array() ); |
| 53 |
|
| 54 |
// Add shortcodes. |
| 55 |
foreach ( $sections as $section ) { |
| 56 |
if ( true === $section['autoregister'] ) { |
| 57 |
add_shortcode( $section['base'], array( $this, 'shortcode_display' ) ); |
| 58 |
} |
| 59 |
|
| 60 |
// Repeat Shortcodes. |
| 61 |
if ( ! empty( $section['fields'] ) ) { |
| 62 |
foreach ( $section['fields'] as $field ) { |
| 63 |
if ( 'repeater' === $field['type'] && true === $field['autoregister'] ) { |
| 64 |
add_shortcode( $field['base'], array( $this, 'shortcode_display' ) ); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Shortcode Public Display |
| 73 |
* |
| 74 |
* @param array $atts User defined attributes in shortcode tag. |
| 75 |
* @param string $content Shorcode Content. |
| 76 |
* @param string $shortcode The name of the shortcode. |
| 77 |
* @return string Shortcode result HTML. |
| 78 |
*/ |
| 79 |
public function shortcode_display( $atts, $content = false, $shortcode = '' ) { |
| 80 |
// Get all shortcodes. |
| 81 |
$sections = apply_filters( 'powerkit_basic_shortcodes_ui_args', array() ); |
| 82 |
|
| 83 |
// Get Default Attrs. |
| 84 |
$default_attrs = array(); |
| 85 |
foreach ( $sections as $section ) { |
| 86 |
if ( isset( $section['fields'] ) && is_array( $section['fields'] ) ) { |
| 87 |
foreach ( $section['fields'] as $field ) { |
| 88 |
switch ( $field['type'] ) { |
| 89 |
case 'section': |
| 90 |
break; |
| 91 |
case 'repeater': |
| 92 |
if ( $field['base'] === $shortcode ) { |
| 93 |
foreach ( $field['fields'] as $repeater_field ) { |
| 94 |
$default_attrs[ $repeater_field['name'] ] = $repeater_field['default'] ? $repeater_field['default'] : ''; |
| 95 |
} |
| 96 |
} |
| 97 |
break; |
| 98 |
default: |
| 99 |
if ( $section['base'] === $shortcode ) { |
| 100 |
$default_attrs[ $field['name'] ] = $field['default'] ? $field['default'] : ''; |
| 101 |
} |
| 102 |
break; |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
// Merge Attrs. |
| 109 |
$atts = shortcode_atts( $default_attrs, $atts ); |
| 110 |
|
| 111 |
// Content. |
| 112 |
$content = do_shortcode( $content ); |
| 113 |
|
| 114 |
/** |
| 115 |
* Filters a shortcode's HTML. |
| 116 |
* |
| 117 |
* @param array $output Shortcode HTML. |
| 118 |
* @param array $atts User defined attributes in shortcode tag. |
| 119 |
* @param string $content Shorcode tag content. |
| 120 |
* @return string Shortcode result HTML. |
| 121 |
*/ |
| 122 |
$output = apply_filters( $shortcode . '_shortcode', '', $atts, $content ); |
| 123 |
|
| 124 |
return $output; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Register the stylesheets for the public-facing side of the site. |
| 129 |
*/ |
| 130 |
public function wp_enqueue_scripts() { |
| 131 |
// Styles. |
| 132 |
wp_enqueue_style( 'powerkit-basic-elements', powerkit_style( plugin_dir_url( __FILE__ ) . 'css/public-powerkit-basic-elements.css' ), false, powerkit_get_setting( 'version' ), 'screen' ); |
| 133 |
|
| 134 |
// Add RTL support. |
| 135 |
wp_style_add_data( 'powerkit-basic-elements', 'rtl', 'replace' ); |
| 136 |
|
| 137 |
// Scripts. |
| 138 |
wp_enqueue_script( 'powerkit-basic-elements', plugin_dir_url( __FILE__ ) . 'js/public-powerkit-basic-elements.js', array( 'jquery' ), '4.0.0', true ); |
| 139 |
} |
| 140 |
} |
| 141 |
|