Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.01 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Deals with the basic initialization and core methods for theme
  5. *
  6. * @package Bunyad
  7. */
  8. class Bunyad_Core
  9. {
  10. private $cache = array(
  11. 'body_classes' => array()
  12. );
  13.  
  14. public function init($options = array())
  15. {
  16. $this->cache['options'] = $options;
  17.  
  18. // Register Sphere Core plugin components options
  19. add_filter('sphere_plugin_components', array($this, '_sphere_components'));
  20.  
  21. // Pre-initialization hook
  22. do_action('bunyad_core_pre_init', $this);
  23.  
  24. /*
  25. * Setup framework internal functionality
  26. */
  27. add_filter('bunyad-active-widgets', array($this, '_filter_widgets'));
  28.  
  29. // initialize options and add to cache
  30. Bunyad::options()->set_config(array_merge(
  31. array(
  32. 'meta_prefix' => '_' . strtolower($options['theme_name']), // _ underscore hides in custom fields
  33. 'theme_prefix' => strtolower($options['theme_name'])
  34. ),
  35. $options
  36. ))->init();
  37.  
  38. if (isset($options['options']) && is_array($options['options'])) {
  39. Bunyad::options()->set($options['options']);
  40. }
  41.  
  42. // initialize admin
  43. if (is_admin()) {
  44. $this->init_admin($options);
  45. }
  46.  
  47. // init menu helper classes
  48. Bunyad::menus();
  49.  
  50. // default to no sidebar
  51. $this->set_sidebar(Bunyad::options()->default_sidebar);
  52.  
  53. // set default style
  54. $this->add_body_class(Bunyad::options()->layout_style);
  55.  
  56. /*
  57. * Add theme related functionality using the after_setup_theme hook
  58. */
  59. add_action('after_setup_theme', array($this, 'setup'), 11);
  60.  
  61. // Post-initialization hook
  62. do_action('bunyad_core_post_init', $this);
  63.  
  64. return $this;
  65. }
  66.  
  67. /**
  68. * Action callback: Setup theme related functionality at after_setup_theme hook
  69. */
  70. public function setup()
  71. {
  72. $options = $this->cache['options'];
  73.  
  74. // theme options
  75. add_theme_support('post-thumbnails');
  76. add_theme_support('automatic-feed-links');
  77. add_theme_support('html5');
  78.  
  79. add_theme_support('post-formats', $options['post_formats']);
  80.  
  81. // add body class filter
  82. add_filter('body_class', array($this, '_add_body_classes'));
  83.  
  84. // add filter for excerpt
  85. add_filter('excerpt_more', array(Bunyad::posts(), 'excerpt_read_more'));
  86. add_filter('the_content_more_link', array(Bunyad::posts(), 'excerpt_read_more'));
  87.  
  88. // add support for shortcodes in text widget
  89. add_filter('widget_text', 'do_shortcode');
  90.  
  91. // setup shortcode configs - for bunyad shortcode plugin
  92. if (is_array($options['shortcode_config'])) {
  93. $this->shortcode_config();
  94. //add_action('after_setup_theme', array($this, 'shortcode_config'));
  95. }
  96.  
  97. // fix title on home page - with SEO plugins compatibilty
  98. add_filter('wp_title', array($this, '_fix_home_title'));
  99.  
  100. // fix header sidebar
  101. add_action('get_header', array($this, '_set_header_options'));
  102.  
  103. add_action('wp_head', array($this, '_add_header_code'), 100);
  104. add_action('wp_footer', array($this, '_add_footer_code'), 100);
  105.  
  106. // add inline css
  107. add_action('wp_enqueue_scripts', array($this, '_add_inline_css'), 200);
  108. }
  109.  
  110. /**
  111. * Filter: Active widgets when Bunyad Widgets is enabled
  112. *
  113. * @param array $widgets
  114. */
  115. public function _filter_widgets($widgets)
  116. {
  117. return $this->cache['options']['widgets'];
  118. }
  119.  
  120. /**
  121. * Filter: Sphere Core plugin components
  122. *
  123. * @param array $components
  124. * @see Sphere_Plugin_Core::setup()
  125. * @return array
  126. */
  127. public function _sphere_components($components)
  128. {
  129. if (isset($this->cache['options']['sphere_components'])) {
  130. return $this->cache['options']['sphere_components'];
  131. }
  132.  
  133. return $components;
  134. }
  135.  
  136. /**
  137. * Initialize admin related classes
  138. */
  139. private function init_admin($options)
  140. {
  141. add_action('admin_enqueue_scripts', array($this, '_admin_assets'));
  142.  
  143. Bunyad::factory('admin/options');
  144. Bunyad::factory('admin/meta-boxes');
  145. Bunyad::factory('admin/importer');
  146. }
  147.  
  148. // callback function for assets
  149. public function _admin_assets()
  150. {
  151. wp_enqueue_style('bunyad-base', get_template_directory_uri() . '/admin/css/base.css');
  152. }
  153.  
  154. /**
  155. * Set current layout sidebar
  156. *
  157. * @param string $type none or right
  158. */
  159. public function set_sidebar($type)
  160. {
  161. $this->cache['sidebar'] = $type;
  162.  
  163. if ($type == 'right') {
  164. $this->add_body_class('right-sidebar');
  165. $this->remove_body_class('no-sidebar');
  166. }
  167. else {
  168. $this->remove_body_class('right-sidebar');
  169. $this->add_body_class('no-sidebar');
  170. }
  171.  
  172. return $this;
  173. }
  174.  
  175. /**
  176. * Get current active sidebar value outside
  177. */
  178. public function get_sidebar()
  179. {
  180. if (!array_key_exists('sidebar', $this->cache)) {
  181. return (string) Bunyad::options()->default_sidebar;
  182. }
  183.  
  184. return $this->cache['sidebar'];
  185. }
  186.  
  187. /**
  188. * Include main sidebar
  189. *
  190. * @see get_sidebar()
  191. */
  192. public function theme_sidebar()
  193. {
  194. if ($this->get_sidebar() !== 'none') {
  195. get_sidebar();
  196. }
  197.  
  198. return $this;
  199. }
  200.  
  201. /**
  202. * Callback: Set the relevant header options for the theme such as sidebar
  203. */
  204. public function _set_header_options()
  205. {
  206. // posts, pages, attachments etc.
  207. if (is_singular()) {
  208.  
  209. wp_enqueue_script('comment-reply', null, null, null, true);
  210.  
  211. // set layout
  212. $layout = Bunyad::posts()->meta('layout_style');
  213.  
  214. if ($layout) {
  215. $this->set_sidebar(($layout == 'full' ? 'none' : $layout));
  216. }
  217. }
  218. }
  219.  
  220. /**
  221. * Add a custom class to body - MUST be called before get_header() in theme
  222. *
  223. * @param string $class
  224. */
  225. public function add_body_class($class)
  226. {
  227. $this->cache['body_classes'][] = esc_attr($class);
  228. return $this;
  229. }
  230.  
  231. /**
  232. * Remove body class - MUST be called before get_header() in theme
  233. */
  234. public function remove_body_class($class)
  235. {
  236. foreach ($this->cache['body_classes'] as $key => $value) {
  237. if ($value === $class) {
  238. unset($this->cache['body_classes'][$key]);
  239. }
  240. }
  241.  
  242. return $this;
  243. }
  244.  
  245. /**
  246. * Action callback: Set up shortcode configs for the shortcodes plugin
  247. */
  248. public function shortcode_config()
  249. {
  250. // initialize shortcodes
  251. if (is_object(Bunyad::codes())) {
  252. Bunyad::codes()->set_config((array) $this->cache['options']['shortcode_config']);
  253. }
  254. }
  255.  
  256. /**
  257. * Filter callback: Add stored classes to the body
  258. */
  259. public function _add_body_classes($classes)
  260. {
  261. return array_merge($classes, $this->cache['body_classes']);
  262. }
  263.  
  264. /**
  265. * Filter callback: Fix home title - stay compatible with SEO plugins
  266. */
  267. public function _fix_home_title($title = '')
  268. {
  269. if (!is_front_page() && !is_home()) {
  270. return $title;
  271. }
  272.  
  273. // modify only if empty
  274. if (empty($title)) {
  275. $title = get_bloginfo('name');
  276. $description = get_bloginfo('description');
  277.  
  278. if ($description) {
  279. $title .= ' &mdash; ' . $description;
  280. }
  281. }
  282.  
  283. return $title;
  284. }
  285.  
  286. /**
  287. * Queue inline CSS to be added to the header
  288. *
  289. * @param string $script
  290. * @param mixed $data
  291. * @see wp_add_inline_style
  292. */
  293. public function enqueue_css($script, $data)
  294. {
  295. $this->cache['inline_css'][$script] = $data;
  296. }
  297.  
  298. /**
  299. * Action callback: Add header from theme settings
  300. */
  301. public function _add_header_code()
  302. {
  303. if (Bunyad::options()->header_custom_code) {
  304. echo Bunyad::options()->header_custom_code;
  305. }
  306. }
  307.  
  308. /**
  309. * Action callback: Add footer from theme settings
  310. */
  311. public function _add_footer_code()
  312. {
  313. if (Bunyad::options()->footer_custom_code) {
  314. echo Bunyad::options()->footer_custom_code;
  315. }
  316. }
  317.  
  318. /**
  319. * Add any inline CSS that was enqueued properly using wp_add_inline_style()
  320. */
  321. public function _add_inline_css()
  322. {
  323. if (!array_key_exists('inline_css', $this->cache)) {
  324. return;
  325. }
  326.  
  327. foreach ($this->cache['inline_css'] as $script => $data) {
  328. wp_add_inline_style($script, $data);
  329. }
  330. }
  331.  
  332. /**
  333. * Breadcrumbs
  334. *
  335. * Adapted from http://dimox.net/wordpress-breadcrumbs-without-a-plugin/
  336. */
  337. public function breadcrumbs($options = array())
  338. {
  339. global $post;
  340.  
  341. // preliminary checks
  342. if (Bunyad::options()->disable_breadcrumbs) {
  343. return;
  344. }
  345.  
  346. $text['home'] = _x('Home', 'breadcrumbs', 'bunyad'); // text for the 'Home' link
  347. $text['category'] = __('Category: "%s"', 'bunyad'); // text for a category page
  348. $text['tax'] = __('Archive for "%s"', 'bunyad'); // text for a taxonomy page
  349. $text['search'] = __('Search Results for "%s" Query', 'bunyad'); // text for a search results page
  350. $text['tag'] = __('Posts Tagged "%s"', 'bunyad'); // text for a tag page
  351. $text['author'] = __('Author: %s', 'bunyad'); // text for an author page
  352. $text['404'] = __('Error 404', 'bunyad'); // text for the 404 page
  353.  
  354. $defaults = array(
  355. 'show_current' => 1, // 1 - show current post/page title in breadcrumbs, 0 - don't show
  356. 'show_on_home' => 0, // 1 - show breadcrumbs on the homepage, 0 - don't show
  357. 'delimiter' => '<span class="delim">&raquo;</span>',
  358. 'before' => '<span class="current">',
  359. 'after' => '</span>',
  360.  
  361. 'home_before' => '<span class="location">'. __('You are at:', 'bunyad') .'</span>',
  362. 'home_after' => '',
  363. 'home_link' => home_url() . '/',
  364.  
  365. 'link_before' => '<span itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">',
  366. 'link_after' => '</span>',
  367. 'link_attr' => ' itemprop="item" ',
  368. 'link_in_before' => '<span itemprop="name">',
  369. 'link_in_after' => '</span> <meta itemprop="position" content="%3$s" />',
  370.  
  371. 'text' => $text,
  372. );
  373.  
  374. extract(apply_filters('bunyad_breadcrumbs_defaults', $defaults));
  375.  
  376. $link = '<a itemprop="url" href="%1$s">' . $link_in_before . '%2$s' . $link_in_after . '</a>';
  377.  
  378. // form whole link option
  379. $link = $link_before . $link . $link_after;
  380.  
  381. if (isset($options['text'])) {
  382. $options['text'] = array_merge($text, (array) $options['text']);
  383. }
  384.  
  385. // override defaults
  386. extract($options);
  387.  
  388. // regex replacement
  389. $replace = $link_before . '<a' . $link_attr . '\\1>' . $link_in_before . '\\2' . $link_in_after . '</a>' . $link_after;
  390.  
  391. /*
  392. * Use bbPress's breadcrumbs when available
  393. */
  394. if (function_exists('bbp_breadcrumb') && is_bbpress()) {
  395.  
  396. $bbp_crumbs =
  397. bbp_get_breadcrumb(array(
  398. 'home_text' => $text['home'],
  399. 'sep' => $delimiter,
  400. 'sep_before' => '',
  401. 'sep_after' => '',
  402. 'pad_sep' => 0,
  403. 'before' => '<div class="breadcrumbs" itemscope itemtype="http://schema.org/BreadcrumbList">' . $home_before,
  404. 'after' => $home_after . '</div>',
  405. 'current_before' => $before,
  406. 'current_after' => $after,
  407. ));
  408.  
  409. if ($bbp_crumbs) {
  410. echo $bbp_crumbs;
  411. return;
  412. }
  413. }
  414.  
  415. /*
  416. * Use WooCommerce's breadcrumbs when available
  417. */
  418. if (function_exists('woocommerce_breadcrumb') && (is_woocommerce() OR is_cart() OR is_shop())) {
  419. woocommerce_breadcrumb(array(
  420. 'delimiter' => $delimiter,
  421. 'before' => '',
  422. 'after' => '',
  423. 'wrap_before' => '<div class="breadcrumbs" itemscope itemtype="http://schema.org/BreadcrumbList" >' . $home_before,
  424. 'wrap_after' => $home_after . '</div>',
  425. 'home' => $text['home'],
  426. ));
  427.  
  428. return;
  429. }
  430.  
  431. // normal breadcrumbs
  432. if ((is_home() || is_front_page())) {
  433.  
  434. if ($show_on_home == 1) {
  435. echo '<div class="breadcrumbs" itemscope itemtype="http://schema.org/BreadcrumbList">'. $home_before . '<a href="' . $home_link . '">' . $text['home'] . '</a>'. $home_after .'</div>';
  436. }
  437.  
  438. } else {
  439. $position = 0;
  440. echo '<div class="breadcrumbs" itemscope itemtype="http://schema.org/BreadcrumbList">' . $home_before . sprintf($link, $home_link, $text['home']) . $home_after . $delimiter;
  441.  
  442. if (is_category() || is_tax())
  443. {
  444. $the_cat = get_category(get_query_var('cat'), false);
  445.  
  446. // have parents?
  447. if (!is_wp_error($the_cat) && $the_cat->parent != 0) {
  448.  
  449. $cats = get_category_parents($the_cat->parent, true, $delimiter);
  450. $cats = preg_replace('#<a([^>]+)>([^<]+)</a>#', $replace, $cats);
  451.  
  452. echo $cats;
  453. }
  454.  
  455. // print category
  456. echo $before . sprintf((is_category() ? $text['category'] : $text['tax']), single_cat_title('', false)) . $after;
  457.  
  458. }
  459. else if (is_search()) {
  460.  
  461. echo $before . sprintf($text['search'], get_search_query()) . $after;
  462.  
  463. }
  464. else if (is_day()) {
  465.  
  466. echo sprintf($link, get_year_link(get_the_time('Y')), get_the_time('Y'), $positon+1 ) . $delimiter
  467. . sprintf($link, get_month_link(get_the_time('Y'),get_the_time('m')), get_the_time('F'), $positon+1 ) . $delimiter
  468. . $before . get_the_time('d') . $after;
  469.  
  470. }
  471. else if (is_month()) {
  472.  
  473. echo sprintf($link, get_year_link(get_the_time('Y')), get_the_time('Y'), $positon+1) . $delimiter
  474. . $before . get_the_time('F') . $after;
  475.  
  476. }
  477. else if (is_year()) {
  478.  
  479. echo $before . get_the_time('Y') . $after;
  480.  
  481. }
  482. // single post or page
  483. else if (is_single() && !is_attachment()) {
  484.  
  485. // custom post type
  486. if (get_post_type() != 'post') {
  487.  
  488. $post_type = get_post_type_object(get_post_type());
  489. printf($link, get_post_type_archive_link(get_post_type()), $post_type->labels->name, $positon+1);
  490.  
  491. if ($show_current == 1) {
  492. echo $delimiter . $before . get_the_title() . $after;
  493. }
  494. }
  495. else {
  496.  
  497. $cat = Bunyad::blocks()->get_primary_cat();
  498. //$cat = get_the_category();
  499.  
  500. if ($cat) {
  501. $cats = get_category_parents($cat, true, $delimiter);
  502.  
  503. if ($show_current == 0) {
  504. $cats = preg_replace("#^(.+)$delimiter$#", "$1", $cats);
  505. }
  506.  
  507. $cats = preg_replace('#<a([^>]+)>([^<]+)</a>#', $replace, $cats);
  508.  
  509. echo $cats;
  510.  
  511. if ($show_current == 1) {
  512. echo $before . get_the_title() . $after;
  513. }
  514. }
  515. }
  516.  
  517. }
  518. elseif (!is_single() && !is_page() && get_post_type() != 'post' && !is_404()) {
  519.  
  520. $post_type = get_post_type_object(get_post_type());
  521.  
  522. echo $before . $post_type->labels->name . $after;
  523.  
  524. }
  525. elseif (is_attachment()) {
  526.  
  527. $parent = get_post($post->post_parent);
  528. $cat = current(get_the_category($parent->ID));
  529. $cats = get_category_parents($cat, true, $delimiter);
  530.  
  531. if (!is_wp_error($cats)) {
  532. $cats = preg_replace('#<a([^>]+)>([^<]+)</a>#', $replace, $cats);
  533. echo $cats;
  534. }
  535.  
  536. printf($link, get_permalink($parent), $parent->post_title, $positon+1);
  537.  
  538. if ($show_current == 1) {
  539. echo $delimiter . $before . get_the_title() . $after;
  540. }
  541.  
  542. }
  543. elseif (is_page() && !$post->post_parent && $show_current == 1) {
  544.  
  545. echo $before . get_the_title() . $after;
  546.  
  547. }
  548. elseif (is_page() && $post->post_parent) {
  549.  
  550. $parent_id = $post->post_parent;
  551. $breadcrumbs = array();
  552.  
  553. while ($parent_id) {
  554. $page = get_post($parent_id);
  555. $breadcrumbs[] = sprintf($link, get_permalink($page->ID), get_the_title($page->ID), $positon+1);
  556. $parent_id = $page->post_parent;
  557. }
  558.  
  559. $breadcrumbs = array_reverse($breadcrumbs);
  560.  
  561. for ($i = 0; $i < count($breadcrumbs); $i++) {
  562.  
  563. echo $breadcrumbs[$i];
  564.  
  565. if ($i != count($breadcrumbs)-1) {
  566. echo $delimiter;
  567. }
  568. }
  569.  
  570. if ($show_current == 1) {
  571. echo $delimiter . $before . get_the_title() . $after;
  572. }
  573.  
  574. }
  575. elseif (is_tag()) {
  576. echo $before . sprintf($text['tag'], single_tag_title('', false)) . $after;
  577.  
  578. }
  579. elseif (is_author()) {
  580.  
  581. global $author;
  582.  
  583. $userdata = get_userdata($author);
  584. echo $before . sprintf($text['author'], $userdata->display_name) . $after;
  585.  
  586. }
  587. elseif (is_404()) {
  588. echo $before . $text['404'] . $after;
  589. }
  590.  
  591. // have pages?
  592. if (get_query_var('paged')) {
  593.  
  594. if (is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author()) {
  595. echo ' (' . __('Page', 'bunyad') . ' ' . get_query_var('paged') . ')';
  596. }
  597. }
  598.  
  599. echo '</div>';
  600.  
  601. }
  602.  
  603. } // breadcrumbs()
  604.  
  605.  
  606. /**
  607. * A get_template_part() improvement with variable scope
  608. *
  609. * @see get_template_part()
  610. * @see locate_template()
  611. *
  612. * @param string $slug The template part to locate.
  613. * @param array $data An array of variables to make available in local scope.
  614. * @param string $name An extension to the partial name.
  615. */
  616. public function partial($slug, $data = array(), $name = '')
  617. {
  618. /**
  619. * Set a few essential globals to match load_template(), without cluttering
  620. * the local scope.
  621. */
  622. global $wp_query, $post;
  623.  
  624. /**
  625. * Fires before the specified template part file is loaded.
  626. *
  627. * @param string $slug The slug name for the generic template.
  628. * @param string $name The name of the specialized template.
  629. */
  630.  
  631. do_action("get_template_part_{$slug}", $slug, $name);
  632.  
  633. $templates = array();
  634. $name = (string) $name;
  635. if (!empty($name)) {
  636. $templates[] = "{$slug}-{$name}.php";
  637. }
  638.  
  639. $templates[] = "{$slug}.php";
  640.  
  641. // Make data array available in local scope
  642. extract($data);
  643.  
  644. // Include the template
  645. include locate_template($templates);
  646. }
  647.  
  648. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement