Advertisement
Guest User

Untitled

a guest
Jun 1st, 2014
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.20 KB | None | 0 0
  1. <?php
  2. /**
  3. * @package   Warp Theme Framework
  4. * @author    YOOtheme http://www.yootheme.com
  5. * @copyright Copyright (C) YOOtheme GmbH
  6. * @license   YOOtheme Proprietary Use License (http://www.yootheme.com/license)
  7. */
  8.  
  9. /*
  10.     Class: WarpHelperSystem
  11.         Wordpress system helper class, provides Wordpress 3.0 Blog integration (http://wordpress.org)
  12. */
  13. class WarpHelperSystem extends WarpHelper {
  14.  
  15.     /* system path */
  16.     var $path;
  17.  
  18.     /* system url */
  19.     var $url;
  20.  
  21.     /* cache path */
  22.     var $cache_path;
  23.  
  24.     /* cache time */
  25.     var $cache_time;
  26.  
  27.     /* theme prefix */
  28.     var $prefix;
  29.  
  30.     /* theme xml */
  31.     var $xml;
  32.  
  33.     /* query */
  34.     var $query;
  35.  
  36.     /* widgets */
  37.     var $widgets;
  38.    
  39.     /* all widget options */
  40.     var $widget_options;
  41.    
  42.     /* all menu items options */
  43.     var $menu_item_options;
  44.    
  45.     /* all config overrides */
  46.     var $config_overrides;
  47.    
  48.     /*
  49.         Function: Constructor
  50.             Class Constructor.
  51.     */
  52.     function __construct() {
  53.         parent::__construct();
  54.  
  55.         // parse site url
  56.         $urlinfo = parse_url(get_option('siteurl'));
  57.         $url     = $urlinfo['path'];
  58.  
  59.         // init vars
  60.         $this->path       = rtrim(str_replace(DIRECTORY_SEPARATOR, '/', ABSPATH), '/');
  61.         $this->url        = $url != '/' ? $url : null;
  62.         $this->cache_path = rtrim(str_replace(DIRECTORY_SEPARATOR, '/', get_template_directory()), '/').'/cache';
  63.         $this->cache_time = 86400;
  64.         $this->prefix     = get_template().'_';
  65.        
  66.         // set callback
  67.         $this->callbacks['construct'][] = 'init';
  68.     }
  69.  
  70.     /*
  71.         Function: init
  72.             Initialize system configuration
  73.  
  74.         Returns:
  75.             Void
  76.     */
  77.     function init() {
  78.  
  79.         // init options
  80.         $this->widget_options    = get_option('warp_widget_options', array());
  81.         $this->menu_item_options = get_option($this->prefix.'menu-items', array());
  82.         $this->config_overrides  = get_option($this->prefix.'overrides', array());
  83.  
  84.         // init helpers
  85.         $path    =& $this->getHelper('path');
  86.         $config  =& $this->getHelper('config');
  87.         $modules =& $this->getHelper('modules');
  88.         $xml     =& $this->getHelper('xml');
  89.  
  90.         // set cache directory
  91.         if (!file_exists($this->cache_path)) {
  92.             mkdir($this->cache_path, 0755);
  93.         }
  94.  
  95.         // set paths
  96.         $path->register($this->path.'/wp-admin', 'admin');
  97.         $path->register($this->path, 'site');
  98.         $path->register($this->cache_path, 'cache');
  99.         $path->register($path->path('warp:systems/wordpress/menus'), 'menu');
  100.         $path->register($path->path('warp:systems/wordpress/widgets'), 'widgets');
  101.         $path->register($path->path('template:').'/widgets', 'widgets');
  102.  
  103.         // set translations
  104.         load_theme_textdomain('warp', $path->path('template:languages'));
  105.  
  106.         // get theme xml
  107.         $this->xml =& $xml->load($path->path('template:template.xml'), 'xml', true);
  108.  
  109.         // get parameter
  110.         $options = array();
  111.  
  112.         foreach ($this->xml->document->getElements('params') as $param) {
  113.             foreach ($param->children() as $setting) {
  114.  
  115.                 $name = $setting->attributes('name');
  116.                 $options[$param->attributes('group')][] = $setting;
  117.  
  118.                 if ($name == 'presets') {
  119.                     $config->set('presets', array());
  120.                 } else {
  121.                     $config->set($name, get_option($this->prefix.$name, $setting->attributes('default')));
  122.                 }
  123.             }
  124.         }
  125.  
  126.         $config->set('warp.settings.theme', $options);
  127.  
  128.         // get module positions
  129.         $options   = array();
  130.         $positions = $this->xml->document->getElement('positions');
  131.  
  132.         foreach ($positions->children() as $pos) {
  133.  
  134.             $name = trim($pos->data());
  135.             $options[$name] = array('configurable' => true);
  136.  
  137.             if ($pos->attributes('configurable') === '0') {
  138.                 $options[$name]['configurable'] = false;
  139.                 $options[$name]['info'] = $pos->attributes('info') ? $pos->attributes('info') : 'This position is not configurable.';
  140.             }
  141.  
  142.             $modules->register($name);
  143.         }
  144.  
  145.         $config->set('warp.positions', $options);
  146.  
  147.         // load widgets
  148.         foreach ($path->dirs('widgets:') as $name) {
  149.             if ($file = $path->path("widgets:{$name}/{$name}.php")) {
  150.                 require_once($file);
  151.             }
  152.         }
  153.  
  154.         // add actions
  155.         add_action('wp_ajax_warp_search', array($this, 'ajaxSearch'));
  156.         add_action('wp_ajax_nopriv_warp_search', array($this, 'ajaxSearch'));
  157.  
  158.         // register main menu  
  159.         register_nav_menus(array('main_menu' => 'Main Navigation Menu'));
  160.  
  161.         // is admin or site
  162.         if (is_admin()) {
  163.            
  164.             // load helper
  165.             $this->warp->loadHelper(array('control'));
  166.            
  167.             // add actions
  168.             add_action('admin_init', array($this, '_adminInit'));
  169.             add_action('admin_menu', array($this, '_adminMenu'));
  170.  
  171.         } else {
  172.  
  173.             // add actions
  174.             add_action('wp', array($this, 'overrideConfig'));
  175.  
  176.             // allow & and special html chars in menu item
  177.             add_filter('wp_setup_nav_menu_item', create_function('$menu_item','$menu_item->title = htmlspecialchars($menu_item->title, ENT_COMPAT, "UTF-8");return $menu_item;'));
  178.  
  179.             // filter widgets that should not be displayed
  180.             add_filter('widget_display_callback', create_function('$instance,$widget,$args','
  181.                     $warp = Warp::getInstance();
  182.                     $w = $warp->system->getWidget($widget->id);
  183.                     return $w->display ? $instance : false;
  184.             '),10, 3);
  185.  
  186.             // remove auto-linebreaks ?
  187.             if (!$config->get('wpautop', 1)) {
  188.                 remove_filter('the_content', 'wpautop');
  189.             }
  190.  
  191.             // dynamic presets ?
  192.             if ($config->get('allow_dynamic_preset')) {
  193.  
  194.                 if (!session_id()) session_start();
  195.  
  196.                 if (isset($_GET[$this->warp->preset_var])) {
  197.                     $_SESSION['_current_preset'] = preg_replace('/[^A-Z0-9-]/i', '', $_GET[$this->warp->preset_var]);
  198.                 }
  199.  
  200.                 $config->set('_current_preset', isset($_SESSION['_current_preset']) ? $_SESSION['_current_preset'] : null);
  201.             }
  202.  
  203.             $config->set('actual_date', date('d. M Y'));
  204.         }
  205.  
  206.     }
  207.  
  208.     /*
  209.         Function: getQuery
  210.             Get current query information
  211.  
  212.         Returns:
  213.             Object
  214.     */
  215.     function getQuery() {
  216.         global $wp_query;
  217.  
  218.         // create, if not set
  219.         if (empty($this->query)) {
  220.            
  221.             // init vars
  222.             $obj   = $wp_query->get_queried_object();
  223.             $query = array();
  224.  
  225.             // find current page type
  226.             foreach (array('home', 'front_page', 'archive', 'search', 'single', 'page', 'category') as $type) {
  227.                 if (call_user_func('is_'.$type)) {
  228.                     $query[] = $type;
  229.  
  230.                     if ($type == 'page') {
  231.                         $query[] = 'page-'.$obj->ID;
  232.                     }
  233.  
  234.                     if ($type == 'category') {
  235.                         $query[] = 'cat-'.$obj->cat_ID;
  236.                     }
  237.                 }
  238.             }
  239.            
  240.             $this->query = $query;
  241.         }
  242.  
  243.         return $this->query;
  244.     }
  245.  
  246.     /*
  247.         Function: getPostCount
  248.             Retrieve current post count
  249.  
  250.         Returns:
  251.             Int
  252.     */
  253.     function getPostCount() {
  254.         global $wp_query;
  255.         return $wp_query->post_count;
  256.     }
  257.  
  258.     /*
  259.         Function: getWidget
  260.             Retrieve a widget by id
  261.  
  262.         Parameters:
  263.             $id - Widget ID
  264.  
  265.         Returns:
  266.             Object
  267.     */
  268.     function getWidget($id) {
  269.         global $wp_registered_widgets;
  270.        
  271.         $widget  = null;
  272.         $options = $this->widget_options;
  273.  
  274.         if (isset($wp_registered_widgets[$id]) && ($data = $wp_registered_widgets[$id])) {
  275.             $widget = new stdClass();
  276.            
  277.             foreach (array('id', 'name', 'classname', 'description') as $var) {
  278.                 $widget->$var = isset($data[$var]) ? $data[$var] : null;
  279.             }
  280.  
  281.             if (isset($data['callback']) && is_array($data['callback']) && ($object = current($data['callback']))) {
  282.                 if (is_a($object, 'WP_Widget')) {
  283.  
  284.                     $widget->type = $object->id_base;
  285.  
  286.                     if (isset($data['params'][0]['number'])) {
  287.  
  288.                         $number = $data['params'][0]['number'];
  289.                         $params = get_option($object->option_name);
  290.  
  291.                         if (false === $params && isset($object->alt_option_name)) {
  292.                             $params = get_option($object->alt_option_name);
  293.                         }
  294.  
  295.                         if (isset($params[$number])) {
  296.                             $widget->params = $params[$number];
  297.                         }
  298.                     }
  299.                 }
  300.             } else if ($id == 'nav_menu-0') {
  301.                 $widget->type = 'nav_menu';
  302.             }
  303.            
  304.             if (empty($widget->name)) {
  305.                 $widget->name = ucfirst($widget->type);
  306.             }
  307.  
  308.             if (empty($widget->params)) {
  309.                 $widget->params = array();
  310.             }
  311.  
  312.             $widget->options = isset($options[$id]) ? $options[$id] : array();
  313.             $widget->display = $this->displayWidget($widget);
  314.         }
  315.        
  316.         return $widget;
  317.     }
  318.  
  319.     /*
  320.         Function: getWidgets
  321.             Retrieve widgets
  322.  
  323.         Parameters:
  324.             $position - Position
  325.  
  326.         Returns:
  327.             Array
  328.     */
  329.     function getWidgets($position = null) {
  330.  
  331.         if (empty($this->widgets)) {
  332.             foreach (wp_get_sidebars_widgets() as $pos => $ids) {
  333.  
  334.                 if (!is_array($ids) || empty($ids)) {
  335.                     continue;
  336.                 }
  337.  
  338.                 $this->widgets[$pos] = array();
  339.  
  340.                 foreach ($ids as $id) {
  341.                     $this->widgets[$pos][$id] = $this->getWidget($id);
  342.                 }
  343.             }
  344.         }
  345.  
  346.         if (!is_null($position)) {
  347.             return isset($this->widgets[$position]) ? $this->widgets[$position] : array();
  348.         }
  349.    
  350.         return $this->widgets;
  351.     }
  352.  
  353.     /*
  354.         Function: displayWidget
  355.             Checks if a widget should be displayed
  356.  
  357.         Returns:
  358.             Boolean
  359.     */
  360.     function displayWidget($widget) {
  361.         if (!isset($widget->options['display']) || in_array('*', $widget->options['display'])) return true;
  362.        
  363.         foreach ($this->getQuery() as $q) {
  364.             if (in_array($q, $widget->options['display'])) {
  365.                 return true;
  366.             }
  367.         }
  368.  
  369.         return false;
  370.     }
  371.    
  372.     /*
  373.         Function: overrideConfig
  374.             Overrides default config based on page
  375.  
  376.         Returns:
  377.             Void
  378.     */
  379.     function overrideConfig() {
  380.         if (!count($this->config_overrides)) return;
  381.        
  382.         foreach ($this->getQuery() as $q) {
  383.             if (isset($this->config_overrides[$q])) {
  384.                 $this->warp->config->parseString($this->config_overrides[$q]);
  385.             }
  386.         }
  387.     }
  388.  
  389.     /*
  390.         Function: isBlog
  391.  
  392.         Returns:
  393.             Boolean
  394.     */
  395.     function isBlog() {
  396.         return true;
  397.     }
  398.  
  399.     /*
  400.         Function: isPreview
  401.             Checks for default widgets in theme preview
  402.  
  403.         Returns:
  404.             Boolean
  405.     */
  406.     function isPreview($position) {
  407.        
  408.         // preview postions
  409.         $positions = array('logo', 'right');
  410.  
  411.         return is_preview() && in_array($position, $positions);
  412.     }
  413.    
  414.     /*
  415.         Function: ajaxSearch
  416.             Ajax search callback
  417.  
  418.         Returns:
  419.             String
  420.     */
  421.     function ajaxSearch(){
  422.         global $wp_query;
  423.  
  424.         $result = array('results' => array());
  425.         $query  = isset($_REQUEST['s']) ? $_REQUEST['s']:"";
  426.  
  427.         if (strlen($query)>=3) {
  428.            
  429.             $wp_query->query_vars['s'] = $query;
  430.             $wp_query->is_search = true;
  431.  
  432.             foreach ($wp_query->get_posts() as $post) {
  433.                
  434.                 $content = !empty($post->post_excerpt) ? strip_tags(do_shortcode($post->post_excerpt)) : strip_tags(do_shortcode($post->post_content));
  435.                
  436.                 if (strlen($content) > 255) {
  437.                     $content = substr($content, 0, 254).'...';
  438.                 }
  439.                
  440.                 $result['results'][] = array(
  441.                     'title' => $post->post_title,
  442.                     'text'  => $content,
  443.                     'url'   => get_permalink($post->ID)
  444.                 );
  445.             }
  446.         }
  447.  
  448.         die(json_encode($result));
  449.     }
  450.  
  451.     /*
  452.         Function: _adminInit
  453.             Admin init actions
  454.  
  455.         Returns:
  456.             Void
  457.     */
  458.     function _adminInit() {
  459.        
  460.         if ((defined('DOING_AJAX') && DOING_AJAX) && isset($_POST['warp-ajax-save'])) {
  461.  
  462.             // update option values
  463.             foreach ($_POST as $option => $value) {
  464.                 if (preg_match('/^(warp_|'.preg_quote($this->prefix, '/').')/', $option)) {
  465.                     update_option($option, $value);
  466.                 }
  467.             }
  468.  
  469.             die();
  470.         }
  471.        
  472.         // add css/js
  473.         $path =& $this->getHelper('path');     
  474.         $info = parse_url(site_url());
  475.         $url  = sprintf('/%s/i', preg_quote($info['path'], '/'));
  476.  
  477.         if (isset($_GET['page']) && in_array($_GET['page'], array('warp', 'warp_widget'))) {
  478.             wp_enqueue_script('warp-js-admin', preg_replace($url, '', $path->url('warp:systems/wordpress/js/admin.js'), 1));
  479.         }
  480.  
  481.         wp_enqueue_style('warp-css-admin', preg_replace($url, '', $path->url('warp:systems/wordpress/css/admin.css'), 1));
  482.         wp_enqueue_script('warp-js-wp-admin', preg_replace($url, '', $path->url('warp:systems/wordpress/js/wp-admin.js'), 1));
  483.  
  484.         // add actions
  485.         add_action('wp_ajax_save_nav_settings', array($this,'_save_nav_settings'));
  486.         add_action('wp_ajax_get_nav_settings', array($this,'_get_nav_settings'));
  487.     }
  488.  
  489.     /*
  490.         Function: _adminMenu
  491.             Admin menu actions
  492.  
  493.         Returns:
  494.             Void
  495.     */
  496.     function _adminMenu() {
  497.  
  498.         // init vars
  499.         $path =& $this->getHelper('path');
  500.         $name = $this->xml->document->getElement('name');
  501.         $icon = $path->url('warp:systems/wordpress/images/yoo_icon_16.png');
  502.  
  503.         if (function_exists('add_object_page')) {
  504.             add_object_page('', $name->data(), 8, 'warp', false, $icon);
  505.         } else {
  506.             add_menu_page('', $name->data(), 8, 'warp', false, $icon);
  507.         }
  508.  
  509.         add_submenu_page('warp', 'Theme Options', 'Theme Options', 8, 'warp', array($this, '_adminThemeOptions'));
  510.         add_submenu_page('warp', 'Widget Options', 'Widget Options', 8, 'warp_widget', array($this, '_adminWidgetOptions'));
  511.     }
  512.  
  513.     /*
  514.         Function: _adminThemeOptions
  515.             Render admin theme options layout
  516.  
  517.         Returns:
  518.             Void
  519.     */ 
  520.     function _adminThemeOptions() {
  521.        
  522.         // init vars
  523.         $path  =& $this->getHelper('path');
  524.         $xml   =& $this->getHelper('xml');
  525.         $http  =& $this->getHelper('http');
  526.         $check =& $this->getHelper('checksum');
  527.        
  528.         // get warp xml
  529.         $warp_xml = $xml->load($path->path('warp:warp.xml'), 'xml', true);
  530.  
  531.         // update check
  532.         $update = null;
  533.         if ($url = $warp_xml->document->getElement('updateUrl')) {
  534.  
  535.             // get template info
  536.             $template = get_template();
  537.             $version  = $this->xml->document->getElement('version');
  538.             $url      = sprintf('%s?application=%s&version=%s&format=raw', $url->data(), $template, $version->data());
  539.            
  540.             // only check once a day
  541.             if (get_option($this->prefix.'update_check') != date('Y-m-d').' '.$version->data()) {
  542.                 if ($request = $http->get($url)) {
  543.                     update_option($this->prefix.'update_check', date('Y-m-d').' '.$version->data());
  544.                     update_option($this->prefix.'update_data', $request['body']);
  545.                 }
  546.             }
  547.  
  548.             // decode update response
  549.             $update = json_decode(get_option($this->prefix.'update_data'));
  550.         }
  551.  
  552.         // verify theme files
  553.         if (($checksums = $path->path('template:checksums')) && filesize($checksums)) {
  554.             $check->verify($path->path('template:'), $log);
  555.         } else {
  556.             $log = false;
  557.         }
  558.  
  559.         echo $this->warp->template->render('admin/theme_options', array('xml' => $this->xml, 'warp_xml' => $warp_xml, 'update' => $update, 'checklog' => $log));
  560.     }
  561.  
  562.     /*
  563.         Function: _adminWidgetOptions
  564.             Render admin widget options layout
  565.  
  566.         Returns:
  567.             Void
  568.     */ 
  569.     function _adminWidgetOptions() {
  570.        
  571.         // get position settings
  572.         $position_settings = $this->warp->config->get('warp.positions');
  573.        
  574.         // get module settings
  575.         $module_settings = array();
  576.         $settings = $this->xml->document->getElement('modulesettings');
  577.  
  578.         foreach ($settings->children() as $setting) {
  579.             $module_settings[$setting->attributes('name')] = $setting;
  580.         }
  581.        
  582.         echo $this->warp->template->render('admin/widget_options', compact('position_settings', 'module_settings'));
  583.     }
  584.    
  585.     /*
  586.         Function: getMenuItemOptions
  587.             Retrieve menu by id
  588.  
  589.         Parameters:
  590.             $id - Menu Item ID
  591.  
  592.         Returns:
  593.             Array
  594.     */
  595.     function getMenuItemOptions($id) {
  596.        
  597.         $menu_settings = array(
  598.             'columns'     => 1,
  599.             'columnwidth' => -1,
  600.             'image'       => ''
  601.         );
  602.        
  603.         if (isset($this->menu_item_options[$id])) {
  604.             $menu_settings = array_merge($menu_settings, $this->menu_item_options[$id]);
  605.         }
  606.        
  607.         return $menu_settings;
  608.     }
  609.    
  610.    
  611.     /*
  612.         Function: _save_nav_settings
  613.             Saves menu item settings
  614.  
  615.         Returns:
  616.             Void
  617.     */ 
  618.     function _save_nav_settings() {
  619.        
  620.         if (isset($_POST['menu-item'])) {
  621.            
  622.             $menu_item_settings = $this->menu_item_options;
  623.            
  624.             foreach ($_POST['menu-item'] as $itemId=>$settings){
  625.                 $menu_item_settings[$itemId] = $settings;
  626.             }
  627.            
  628.             update_option($this->prefix.'menu-items', $menu_item_settings);
  629.             $this->menu_item_options = $menu_item_settings;
  630.         }
  631.        
  632.         die();
  633.     }
  634.    
  635.     /*
  636.         Function: _get_nav_settings
  637.             Returns menu item settings as json
  638.  
  639.         Returns:
  640.             Boolean
  641.     */
  642.     function _get_nav_settings() {
  643.         die(json_encode($this->menu_item_options));
  644.     }
  645.  
  646. }
  647.  
  648. /*
  649.     Function: mb_strpos
  650.         mb_strpos function for servers not using the multibyte string extension
  651. */
  652. if (!function_exists('mb_strpos')) {
  653.     function mb_strpos($haystack, $needle, $offset = 0) {
  654.         return strpos($haystack, $needle, $offset);
  655.     }
  656. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement