* @license GPL-2.0+ * @link http://www.contentviewspro.com/ * @copyright 2014 PT Guy */ if ( !function_exists( 'get_plugin_data' ) ) { require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); } if ( !class_exists( 'PT_CV_Functions' ) ) { /** * @name PT_CV_Functions * @todo Utility functions */ class PT_CV_Functions { static $prev_random_string = ''; /** * Compare current Wordpress version with a version * * @global string $wp_version * * @param string $version_to_compare * @param string $operator * * @return boolean */ static function wp_version_compare( $version_to_compare, $operator = '>=' ) { if ( empty( $version_to_compare ) ) { return true; } global $wp_version; // Check if using Wordpress version 3.7 or higher return version_compare( $wp_version, $version_to_compare, $operator ); } /** * Get current language of site */ static function get_language() { $language = ''; // WPML global $sitepress; if ( $sitepress && method_exists( $sitepress, 'get_current_language' ) ) { $language = $sitepress->get_current_language(); } /** * qTranslate-X (and qTranslate, mqTranslate) * @since 1.5.3 */ global $q_config; if ( $q_config && !empty( $q_config[ 'language' ] ) ) { $language = $q_config[ 'language' ]; } return $language; } /** * Switch language * * @global type $sitepress * @param string $language Current language */ static function switch_language( $language ) { if ( !$language ) return; // WPML global $sitepress; if ( $sitepress && $language ) { $sitepress->switch_lang( $language, true ); } /** * qTranslate-X (and qTranslate, mqTranslate) * @since 1.5.3 */ global $q_config; if ( $q_config ) { $q_config[ 'language' ] = $language; } } /** * Get plugin info * * @param string $file Absolute path to the plugin file * @param string $data Field of plugin data want to get * * @return array | null */ static function plugin_info( $file, $data = '' ) { $plugin_data = get_plugin_data( $file ); return isset( $plugin_data[ $data ] ) ? $plugin_data[ $data ] : NULL; } /** * Add sub menu page * * @param string $parent_slug Slug of parent menu * @param string $page_title Title of page * @param string $menu_title Title of menu * @param string $user_role Required role to see this menu * @param string $sub_page Slug of sub menu * @param string $class Class name which contains function to output content of page created by this menu */ static function menu_add_sub( $parent_slug, $page_title, $menu_title, $user_role, $sub_page, $class ) { return add_submenu_page( $parent_slug, $page_title, $menu_title, $user_role, $parent_slug . '-' . $sub_page, array( $class, 'display_sub_page_' . $sub_page ) ); } /** * Get current post type in Admin * * @global type $post * @global type $typenow * @global type $current_screen * @return type */ static function admin_current_post_type() { global $post, $typenow, $current_screen; //we have a post so we can just get the post type from that if ( $post && $post->post_type ) { return $post->post_type; } //check the global $typenow - set in admin.php elseif ( $typenow ) { return $typenow; } //check the global $current_screen object - set in sceen.php elseif ( $current_screen && isset( $current_screen->post_type ) ) { return $current_screen->post_type; } } /** * Include content of file * * @param string $file_path Absolute path of file * * @return NULL | string Content of file */ static function file_include_content( $file_path ) { $content = NULL; if ( file_exists( $file_path ) ) { ob_start(); include_once $file_path; $content = ob_get_clean(); } return $content; } /** * Get value of option Content Views Settings page * @param string $option_name * @param mixed $default * @return mixed */ static function get_option_value( $option_name, $default = '' ) { $options = get_option( PT_CV_OPTION_NAME ); return isset( $options[ $option_name ] ) ? $options[ $option_name ] : $default; } /** * Generate random string * * @param bool $prev_return Return previous generated string * * @return string */ static function string_random( $prev_return = false ) { if ( $prev_return ) { return PT_CV_Functions::$prev_random_string; } // Don't use uniqid(), it will cause bug when multiple elements have same ID $str = '0123456789abcdefghijklmnopqrstuvwxyz'; $rand = substr( md5( mt_rand() ), 0, 7 ) . substr( str_shuffle( $str ), 0, 3 ); PT_CV_Functions::$prev_random_string = $rand; return PT_CV_Functions::$prev_random_string; } /** * Create array from string, use explode function * * @param string $string String to explode * @param string $delimiter Delimiter to explode string * * @return array */ static function string_to_array( $string, $delimiter = ',' ) { return is_array( $string ) ? $string : (array) explode( $delimiter, (string) str_replace( ' ', '', $string ) ); } /** * Slug to nice String * * @param string $slug Slug string * * @return string */ static function string_slug_to_text( $slug ) { $slug = preg_replace( '/[_\-]+/', ' ', $slug ); return ucwords( $slug ); } /** * Trims text to a certain number of words. * @since 1.4.3 * @param string $text * @param int $num_words * @return string */ static function cv_trim_words( $text, $num_words = 500 ) { $text = apply_filters( PT_CV_PREFIX_ . 'before_generate_excerpt', $text ); $text = self::cv_strip_shortcodes( $text ); $result = self::cv_strip_tags( $text ); return self::trim_words( $result, $num_words ); } /** * Strip shortcodes in CV way, to replace WP strip_shortcodes() * @after 1.8.3 * @param string $text */ static function cv_strip_shortcodes( $text, $strip_all = true ) { if ( apply_filters( PT_CV_PREFIX_ . 'excerpt_strip_all_shortcodes', false ) ) { return strip_shortcodes( $text ); } global $shortcode_tags, $cv_sc_tagnames, $cv_sc_complete; if ( $cv_sc_complete ) { $tagnames = array_keys( $shortcode_tags ); $tagregexp = join( '|', array_map( 'preg_quote', $tagnames ) ); } else { $tagregexp = $cv_sc_tagnames; } if ( $strip_all ) { // Strip some shortcodes $temp_shortcode_tags = $shortcode_tags; $shortcode_tags = apply_filters( PT_CV_PREFIX_ . 'shortcode_to_strip', array( 'caption' => '' ) ); $text = strip_shortcodes( $text ); $shortcode_tags = $temp_shortcode_tags; } // Keep other shortcodes' content return preg_replace( '/' . '\[' // Opening bracket . '(\/?)' . "($tagregexp)" // All shortcode tags . '[^\]]*' // Shortcode parameters . '\]' // Closing bracket . '/', '', $text ); } /** * Trim words in CV way * @return string */ static function trim_words( $text, $num_words ) { $more = ''; $text = apply_filters( PT_CV_PREFIX_ . 'before_trim_words', $text ); /* * translators: If your word count is based on single characters (e.g. East Asian characters), * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'. * Do not translate into your own language. */ /** * Do not use "[\r\n\t ]|\xC2\xA0| " as regex pattern, it can stop PHP process * @since 1.9.5 */ $text = str_replace( array( '\xC2\xA0', ' ' ), ' ', $text ); if ( strpos( _x( 'words', 'Word count type. Do not translate!' ), 'characters' ) === 0 && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) { $text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' ); preg_match_all( '/./u', $text, $words_array ); $words_array = array_slice( $words_array[ 0 ], 0, $num_words + 1 ); $sep = ''; } else { $words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY ); $sep = ' '; } if ( count( $words_array ) > $num_words ) { array_pop( $words_array ); $text = implode( $sep, $words_array ); $text = $text . $more; } else { $text = implode( $sep, $words_array ); } return apply_filters( PT_CV_PREFIX_ . 'after_trim_words', $text ); } /** * Custom strip tags, allow some tags * * @since 1.4.6 * @param string $string * @return string */ static function cv_strip_tags( $string ) { // Changes double line-breaks in the text into HTML paragraphs (

,
) if ( apply_filters( PT_CV_PREFIX_ . 'wpautop', 0 ) ) { $string = wpautop( $string ); } // Strip HTML tags if ( apply_filters( PT_CV_PREFIX_ . 'strip_tags', 1 ) ) { // Remove entire tag content $tags_to_rm = apply_filters( PT_CV_PREFIX_ . 'tag_to_remove', array( 'script', 'style' ) ); $string = preg_replace( array( '@<(' . implode( '|', $tags_to_rm ) . ')[^>]*?>.*?@si' ), '', $string ); // Remove tag name & properties $allowed_tags = ''; if ( PT_CV_Functions::setting_value( PT_CV_PREFIX . 'field-excerpt-allow_html' ) ) { $allowable_tags = (array) apply_filters( PT_CV_PREFIX_ . 'allowable_tags', array( '', '
', '', '', '', '', '