Advertisement
Guest User

Types 1.7.10 Fixed

a guest
Aug 4th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.04 KB | None | 0 0
  1. <?php
  2. /**
  3.  *
  4.  * Loader class
  5.  *
  6.  *
  7.  */
  8.  
  9. /**
  10.  * Loader Class
  11.  *
  12.  * @since Types 1.2
  13.  * @package Types
  14.  * @subpackage Classes
  15.  * @version 0.2
  16.  * @category Loader
  17.  * @author srdjan <srdjan@icanlocalize.com>
  18.  */
  19. class WPCF_Loader
  20. {
  21.  
  22.     /**
  23.      * Settings
  24.      * @var array
  25.      */
  26.     private static $__settings = array();
  27.  
  28.     public static function init( $settings = array() ) {
  29.         self::$__settings = (array) $settings;
  30.         self::__registerScripts();
  31.         self::__registerStyles();
  32.         self::__toolset();
  33.         add_action( 'admin_print_scripts', array('WPCF_Loader', 'renderJsSettings'), 5 );
  34.         add_filter( 'the_posts', array('WPCF_Loader', 'wpcf_cache_complete_postmeta') );
  35.         add_filter( 'wpcf_fields_value_save', array( 'WPCF_Loader', 'wpcf_sanitize_values_on_save' ) );
  36.     }
  37.  
  38.     /**
  39.     * Sanitize fields values on save
  40.     *
  41.     */
  42.  
  43.     public static function wpcf_sanitize_values_on_save( $value ) {
  44.         if ( is_array( $value ) ) {
  45.             // Recursion
  46.             $value = array_map( array( 'WPCF_Loader', 'wpcf_sanitize_values_on_save' ), $value );
  47.         } else {
  48.             // $value = wp_filter_kses( $value );
  49.         }
  50.         return $value;
  51.     }
  52.  
  53.     /**
  54.      * Cache the postmeta for posts returned by a WP_Query
  55.      *
  56.      * @global object $wpdb
  57.      *
  58.      */
  59.  
  60.     public static function wpcf_cache_complete_postmeta( $posts ) {
  61.         global $wpdb;
  62.         if ( !$posts )
  63.             return $posts;
  64.         $post_ids = array();
  65.         $cache_group_ids = 'types_cache_ids';
  66.         $cache_group = 'types_cache';
  67.         foreach ( $posts as $post ) {
  68.             $cache_key_looped_post = md5( 'post::_is_cached' . $post->ID );
  69.             $cached_object = wp_cache_get( $cache_key_looped_post, $cache_group_ids );
  70.             if ( false === $cached_object ) {
  71.                 $post_ids[] = intval( $post->ID );
  72.                 wp_cache_add( $cache_key_looped_post, $post->ID, $cache_group_ids );
  73.             }
  74.         }
  75.         if ( count( $post_ids ) > 0 ) {
  76.             $id_list = join( ',', $post_ids );
  77.             $all_postmeta = $wpdb->get_results( "SELECT * FROM {$wpdb->postmeta} WHERE post_id IN ($id_list)", OBJECT );
  78.             if ( !empty( $all_postmeta ) ) {
  79.                 $cache_key_keys = array();
  80.                 foreach ( $all_postmeta as $metarow ) {
  81.                     $mpid = intval($metarow->post_id);
  82.                     $mkey = $metarow->meta_key;
  83.                     $cache_key_keys[$mpid . $mkey][] = $metarow;
  84.                 }
  85.                 foreach ( $cache_key_keys as $single_meta_keys => $single_meta_values ) {
  86.                     $cache_key_looped_new = md5( 'field::_get_meta' . $single_meta_keys );
  87.                     wp_cache_add( $cache_key_looped_new, $single_meta_values, $cache_group );// WordPress cache
  88.                 }
  89.             }
  90.         }
  91.         return $posts;
  92.     }
  93.  
  94.     /**
  95.      * Register scripts.
  96.      */
  97.     private static function __registerScripts() {
  98.         $min = '';//WPCF_DEBUG ? '-min' : '';
  99.         wp_register_script( 'types', WPCF_EMBEDDED_RES_RELPATH . '/js/basic.js',
  100.                 array('jquery'), WPCF_VERSION, true );
  101.         wp_register_script( 'types-knockout',
  102.                 WPCF_EMBEDDED_RES_RELPATH . '/js/knockout-2.2.1.js',
  103.                 array('jquery'), WPCF_VERSION, true );
  104.         if ( !wp_script_is( 'toolset-colorbox', 'registered' ) ) {
  105.             wp_register_script( 'toolset-colorbox',
  106.                     WPCF_EMBEDDED_RES_RELPATH . '/js/jquery.colorbox-min.js',
  107.                     array('jquery'), WPCF_VERSION, true );
  108.         }
  109.         wp_register_script( 'types-utils',
  110.                 WPCF_EMBEDDED_RES_RELPATH . "/js/utils{$min}.js", array('jquery'),
  111.                 WPCF_VERSION, true );
  112.         wp_register_script( 'types-wp-views',
  113.                 WPCF_EMBEDDED_RES_RELPATH . '/js/wp-views.js', array('jquery'),
  114.                 WPCF_VERSION, true );
  115.         global $pagenow;
  116.         // Exclude on post edit screen
  117.         if ( defined( 'WPTOOLSET_FORMS_ABSPATH' )
  118.                 && !in_array( $pagenow, array('edit.php', 'post.php', 'post-new.php') ) ) {
  119.         wp_register_script( 'types-conditional',
  120.                 WPCF_EMBEDDED_RES_RELPATH . '/js/conditional.js',
  121.                 array('types-utils'), WPCF_VERSION, true );
  122.         wp_register_script( 'types-validation',
  123.                 WPCF_EMBEDDED_RES_RELPATH . "/js/validation{$min}.js",
  124.                 array('jquery'), WPCF_VERSION, true );
  125.         }
  126. //        wp_register_script( 'types-jquery-validation',
  127. //                WPCF_EMBEDDED_RES_RELPATH . '/js/jquery-form-validation/jquery.validate-1.11.1.min.js',
  128. //                array('jquery'), WPCF_VERSION, true );
  129. //        wp_register_script( 'types-jquery-validation-additional',
  130. //                WPCF_EMBEDDED_RES_RELPATH . '/js/jquery-form-validation/additional-methods-1.11.1.min.js',
  131. //                array('types-jquery-validation'), WPCF_VERSION, true );
  132. //        wp_register_script( 'types-js-validation',
  133. //                WPCF_EMBEDDED_RES_RELPATH . '/js/jquery-form-validation/types.js',
  134. //                array('types-jquery-validation-additional'), WPCF_VERSION, true );
  135.     }
  136.  
  137.     /**
  138.      * Register styles.
  139.      */
  140.     private static function __registerStyles() {
  141.         wp_register_style( 'types',
  142.                 WPCF_EMBEDDED_RES_RELPATH . '/css/basic.css', array(),
  143.                 WPCF_VERSION );
  144.         if ( !wp_style_is( 'toolset-colorbox', 'registered' ) ) {
  145.             wp_register_style( 'toolset-colorbox',
  146.                     WPCF_EMBEDDED_RES_RELPATH . '/css/colorbox.css', array(),
  147.                     WPCF_VERSION );
  148.         }
  149.         if ( !wp_style_is( 'toolset-font-awesome', 'registered' ) ) {
  150.             wp_register_style( 'toolset-font-awesome',
  151.                     WPCF_EMBEDDED_RES_RELPATH . '/css/font-awesome/css/font-awesome.min.css',
  152.                     array('admin-bar', 'wp-admin', 'buttons', 'media-views'),
  153.                     WPCF_VERSION );
  154.         }
  155.         if ( !wp_style_is( 'toolset-dashicons', 'registered' ) ) {
  156.             wp_register_style(
  157.                 'toolset-dashicons',
  158.                 WPCF_EMBEDDED_RES_RELPATH . '/css/dashicons.css',
  159.                 array(),
  160.                 WPCF_VERSION
  161.             );
  162.         }
  163.     }
  164.  
  165.     /**
  166.      * Returns HTML formatted output.
  167.      *
  168.      * @param string $view
  169.      * @param mixed $data
  170.      * @return string
  171.      */
  172.     public static function view( $view, $data = array() ) {
  173.         $file = WPCF_EMBEDDED_ABSPATH . '/views/'
  174.                 . strtolower( strval( $view ) ) . '.php';
  175.         if ( !file_exists( $file ) ) {
  176.             return '<code>missing_view</code>';
  177.         }
  178.         ob_start();
  179.         include $file;
  180.         $output = ob_get_contents();
  181.         ob_get_clean();
  182.  
  183.         return apply_filters( 'wpcf_get_view', $output, $view, $data );
  184.     }
  185.  
  186.     /**
  187.      * Returns HTML formatted output.
  188.      *
  189.      * @param string $view
  190.      * @param mixed $data
  191.      * @return string
  192.      */
  193.     public static function loadView( $view ) {
  194.         $file = WPCF_EMBEDDED_ABSPATH . '/views/'
  195.                 . strtolower( strval( $view ) ) . '.php';
  196.         if ( !file_exists( $file ) ) {
  197.             return new WP_Error( 'types_loader', 'missing view ' . $view );
  198.         }
  199.         require_once $file;
  200.     }
  201.  
  202.     /**
  203.      * Returns HTML formatted output.
  204.      *
  205.      * @param string $template
  206.      * @param mixed $data
  207.      * @return string
  208.      */
  209.     public static function template( $template, $data = array() ) {
  210.         $file = WPCF_EMBEDDED_ABSPATH . '/views/templates/'
  211.                 . strtolower( strval( $template ) ) . '.tpl.php';
  212.         if ( !file_exists( $file ) ) {
  213.             return '<code>missing_template</code>';
  214.         }
  215.         ob_start();
  216.         include $file;
  217.         $output = ob_get_contents();
  218.         ob_get_clean();
  219.  
  220.         return apply_filters( 'wpcf_get_template', $output, $template, $data );
  221.     }
  222.  
  223.     /**
  224.      * Loads model.
  225.      *
  226.      * @param string $template
  227.      * @param mixed $data
  228.      * @return string
  229.      */
  230.     public static function loadModel( $model ) {
  231.         $file = WPCF_EMBEDDED_ABSPATH . '/models/'
  232.                 . strtolower( strval( $model ) ) . '.php';
  233.         if ( !file_exists( $file ) ) {
  234.             return new WP_Error( 'types_loader', 'missing model ' . $model );
  235.         }
  236.         require_once $file;
  237.     }
  238.  
  239.     /**
  240.      * Loads class.
  241.      *
  242.      * @param string $template
  243.      * @param mixed $data
  244.      * @return string
  245.      */
  246.     public static function loadClass( $class ) {
  247.         $file = WPCF_EMBEDDED_ABSPATH . '/classes/'
  248.                 . strtolower( strval( $class ) ) . '.php';
  249.         if ( !file_exists( $file ) ) {
  250.             return new WP_Error( 'types_loader', 'missing class ' . $class );
  251.         }
  252.         require_once $file;
  253.     }
  254.  
  255.     /**
  256.      * Loads include.
  257.      *
  258.      * @param string $template
  259.      * @param mixed $data
  260.      * @return string
  261.      */
  262.     public static function loadInclude( $name, $mode = 'embedded' ) {
  263.         $path = $mode == 'plugin' ? WPCF_ABSPATH : WPCF_EMBEDDED_ABSPATH;
  264.         $file = $path . '/includes/' . strtolower( strval( $name ) ) . '.php';
  265.         if ( !file_exists( $file ) ) {
  266.             return new WP_Error( 'types_loader', 'missing include ' . $name );
  267.         }
  268.         require_once $file;
  269.     }
  270.  
  271.     /**
  272.      * Adds JS settings.
  273.      *
  274.      * @staticvar array $settings
  275.      * @param type $id
  276.      * @param type $setting
  277.      */
  278.     public static function addJsSetting( $id, $setting = '' ) {
  279.         self::$__settings[$id] = $setting;
  280.     }
  281.  
  282.     /**
  283.      * Renders JS settings.
  284.      */
  285.     public static function renderJsSettings() {
  286.         $settings = (array) self::$__settings;
  287.         $settings['wpnonce'] = wp_create_nonce( '_typesnonce' );
  288.         $settings['cookiedomain'] = COOKIE_DOMAIN;
  289.         $settings['cookiepath'] = COOKIEPATH;
  290.         $settings['validation'] = array();
  291.         echo '
  292.        <script type="text/javascript">
  293.            //<![CDATA[
  294.            var types = ' . json_encode( $settings ) . ';
  295.            //]]>
  296.        </script>';
  297.     }
  298.  
  299.     /**
  300.      * Toolset loading.
  301.      */
  302.     private static function __toolset() {
  303.         // Views
  304.         if ( defined( 'WPV_VERSION' ) ) {
  305.             self::loadClass( 'wpviews' );
  306.             WPCF_WPViews::init();
  307.         }
  308.     }
  309.  
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement