Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.63 KB | None | 0 0
  1. <?php
  2.  
  3. class SystemWarpHelper extends WarpHelper {
  4.  
  5. /* system path */
  6. public $path;
  7.  
  8. /* system url */
  9. public $url;
  10.  
  11. /* cache path */
  12. public $cache_path;
  13.  
  14. /* cache time */
  15. public $cache_time;
  16.  
  17. /* configuration */
  18. public $config;
  19.  
  20. /* theme xml */
  21. public $xml;
  22.  
  23. /* query */
  24. public $query;
  25.  
  26. /* all menu items options */
  27. public $menu_item_options;
  28.  
  29. /*
  30. Function: Constructor
  31. Class Constructor.
  32. */
  33. public function __construct() {
  34. parent::__construct();
  35.  
  36. // init vars
  37. $this->path = rtrim(str_replace(DIRECTORY_SEPARATOR, '/', ABSPATH), '/');
  38. $this->url = rtrim(site_url(), '/');
  39. $this->cache_path = rtrim(str_replace(DIRECTORY_SEPARATOR, '/', get_template_directory()), '/').'/cache';
  40. $this->cache_time = 86400;
  41. $this->menu_item_options = $this['option']->get('menu-items', array());
  42.  
  43. // get config
  44. if (defined('WP_ALLOW_MULTISITE') && WP_ALLOW_MULTISITE) {
  45. $settings = $this['option']->get('warp_theme_options', array());
  46. } else {
  47. $settings = ($file = $this['path']->path('template:config')) ? file_get_contents($file) : array();
  48. }
  49.  
  50. // set config or load defaults
  51. if (count($settings)) {
  52. $this->config = $this['data']->create($settings);
  53. } else {
  54. $this->config = $this['data']->create(file_get_contents($this['path']->path('template:config.default')));
  55. }
  56.  
  57. // set cache directory
  58. if (!file_exists($this->cache_path)) {
  59. mkdir($this->cache_path, 0755);
  60. }
  61. }
  62.  
  63. /*
  64. Function: init
  65. Initialize system configuration
  66.  
  67. Returns:
  68. Void
  69. */
  70. public function init() {
  71.  
  72. // set paths
  73. $this['path']->register($this->path.'/wp-admin', 'admin');
  74. $this['path']->register($this->path, 'site');
  75. $this['path']->register($this->cache_path, 'cache');
  76. $this['path']->register($this['path']->path('warp:systems/wordpress/menus'), 'menu');
  77. $this['path']->register($this['path']->path('warp:systems/wordpress/widgets'), 'widgets');
  78. $this['path']->register($this['path']->path('template:').'/widgets', 'widgets');
  79.  
  80. // set theme support
  81. add_theme_support('post-thumbnails');
  82. add_theme_support('woocommerce');
  83.  
  84. // set translations
  85. load_theme_textdomain('warp', $this['path']->path('template:languages'));
  86.  
  87. // get theme xml
  88. $this->xml = $this['dom']->create($this['path']->path('template:template.xml'), 'xml');
  89.  
  90. // get module positions
  91. foreach ($this->xml->find('positions > position') as $position) {
  92. $this['modules']->register($position->text());
  93. }
  94.  
  95. // load widgets
  96. foreach ($this['path']->dirs('widgets:') as $name) {
  97. if ($file = $this['path']->path("widgets:{$name}/{$name}.php")) {
  98. require_once($file);
  99. }
  100. }
  101.  
  102. // add actions
  103. add_action('wp_ajax_warp_search', array($this, 'ajaxSearch'));
  104. add_action('wp_ajax_nopriv_warp_search', array($this, 'ajaxSearch'));
  105.  
  106. // register main menu
  107. register_nav_menus(array('main_menu' => 'Main Navigation Menu'));
  108.  
  109. // is admin or site
  110. if (is_admin()) {
  111.  
  112. // set paths
  113. $this['path']->register($this['path']->path('warp:config'), 'config');
  114. $this['path']->register($this['path']->path('warp:systems/wordpress/config'), 'config');
  115.  
  116. // add actions
  117. add_action('admin_init', array($this, '_adminInit'));
  118. add_action('admin_menu', array($this, '_adminMenu'));
  119.  
  120. // add notices
  121. if (isset($_GET['page']) && in_array($_GET['page'], array('warp', 'warp_widget'))) {
  122. add_action('admin_notices', array($this, '_adminNotices'));
  123. }
  124.  
  125. } else {
  126.  
  127. // add action
  128. add_action('wp', array($this, '_wp'));
  129. add_action("get_sidebar", array($this, '_get_sidebar'));
  130.  
  131. // remove auto-linebreaks ?
  132. if (!$this->config->get('wpautop', 1)) {
  133. remove_filter('the_content', 'wpautop');
  134. }
  135.  
  136. // set custom menu walker
  137. add_filter('wp_nav_menu_args', create_function('$args','if (empty($args["walker"])) $args["walker"] = new WarpMenuWalker();return $args;'));
  138.  
  139. // filter widgets that should not be displayed
  140. add_filter('widget_display_callback', create_function('$instance,$widget,$args','$warp = Warp::getInstance(); return $warp["widgets"]->get($widget->id)->display ? $instance : false;'), 10, 3);
  141.  
  142. // disable the admin bar for mobiles
  143. if ($this->config->get('mobile') && $this['browser']->isMobile()) {
  144. add_theme_support('admin-bar', array('callback' => '__return_false'));
  145. }
  146. }
  147. }
  148.  
  149. /*
  150. Function: getQuery
  151. Get current query information
  152.  
  153. Returns:
  154. Object
  155. */
  156. public function getQuery() {
  157. global $wp_query;
  158.  
  159. // create, if not set
  160. if (empty($this->query)) {
  161.  
  162. // init vars
  163. $obj = $wp_query->get_queried_object();
  164. $query = array();
  165.  
  166. // find current page type
  167. foreach (array('home', 'front_page', 'archive', 'search', 'single', 'page', 'category') as $type) {
  168. if (call_user_func('is_'.$type)) {
  169. $query[] = $type;
  170.  
  171. if ($type == 'page') {
  172. $query[] = 'page-'.$obj->ID;
  173. }
  174.  
  175. if ($type == 'single') {
  176. $query[] = $obj->post_type;
  177. $query[] = $obj->post_type.'-'.$obj->ID;
  178. }
  179.  
  180. if ($type == 'category') {
  181. $query[] = 'cat-'.$obj->cat_ID;
  182. }
  183.  
  184. if ($type == 'archive') {
  185. $query[] = 'cat-'.$obj->term_id;
  186. }
  187.  
  188. // WPML support
  189. if (defined('ICL_LANGUAGE_CODE') && ICL_LANGUAGE_CODE != ($lang = icl_get_default_language())) {
  190.  
  191. if ($type == 'page') {
  192. $query[] = 'page-'.icl_object_id($obj->ID, $type, true, $lang);
  193. }
  194.  
  195. if ($type == 'category') {
  196.  
  197. $query[] = 'cat-'.icl_object_id($obj->term_id, $type, true, $lang);
  198. }
  199. }
  200. }
  201. }
  202.  
  203. $this->query = $query;
  204. }
  205.  
  206. return $this->query;
  207. }
  208.  
  209. /*
  210. Function: getPostCount
  211. Retrieve current post count
  212.  
  213. Returns:
  214. Int
  215. */
  216. public function getPostCount() {
  217. global $wp_query;
  218. return $wp_query->post_count;
  219. }
  220.  
  221. /*
  222. Function: isBlog
  223.  
  224. Returns:
  225. Boolean
  226. */
  227. public function isBlog() {
  228. return true;
  229. }
  230.  
  231. /*
  232. Function: isPreview
  233. Checks for default widgets in theme preview
  234.  
  235. Returns:
  236. Boolean
  237. */
  238. public function isPreview($position) {
  239.  
  240. // preview postions
  241. $positions = array('logo', 'right');
  242.  
  243. return is_preview() && in_array($position, $positions);
  244. }
  245.  
  246. /*
  247. Function: ajaxSearch
  248. Ajax search callback
  249.  
  250. Returns:
  251. String
  252. */
  253. public function ajaxSearch(){
  254. global $wp_query;
  255.  
  256. $result = array('results' => array());
  257. $query = isset($_REQUEST['s']) ? $_REQUEST['s']:"";
  258.  
  259. if (strlen($query) >= 3) {
  260.  
  261. $wp_query->query_vars['posts_per_page'] = $this->config->get('search_results', 5);
  262. $wp_query->query_vars['post_status'] = 'publish';
  263. $wp_query->query_vars['s'] = $query;
  264. $wp_query->is_search = true;
  265.  
  266. foreach ($wp_query->get_posts() as $post) {
  267.  
  268. $content = !empty($post->post_excerpt) ? strip_tags(do_shortcode($post->post_excerpt)) : strip_tags(do_shortcode($post->post_content));
  269.  
  270. if (strlen($content) > 180) {
  271. $content = substr($content, 0, 179).'...';
  272. }
  273.  
  274. $result['results'][] = array(
  275. 'title' => $post->post_title,
  276. 'text' => $content,
  277. 'url' => get_permalink($post->ID)
  278. );
  279. }
  280. }
  281.  
  282. die(json_encode($result));
  283. }
  284.  
  285. /*
  286. Function: _wp
  287. WP action callback
  288.  
  289. Returns:
  290. Void
  291. */
  292. public function _wp() {
  293.  
  294. // set config
  295. $this->config->set('language', get_bloginfo("language"));
  296. $this->config->set('direction', $GLOBALS['wp_locale']->is_rtl() ? 'rtl' : 'ltr');
  297. $this->config->set('site_url', rtrim(get_bloginfo('url'), '/'));
  298. $this->config->set('site_name', get_option('blogname'));
  299. $this->config->set('datetime', date('Y-m-d'));
  300. $this->config->set('actual_date', date_i18n($this->config->get('date_format', 'l, j F Y')));
  301. $this->config->set('page_class', implode(' ', array_map(create_function('$element','return "wp-".$element;'), $this->getQuery())));
  302.  
  303. // outdated browser page ?
  304. if (($this['config']->get('ie6page') && $this['browser']->isIE6()) || ($this['config']->get('outdated_browser') && $this['browser']->outdatedBrowser())) {
  305. $this['event']->bind('render.layouts:template', create_function('&$layout,&$args', '$args["title"] = __("Please update to a modern browser", "warp"); $args["error"] = "browser"; $args["message"] = __("outdatedBrowser_page_message", "warp"); $layout = "layouts:error";'));
  306. }
  307.  
  308. // mobile theme ?
  309. if ($this['config']->get('mobile') && $this['browser']->isMobile()) {
  310. $this['config']->set('style', 'mobile');
  311. }
  312.  
  313. // branding ?
  314. if ($this['config']->get('warp_branding')) {
  315. $this['template']->set('warp_branding', $this->warp->getBranding());
  316. }
  317.  
  318. // set theme style paths
  319. if ($style = $this['config']->get('style')) {
  320. foreach (array('css' => 'template:styles/%s/css', 'js' => 'template:styles/%s/js', 'layouts' => 'template:styles/%s/layouts') as $name => $resource) {
  321. if ($p = $this['path']->path(sprintf($resource, $style))) {
  322. $this['path']->register($p, $name);
  323. }
  324. }
  325. }
  326. }
  327.  
  328. /*
  329. Function: _adminInit
  330. Admin init action callback
  331.  
  332. Returns:
  333. Void
  334. */
  335. public function _adminInit() {
  336.  
  337. if (defined('DOING_AJAX') && DOING_AJAX && isset($_POST['task'], $_POST['warp-ajax-save'])) {
  338.  
  339. $message = 'failed';
  340. $post = function_exists('wp_magic_quotes') ? array_map('stripslashes_deep', $_POST) : $_POST;
  341.  
  342. switch ($post['task']) {
  343.  
  344. case 'theme-options':
  345.  
  346. // update theme config
  347. $config = isset($post['config']) ? $post['config'] : array();
  348. $config = array_merge($config, array('profile_data' => isset($post['profile_data']) ? $post['profile_data'] : array()));
  349. $config = array_merge($config, array('profile_map' => isset($post['profile_map']) ? $post['profile_map'] : array()));
  350.  
  351. if (defined('WP_ALLOW_MULTISITE') && WP_ALLOW_MULTISITE) {
  352.  
  353. if ($this['option']->set('warp_theme_options', (string) $this['data']->create($config))) {
  354. $message = 'success';
  355. }
  356.  
  357. } else {
  358.  
  359. if (file_put_contents($this['path']->path('template:').'/config', (string) $this['data']->create($config))) {
  360. $message = 'success';
  361. }
  362. }
  363.  
  364. break;
  365.  
  366. case 'widget-options':
  367.  
  368. // update widget options
  369. if (update_option('warp_widget_options', $post['warp_widget_options'])) {
  370. $message = 'success';
  371. }
  372.  
  373. break;
  374. }
  375.  
  376. die(json_encode(compact('message')));
  377. }
  378.  
  379. // add css/js
  380. $siteurl = sprintf('/%s/i', preg_quote(parse_url(site_url(), PHP_URL_PATH), '/'));
  381.  
  382. if (isset($_GET['page']) && in_array($_GET['page'], array('warp', 'warp_widget'))) {
  383. wp_enqueue_style('warp-css-config', preg_replace($siteurl, '', $this['path']->url('config:css/config.css'), 1));
  384. wp_enqueue_script('warp-js-config', preg_replace($siteurl, '', $this['path']->url('config:js/config.js'), 1));
  385. wp_enqueue_script('warp-js-admin', preg_replace($siteurl, '', $this['path']->url('config:js/admin.js'), 1));
  386. }
  387.  
  388. wp_enqueue_style('warp-css-admin', preg_replace($siteurl, '', $this['path']->url('config:css/admin.css'), 1));
  389. wp_enqueue_script('warp-js-wp-admin', preg_replace($siteurl, '', $this['path']->url('config:js/wp-admin.js'), 1));
  390.  
  391. // add actions
  392. add_action('wp_ajax_save_nav_settings', array($this,'_save_nav_settings'));
  393. add_action('wp_ajax_get_nav_settings', array($this,'_get_nav_settings'));
  394. }
  395.  
  396. /*
  397. Function: _adminNotices
  398. Admin notices action callback
  399.  
  400. Returns:
  401. Void
  402. */
  403. public function _adminNotices() {
  404.  
  405. // get warp xml
  406. $xml = $this['dom']->create($this['path']->path('warp:warp.xml'), 'xml');
  407.  
  408. // cache writable ?
  409. if (!file_exists($this->cache_path) || !is_writable($this->cache_path)) {
  410. $update['cache'] = "Cache not writable, please check directory permissions ({$this->cache_path})";
  411. }
  412.  
  413. // update check
  414. if ($url = $xml->first('updateUrl')->text()) {
  415.  
  416. // create check urls
  417. $urls['tmpl'] = sprintf('%s?application=%s&version=%s&format=raw', $url, get_template(), $this->xml->first('version')->text());
  418. $urls['warp'] = sprintf('%s?application=%s&version=%s&format=raw', $url, 'warp', $xml->first('version')->text());
  419.  
  420. foreach ($urls as $type => $url) {
  421.  
  422. // only check once a day
  423. $hash = md5($url.date('Y-m-d'));
  424. if ($this['option']->get("{$type}_check") != $hash) {
  425. if ($request = $this['http']->get($url)) {
  426. $this['option']->set("{$type}_check", $hash);
  427. $this['option']->set("{$type}_data", $request['body']);
  428. }
  429. }
  430.  
  431. // decode response and set message
  432. if (($data = json_decode($this['option']->get("{$type}_data"))) && $data->status == 'update-available') {
  433. $update[$type] = $data->message;
  434. }
  435.  
  436. }
  437. }
  438.  
  439. // show notice
  440. if (!empty($update)) {
  441. echo '<div class="update-nag">'.implode('<br>', $update).'</div>';
  442. }
  443.  
  444. return false;
  445. }
  446.  
  447. /*
  448. Function: _adminMenu
  449. Admin menu action callback
  450.  
  451. Returns:
  452. Void
  453. */
  454. public function _adminMenu() {
  455.  
  456. // init vars
  457. $name = $this->xml->first('name')->text();
  458. $icon = $this['path']->url('config:images/yoo_icon_16.png');
  459.  
  460. add_menu_page('', $name, apply_filters('warp_edit_theme_options', 'edit_theme_options'), 'warp', false, $icon);
  461.  
  462. add_submenu_page('warp', 'Theme Options', 'Theme Options', apply_filters('warp_edit_theme_options', 'edit_theme_options'), 'warp', array($this, '_adminThemeOptions'));
  463. add_submenu_page('warp', 'Widget Options', 'Widget Options', apply_filters('warp_edit_theme_options', 'edit_theme_options'), 'warp_widget', array($this, '_adminWidgetOptions'));
  464. }
  465.  
  466. /*
  467. Function: _adminThemeOptions
  468. Render admin theme options layout
  469.  
  470. Returns:
  471. Void
  472. */
  473. public function _adminThemeOptions() {
  474. echo $this['template']->render('config:layouts/theme_options', array('xml' => $this->xml));
  475. }
  476.  
  477. /*
  478. Function: _adminWidgetOptions
  479. Render admin widget options layout
  480.  
  481. Returns:
  482. Void
  483. */
  484. public function _adminWidgetOptions() {
  485.  
  486. // get module settings
  487. $module_settings = $this->xml->find('modulesettings > setting');
  488.  
  489. // get position settings
  490. $position_settings = array();
  491.  
  492. foreach ($this->xml->find('positions > position') as $position) {
  493. $position_settings[$position->text()] = $position;
  494. }
  495.  
  496. echo $this['template']->render('config:layouts/widget_options', compact('position_settings', 'module_settings'));
  497. }
  498.  
  499. /*
  500. Function: getMenuItemOptions
  501. Retrieve menu by id
  502.  
  503. Parameters:
  504. $id - Menu Item ID
  505.  
  506. Returns:
  507. Array
  508. */
  509. public function getMenuItemOptions($id) {
  510. return isset($this->menu_item_options[$id]) ? $this->menu_item_options[$id] : array();
  511. }
  512.  
  513. /*
  514. Function: _save_nav_settings
  515. Saves menu item settings
  516.  
  517. Returns:
  518. Void
  519. */
  520. public function _save_nav_settings() {
  521.  
  522. if (isset($_POST['menu-item'])) {
  523.  
  524. $menu_item_settings = $this->menu_item_options;
  525.  
  526. foreach ($_POST['menu-item'] as $itemId=>$settings) {
  527. $menu_item_settings[$itemId] = $settings;
  528. }
  529.  
  530. $this['option']->set('menu-items', $menu_item_settings);
  531. $this->menu_item_options = $menu_item_settings;
  532. }
  533.  
  534. die();
  535. }
  536.  
  537. /*
  538. Function: _get_nav_settings
  539. Returns menu item settings as json
  540.  
  541. Returns:
  542. Boolean
  543. */
  544. public function _get_nav_settings() {
  545. die(json_encode($this->menu_item_options));
  546. }
  547.  
  548. /*
  549. Function: _get_sidebar
  550. Catches default sidebar content and makes it available for the sidebar widget
  551.  
  552. Returns:
  553. Void
  554. */
  555. public function _get_sidebar($name=null) {
  556.  
  557. $templates = isset($name) ? array("sidebar-{$name}.php", "sidebar.php") : array("sidebar.php");
  558.  
  559. ob_start();
  560.  
  561. if ("" == locate_template($templates, true, true)) {
  562. load_template(ABSPATH.WPINC."/theme-compat/sidebar.php", true);
  563. $clear = true;
  564. }
  565.  
  566. $output = ob_get_clean();
  567.  
  568. if (isset($clear)) {
  569. $output = "";
  570. }
  571.  
  572. $this["template"]->set("sidebar.output", $output);
  573. }
  574.  
  575. }
  576.  
  577. /*
  578. Class: WarpMenuWalker
  579. Custom Menu Walker
  580. */
  581. class WarpMenuWalker extends Walker_Nav_Menu {
  582.  
  583. public function start_lvl(&$output, $depth) {
  584. $output .= '<ul>';
  585. }
  586.  
  587. public function end_lvl(&$output, $depth) {
  588. $output .= '</ul>';
  589. }
  590.  
  591. public function start_el(&$output, $item, $depth, $args) {
  592.  
  593. // get warp
  594. $warp = Warp::getInstance();
  595.  
  596. // init vars
  597. $data = array();
  598. $classes = empty($item->classes) ? array() : (array) $item->classes;
  599. $options = $warp['system']->getMenuItemOptions($item->ID);
  600.  
  601. // set id
  602. $data['data-id'] = $item->ID;
  603.  
  604. // is current item ?
  605. if (in_array('current-menu-item', $classes) || in_array('current_page_item', $classes)) {
  606. $data['data-menu-active'] = 2;
  607.  
  608. // home/fronpage item
  609. } elseif ($item->url == 'index.php' && (is_home() || is_front_page())) {
  610. $data['data-menu-active'] = 2;
  611. }
  612.  
  613. // has columns ?
  614. if (!empty($options['columns'])) {
  615. $data['data-menu-columns'] = (int) $options['columns'];
  616. }
  617.  
  618. // has columnwidth ?
  619. if (!empty($options['columnwidth'])) {
  620. $data['data-menu-columnwidth'] = (int) $options['columnwidth'];
  621. }
  622.  
  623. // has image ?
  624. if (!empty($options['image'])) {
  625. $upload = wp_upload_dir();
  626. $data['data-menu-image'] = trailingslashit($upload['baseurl']).$options['image'];
  627. }
  628.  
  629. // set item attributes
  630. $attributes = '';
  631. foreach ($data as $name => $value) {
  632. $attributes .= sprintf(' %s="%s"', $name, esc_attr($value));
  633. }
  634.  
  635. // create item output
  636. $id = apply_filters('nav_menu_item_id', '', $item, $args);
  637. $output .= '<li'.(strlen($id) ? sprintf(' id="%s"', esc_attr($id)) : '').$attributes.'>';
  638.  
  639. // set link attributes
  640. $attributes = '';
  641. foreach (array('attr_title' => 'title', 'target' => 'target', 'xfn' => 'rel', 'url' => 'href') as $var => $attr) {
  642. if (!empty($item->$var)) {
  643. $attributes .= sprintf(' %s="%s"', $attr, esc_attr($item->$var));
  644. }
  645. }
  646.  
  647. // escape link title
  648. $item->title = htmlspecialchars($item->title, ENT_COMPAT, "UTF-8");
  649.  
  650. // is separator ?
  651. if ($item->url == '#') {
  652. $format = '%s<span%s><span>%s</span></span>%s';
  653. $attributes = ' class="separator"';
  654. } else {
  655. $format = '%s<a%s><span>%s</span></a>%s';
  656. }
  657.  
  658. // create link output
  659. $item_output = sprintf($format, $args->before, $attributes, $args->link_before.apply_filters('the_title', $item->title, $item->ID).$args->link_after, $args->after);
  660.  
  661. $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
  662. }
  663.  
  664. public function end_el(&$output, $item, $depth) {
  665. $output .= '</li>';
  666. }
  667.  
  668. }
  669.  
  670.  
  671. /*
  672. Function: mb_strpos
  673. mb_strpos function for servers not using the multibyte string extension
  674. */
  675. if (!function_exists('mb_strpos')) {
  676. function mb_strpos($haystack, $needle, $offset = 0) {
  677. return strpos($haystack, $needle, $offset);
  678. }
  679. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement