arie_cristianD

add jkit additional funcitons

Jul 10th, 2025
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.09 KB | None | 0 0
  1. /**
  2.  * Get version of Jeg Framework. Can be overridden by plugin or theme.
  3.  *
  4.  * @return string
  5.  */
  6. if ( ! function_exists( 'jeg_get_version' ) ) {
  7.     function jeg_get_version() {
  8.         return apply_filters( 'jeg_framework_version', JEG_VERSION );
  9.     }
  10. }
  11.  
  12. if ( ! function_exists( 'jeg_is_json' ) ) {
  13.     /**
  14.      * Check if string is json
  15.      *
  16.      * @param string $string string to check.
  17.      *
  18.      * @return bool
  19.      */
  20.     function jeg_is_json( $string ) {
  21.         if ( ! is_string( $string ) ) {
  22.             return false;
  23.         }
  24.  
  25.         json_decode( urldecode( $string ) );
  26.  
  27.         return ( JSON_ERROR_NONE == json_last_error() );
  28.     }
  29. }
  30.  
  31. if ( ! function_exists( 'jeg_sanitize_input_field' ) ) {
  32.     /**
  33.      * Recursively Sanitize Input Field
  34.      *
  35.      * @param mixed $values Value to be sanitized.
  36.      *
  37.      * @return mixed
  38.      */
  39.     function jeg_sanitize_input_field( $values ) {
  40.  
  41.         foreach ( $values as $key => $value ) {
  42.             if ( jeg_is_json( $value ) ) {
  43.                 $value = json_decode( urldecode( $value ) );
  44.             }
  45.  
  46.             if ( is_object( $value ) ) {
  47.                 $value = (array) $value;
  48.             }
  49.  
  50.             if ( is_array( $value ) ) {
  51.                 $values[ $key ] = jeg_sanitize_input_field( $value );
  52.             } else {
  53.                 $values[ $key ] = sanitize_text_field( $value );
  54.             }
  55.         }
  56.  
  57.         return $values;
  58.     }
  59. }
  60.  
  61. /**
  62.  * Get Meta box value
  63.  *
  64.  * @param string $name Metabox Name.
  65.  * @param mixed  $default Default Value for Metabox.
  66.  * @param int    $post_id Post ID.
  67.  *
  68.  * @return mixed
  69.  */
  70. if ( ! function_exists( 'jeg_metabox' ) ) {
  71.     function jeg_metabox( $name, $default = null, $post_id = null ) {
  72.         global $post;
  73.  
  74.         if ( ! is_null( $post_id ) ) {
  75.             $the_post = get_post( $post_id );
  76.             if ( empty( $the_post ) ) {
  77.                 $post_id = null;
  78.             }
  79.         }
  80.  
  81.         if ( is_null( $post ) && is_null( $post_id ) ) {
  82.             return apply_filters( 'jeg_metabox', $default, $name );
  83.         }
  84.  
  85.         if ( is_null( $post_id ) ) {
  86.             $post_id = $post->ID;
  87.         }
  88.  
  89.         $keys = explode( '.', $name );
  90.         $temp = null;
  91.  
  92.         foreach ( $keys as $index => $key ) {
  93.             if ( 0 === $index ) {
  94.                 $meta_values = get_post_meta( $post_id, $key, true );
  95.                 if ( ! empty( $meta_values ) ) {
  96.                     $temp = $meta_values;
  97.                 } else {
  98.                     return apply_filters( 'jeg_metabox', $default, $name );
  99.                 }
  100.             } else {
  101.                 if ( is_array( $temp ) ) {
  102.                     if ( isset( $temp[ $key ] ) ) {
  103.                         $temp = $temp[ $key ];
  104.                     } else {
  105.                         $temp = $default;
  106.                     }
  107.                 }
  108.             }
  109.         }
  110.  
  111.         return apply_filters( 'jeg_metabox', $temp, $name );
  112.     }
  113. }
  114.  
  115. if ( ! function_exists( 'jeg_allowed_protocols' ) ) {
  116.  
  117.     add_filter( 'kses_allowed_protocols', 'jeg_allowed_protocols' );
  118.  
  119.     /**
  120.      * Extend Allowed Protocol WP KSES.
  121.      *
  122.      * @param array $allowed_protocols Allowed Protocols.
  123.      *
  124.      * @return array
  125.      */
  126.     function jeg_allowed_protocols( $allowed_protocols ) {
  127.         $allowed_protocols = array_merge( $allowed_protocols, array( 'data' ) );
  128.  
  129.         return $allowed_protocols;
  130.     }
  131. }
  132.  
  133. if ( ! function_exists( 'jeg_allowed_html' ) ) {
  134.  
  135.     add_filter( 'wp_kses_allowed_html', 'jeg_allowed_html' );
  136.  
  137.     /**
  138.      * Extend Allowed HTML WP KSES.
  139.      *
  140.      * @param array $allowedtags Allowed Tag.
  141.      *
  142.      * @return array
  143.      */
  144.     function jeg_allowed_html( $allowedtags ) {
  145.         $allowedtags['br']   = array_merge( isset( $allowedtags['br'] ) ? $allowedtags['br'] : array(), array() );
  146.         $allowedtags['ul']   = array_merge(
  147.             isset( $allowedtags['ul'] ) ? $allowedtags['ul'] : array(),
  148.             array(
  149.                 'class' => true,
  150.                 'style' => true,
  151.             )
  152.         );
  153.         $allowedtags['ol']   = array_merge( isset( $allowedtags['ol'] ) ? $allowedtags['ol'] : array(), array() );
  154.         $allowedtags['li']   = array_merge( isset( $allowedtags['li'] ) ? $allowedtags['li'] : array(), array() );
  155.         $allowedtags['a']    = array_merge(
  156.             isset( $allowedtags['a'] ) ? $allowedtags['a'] : array(),
  157.             array(
  158.                 'href'   => true,
  159.                 'title'  => true,
  160.                 'target' => true,
  161.                 'class'  => true,
  162.                 'style'  => true,
  163.             )
  164.         );
  165.         $allowedtags['span'] = array_merge(
  166.             isset( $allowedtags['span'] ) ? $allowedtags['span'] : array(),
  167.             array(
  168.                 'class'       => true,
  169.                 'style'       => true,
  170.                 'data-*'      => true,
  171.                 'aria-hidden' => true,
  172.             )
  173.         );
  174.         $allowedtags['i']    = array_merge(
  175.             isset( $allowedtags['i'] ) ? $allowedtags['i'] : array(),
  176.             array(
  177.                 'class' => true,
  178.             )
  179.         );
  180.         $allowedtags['div']  = array_merge(
  181.             isset( $allowedtags['div'] ) ? $allowedtags['div'] : array(),
  182.             array(
  183.                 'id'         => true,
  184.                 'class'      => true,
  185.                 'data-id'    => true,
  186.                 'data-video' => true,
  187.                 'style'      => true,
  188.             )
  189.         );
  190.         $allowedtags['img']  = array_merge(
  191.             isset( $allowedtags['img'] ) ? $allowedtags['img'] : array(),
  192.             array(
  193.                 'class'  => true,
  194.                 'src'    => true,
  195.                 'alt'    => true,
  196.                 'srcset' => true,
  197.                 'width'  => true,
  198.                 'height' => true,
  199.             )
  200.         );
  201.  
  202.         return $allowedtags;
  203.     }
  204. }
  205.  
  206. if ( ! function_exists( 'jeg_check_video_source' ) ) {
  207.     /**
  208.      * Check video source
  209.      */
  210.     function jeg_check_video_source( $url ) {
  211.         $source = '';
  212.  
  213.         // check if the value contain html tag
  214.         if ( $url != strip_tags( $url ) ) {
  215.             return $source;
  216.         }
  217.  
  218.         if ( strpos( $url, 'youtube' ) > 0 || strpos( $url, 'youtu.be' ) > 0 ) {
  219.             $source = 'youtube';
  220.         } elseif ( strpos( $url, 'vimeo' ) > 0 ) {
  221.             $source = 'vimeo';
  222.         } else {
  223.             $format = strtolower( pathinfo( $url, PATHINFO_EXTENSION ) );
  224.  
  225.             if ( 'mp4' === $format ) {
  226.                 $source = 'mp4';
  227.             }
  228.         }
  229.  
  230.         return $source;
  231.     }
  232. }
  233.  
  234. if ( ! function_exists( 'jeg_get_option' ) ) {
  235.     /**
  236.      * Get Jeg option
  237.      */
  238.     function jeg_get_option( $setting, $default = null ) {
  239.         $options = get_option( 'jeg', array() );
  240.         $value   = $default;
  241.         if ( isset( $options[ $setting ] ) ) {
  242.             $value = $options[ $setting ];
  243.         }
  244.  
  245.         return $value;
  246.     }
  247. }
  248.  
  249. if ( ! function_exists( 'jeg_sanitize_array' ) ) {
  250.     /**
  251.      * Sanitizing Array recursively
  252.      *
  253.      * @param array $data The data to be sanitized.
  254.      *
  255.      * @return array sanitized array
  256.      */
  257.     function jeg_sanitize_array( $data ) {
  258.         if ( is_array( $data ) ) {
  259.             foreach ( $data as $key => $value ) {
  260.                 $data[ $key ] = jeg_sanitize_array( $value );
  261.             }
  262.             return $data;
  263.         }
  264.         return sanitize_text_field( $data );
  265.     }
  266. }
  267.  
  268.  
Add Comment
Please, Sign In to add comment