/src/functions.php
PHP | 201 lines | 135 code | 56 blank | 10 comment | 10 complexity | 104cd903539b4503d930ac66ed2021fd MD5 | raw file
- <?php
- // Send a notice to the user that Timber is not installed
- if ( ! class_exists( 'Timber' ) ) {
- add_action( 'admin_notices', function() {
- 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>';
- } );
- return;
- }
- Timber::$dirname = array('templates', 'views');
- class BadMishkaTheme extends TimberSite {
- public $theme_slug = "bad-mishka";
- public $sidebarFormat = array(
- 'before_widget' => '<section id="%1$s" class="widget %2$s">',
- 'after_widget' => '</section>',
- 'before_title' => '<h2>',
- 'after_title' => '</h2>',
- );
- function __construct() {
-
-
- // theme support
- add_theme_support( 'post-formats' );
- add_theme_support( 'post-thumbnails' );
- add_theme_support( 'menus' );
- add_theme_support( 'automatic-feed-links' );
- add_theme_support( 'title-tag' );
-
- // filter
- add_filter( 'timber_context', array( $this, 'add_to_context' ) );
- add_filter( 'get_twig', array( $this, 'add_to_twig' ) );
-
- // actions
- add_action( 'init', array( $this, 'register_post_types' ) );
- add_action( 'init', array( $this, 'register_taxonomies' ) );
- add_action('init', array( $this, 'update_jquery'));
-
- $can_i_haz_more_posts = 'can_i_haz_more_posts';
-
- add_action( "wp_ajax_$can_i_haz_more_posts", array($this, $can_i_haz_more_posts ));
- add_action( "wp_ajax_nopriv_$can_i_haz_more_posts", array($this, $can_i_haz_more_posts ));
-
- add_action( 'widgets_init', array( $this, 'inititalize_post_right_sidebar' ));
- add_action( 'widgets_init', array( $this, 'inititalize_page_right_sidebar' ));
- add_action( 'widgets_init', array( $this, 'inititalize_primary_right_sidebar' ));
-
- add_action( 'wp_enqueue_scripts', array($this, 'load_metadata' ));
-
- parent::__construct();
- }
- function load_metadata() {
-
- wp_enqueue_script('theme', get_stylesheet_directory_uri() . '/js/theme.js', array( 'jquery' ), '0.1.0', true );
- $args = array();
- if(!is_single("posts")) {
- $query = array(
-
- 'posts_per_page' => 10
- );
-
- $args[ 'nonce' ] = wp_create_nonce( 'load-more-nonce' );
- $args[ 'url' ] = admin_url( 'admin-ajax.php' );
- $args['query'] = $query;
- }
-
- wp_localize_script( 'theme', '$mkSettings', $args );
- }
- private function get_first_term( $taxonomy, $field ) {
- $terms = get_the_terms( get_the_ID(), $taxonomy );
-
- if( empty( $terms ) || is_wp_error( $terms ) )
- return false;
-
- // If there's only one term, use that
- if( 1 == count( $terms ) ) {
- $term = array_shift( $terms );
- } else {
- $term = array_shift( $list );
- }
-
- // Output
- if( $field && isset( $term->$field ) )
- return $term->$field;
-
- else
- return $term;
- }
- function render_ajax_posts($args) {
-
- $context = Timber::get_context();
- $context['posts'] = Timber::get_posts($args);
- $context['right_sidebar'] = $right_sidebar;
- $context['column_size'] = $columns;
- $templates = array( 'articles-ajax.twig' );
- Timber::render( $templates, $context );
- }
- function can_i_haz_more_posts(){
-
- try {
- //check_ajax_referer( 'be-load-more-nonce', 'nonce' );
-
- $args = isset( $_POST['query'] ) ? array_map( 'esc_attr', $_POST['query'] ) : array();
- $args['post_type'] = isset( $args['post_type'] ) ? esc_attr( $args['post_type'] ) : 'post';
- $args['paged'] = esc_attr( $_POST['page'] );
- $args['post_status'] = 'publish';
- ob_start();
-
- $this->render_ajax_posts($args);
- wp_reset_postdata();
- $data = ob_get_clean();
- echo $data;
- } catch(Exception $e) {
- echo "something went wrong";
- }
-
- die;
- }
- function update_jquery() {
- if (!is_admin()) {
- wp_deregister_script('jquery');
- wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js', false, '3.1.0');
- wp_enqueue_script('jquery');
- }
- }
-
- function inititalize_primary_right_sidebar() {
- $theme_slug = $this->theme_slug;
- $rightSidebar = array(
- 'name' => __( 'Primary Right Sidebar', $theme_slug ),
- 'id' => 'primary-right-sidebar',
- 'description' => __( 'Widgets in this area will be shown on the front page.', $theme_slug ),
- );
- register_sidebar( array_merge($rightSidebar, $this->sidebarFormat) );
- }
- function inititalize_page_right_sidebar() {
- $theme_slug = $this->theme_slug;
- $rightSidebar = array(
- 'name' => __( 'Page Right Sidebar', $theme_slug ),
- 'id' => 'page-right-sidebar',
- 'description' => __( 'Widgets in this area will be shown on all pages.', $theme_slug ),
- );
-
- register_sidebar( array_merge($rightSidebar, $this->sidebarFormat) );
- }
- function inititalize_post_right_sidebar() {
- $theme_slug = $this->theme_slug;
- $rightSidebar = array(
- 'name' => __( 'Post Right Sidebar', $theme_slug ),
- 'id' => 'post-right-sidebar',
- 'description' => __( 'Widgets in this area will be shown on all posts.', $theme_slug ),
- );
- register_sidebar( array_merge($rightSidebar, $this->sidebarFormat) );
- }
- function register_post_types() {
- //this is where you can register custom post types
- }
-
-
- function register_taxonomies() {
- //this is where you can register custom taxonomies
- }
- function add_to_context( $context ) {
- $context['menu'] = new TimberMenu();
- $context['site'] = $this;
- return $context;
- }
- function add_to_twig( $twig ) {
- // this is where you can add your own fuctions to twig */
-
- return $twig;
- }
- }
- $timberTheme = new BadMishkaTheme();