PageRenderTime 32ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/functions.php

https://gitlab.com/nerdymishka/wp-bad-mishka-theme
PHP | 201 lines | 135 code | 56 blank | 10 comment | 10 complexity | 104cd903539b4503d930ac66ed2021fd MD5 | raw file
  1. <?php
  2. // Send a notice to the user that Timber is not installed
  3. if ( ! class_exists( 'Timber' ) ) {
  4. add_action( 'admin_notices', function() {
  5. echo '<div class="error"><p>Timber not activated. Make sure you activate the plugin in <a href="' . esc_url( admin_url( 'plugins.php#timber' ) ) . '">' . esc_url( admin_url( 'plugins.php' ) ) . '</a></p></div>';
  6. } );
  7. return;
  8. }
  9. Timber::$dirname = array('templates', 'views');
  10. class BadMishkaTheme extends TimberSite {
  11. public $theme_slug = "bad-mishka";
  12. public $sidebarFormat = array(
  13. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  14. 'after_widget' => '</section>',
  15. 'before_title' => '<h2>',
  16. 'after_title' => '</h2>',
  17. );
  18. function __construct() {
  19. // theme support
  20. add_theme_support( 'post-formats' );
  21. add_theme_support( 'post-thumbnails' );
  22. add_theme_support( 'menus' );
  23. add_theme_support( 'automatic-feed-links' );
  24. add_theme_support( 'title-tag' );
  25. // filter
  26. add_filter( 'timber_context', array( $this, 'add_to_context' ) );
  27. add_filter( 'get_twig', array( $this, 'add_to_twig' ) );
  28. // actions
  29. add_action( 'init', array( $this, 'register_post_types' ) );
  30. add_action( 'init', array( $this, 'register_taxonomies' ) );
  31. add_action('init', array( $this, 'update_jquery'));
  32. $can_i_haz_more_posts = 'can_i_haz_more_posts';
  33. add_action( "wp_ajax_$can_i_haz_more_posts", array($this, $can_i_haz_more_posts ));
  34. add_action( "wp_ajax_nopriv_$can_i_haz_more_posts", array($this, $can_i_haz_more_posts ));
  35. add_action( 'widgets_init', array( $this, 'inititalize_post_right_sidebar' ));
  36. add_action( 'widgets_init', array( $this, 'inititalize_page_right_sidebar' ));
  37. add_action( 'widgets_init', array( $this, 'inititalize_primary_right_sidebar' ));
  38. add_action( 'wp_enqueue_scripts', array($this, 'load_metadata' ));
  39. parent::__construct();
  40. }
  41. function load_metadata() {
  42. wp_enqueue_script('theme', get_stylesheet_directory_uri() . '/js/theme.js', array( 'jquery' ), '0.1.0', true );
  43. $args = array();
  44. if(!is_single("posts")) {
  45. $query = array(
  46. 'posts_per_page' => 10
  47. );
  48. $args[ 'nonce' ] = wp_create_nonce( 'load-more-nonce' );
  49. $args[ 'url' ] = admin_url( 'admin-ajax.php' );
  50. $args['query'] = $query;
  51. }
  52. wp_localize_script( 'theme', '$mkSettings', $args );
  53. }
  54. private function get_first_term( $taxonomy, $field ) {
  55. $terms = get_the_terms( get_the_ID(), $taxonomy );
  56. if( empty( $terms ) || is_wp_error( $terms ) )
  57. return false;
  58. // If there's only one term, use that
  59. if( 1 == count( $terms ) ) {
  60. $term = array_shift( $terms );
  61. } else {
  62. $term = array_shift( $list );
  63. }
  64. // Output
  65. if( $field && isset( $term->$field ) )
  66. return $term->$field;
  67. else
  68. return $term;
  69. }
  70. function render_ajax_posts($args) {
  71. $context = Timber::get_context();
  72. $context['posts'] = Timber::get_posts($args);
  73. $context['right_sidebar'] = $right_sidebar;
  74. $context['column_size'] = $columns;
  75. $templates = array( 'articles-ajax.twig' );
  76. Timber::render( $templates, $context );
  77. }
  78. function can_i_haz_more_posts(){
  79. try {
  80. //check_ajax_referer( 'be-load-more-nonce', 'nonce' );
  81. $args = isset( $_POST['query'] ) ? array_map( 'esc_attr', $_POST['query'] ) : array();
  82. $args['post_type'] = isset( $args['post_type'] ) ? esc_attr( $args['post_type'] ) : 'post';
  83. $args['paged'] = esc_attr( $_POST['page'] );
  84. $args['post_status'] = 'publish';
  85. ob_start();
  86. $this->render_ajax_posts($args);
  87. wp_reset_postdata();
  88. $data = ob_get_clean();
  89. echo $data;
  90. } catch(Exception $e) {
  91. echo "something went wrong";
  92. }
  93. die;
  94. }
  95. function update_jquery() {
  96. if (!is_admin()) {
  97. wp_deregister_script('jquery');
  98. wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js', false, '3.1.0');
  99. wp_enqueue_script('jquery');
  100. }
  101. }
  102. function inititalize_primary_right_sidebar() {
  103. $theme_slug = $this->theme_slug;
  104. $rightSidebar = array(
  105. 'name' => __( 'Primary Right Sidebar', $theme_slug ),
  106. 'id' => 'primary-right-sidebar',
  107. 'description' => __( 'Widgets in this area will be shown on the front page.', $theme_slug ),
  108. );
  109. register_sidebar( array_merge($rightSidebar, $this->sidebarFormat) );
  110. }
  111. function inititalize_page_right_sidebar() {
  112. $theme_slug = $this->theme_slug;
  113. $rightSidebar = array(
  114. 'name' => __( 'Page Right Sidebar', $theme_slug ),
  115. 'id' => 'page-right-sidebar',
  116. 'description' => __( 'Widgets in this area will be shown on all pages.', $theme_slug ),
  117. );
  118. register_sidebar( array_merge($rightSidebar, $this->sidebarFormat) );
  119. }
  120. function inititalize_post_right_sidebar() {
  121. $theme_slug = $this->theme_slug;
  122. $rightSidebar = array(
  123. 'name' => __( 'Post Right Sidebar', $theme_slug ),
  124. 'id' => 'post-right-sidebar',
  125. 'description' => __( 'Widgets in this area will be shown on all posts.', $theme_slug ),
  126. );
  127. register_sidebar( array_merge($rightSidebar, $this->sidebarFormat) );
  128. }
  129. function register_post_types() {
  130. //this is where you can register custom post types
  131. }
  132. function register_taxonomies() {
  133. //this is where you can register custom taxonomies
  134. }
  135. function add_to_context( $context ) {
  136. $context['menu'] = new TimberMenu();
  137. $context['site'] = $this;
  138. return $context;
  139. }
  140. function add_to_twig( $twig ) {
  141. // this is where you can add your own fuctions to twig */
  142. return $twig;
  143. }
  144. }
  145. $timberTheme = new BadMishkaTheme();