*/ class TICA_News_Public { /** * The ID of this plugin. * * @since 1.0.0 * @access private * @var string $plugin_name The ID of this plugin. */ private $plugin_name; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $version; /** * Initialize the class and set its properties. * * @since 1.0.0 * @param string $plugin_name The name of the plugin. * @param string $version The version of this plugin. */ public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; $this->version = $version; } /** * Register the stylesheets for the public-facing side of the site. * * @since 1.0.0 */ public function enqueue_styles() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in TICA_News_Loader as all of the hooks are defined * in that particular class. * * The TICA_News_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/tica-news-public.css', array(), $this->version, 'all' ); } /** * Register the JavaScript for the public-facing side of the site. * * @since 1.0.0 */ public function enqueue_scripts() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in TICA_News_Loader as all of the hooks are defined * in that particular class. * * The TICA_News_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/tica-news-public.js', array( 'jquery' ), $this->version, false ); } public function add_back_link( $content ) { global $post; if ( is_single() && TICA_News::POST_TYPE == $post->post_type && $page = TICA_News::get_page() ) { $news_back_url = ''; $news_page_url = get_permalink( $page ); $referrer = wp_get_referer(); if ( 0 === strpos( $referrer, $news_page_url ) ) { $news_back_url = $referrer; } else { $cat = wp_get_object_terms( $post->ID, TICA_News::TAXONOMY_NAME, [ 'meta_key' => TICA_News::TAXONOMY_ORDER_META, 'orderby' => TICA_News::TAXONOMY_ORDER_META, 'order' => 'ASC', 'number' => 1, ] ); if ( ! empty( $cat ) ) { $cat = reset($cat); $news_back_url = $news_page_url . $cat->slug . '/'; } } if ( $news_back_url ) { ob_start(); include plugin_dir_path( __FILE__ ) . 'partials/back-link.php'; $back_link = ob_get_contents(); ob_end_clean(); $content = $back_link . $content; } } return $content; } public function add_categories_submenu( $items ) { global $wpdb; $sql = "select post_id from {$wpdb->postmeta} where meta_key='_wp_page_template' and meta_value='template-news.php'"; $post_ids = $wpdb->get_col($sql); if ($post_ids) { $categories = get_terms( [ 'taxonomy' => TICA_News::TAXONOMY_NAME, 'hide_empty' => true, 'meta_key' => TICA_News::TAXONOMY_ORDER_META, 'orderby' => TICA_News::TAXONOMY_ORDER_META, 'order' => 'ASC', ] ); if ( $categories ) { $taxonomy = get_taxonomy( TICA_News::TAXONOMY_NAME ); $base_url = get_site_url() . '/' . ( ( ! empty( $taxonomy->rewrite['slug'] ) ) ? $taxonomy->rewrite['slug'] : $taxonomy->name ) . '/'; $sub_menu = []; foreach ($categories as $cat) { $menu_item = new stdClass(); $menu_item->ID = -$cat->term_id; $menu_item->post_status = 'publish'; $menu_item->title = $cat->name; $menu_item->object_id = $cat->term_id; $menu_item->url = $base_url . $cat->slug . '/'; $menu_item->menu_item_parent = 0; $menu_item->db_id = $cat->term_id; $menu_item->classes = [ '', 'menu-item', 'menu-item-type-post_type', 'menu-item-object-page' ]; $sub_menu[] = new WP_Post($menu_item); } $items = array_values($items); $indexes = []; $indexes_func = function ( $val, $key ) use ( &$indexes ) { $indexes[$key] = $val->object_id; }; array_walk( $items, $indexes_func ); foreach ( $post_ids as $object_id ) { $idx = array_search( $object_id, $indexes ); if ( $idx ) { if ( ! in_array( 'menu-item-has-children', $items[$idx]->classes ) ) { $items[$idx]->classes[] = 'menu-item-has-children'; } foreach ( $sub_menu as &$sub_menu_item ) { $sub_menu_item->menu_item_parent = $items[$idx]->db_id; } unset( $sub_menu_item ); $items = array_merge( array_slice( $items, 0, $idx + 1), $sub_menu, array_slice( $items, $idx + 1 ) ); $indexes = []; array_walk( $items, $indexes_func ); } } $_items = []; $i = 1; foreach ($items as $menu_item) { $menu_item->menu_order = $i; $_items[$i] = $menu_item; $i++; } $items = $_items; } } return $items; } public function layers_breadcrumbs( $crumbs ) { if ( TICA_News::TAXONOMY_NAME == get_query_var( 'taxonomy' ) ) { if ( $page = TICA_News::get_page() ) { $link = get_permalink( $page ); $crumbsCount = count( $crumbs ); $beforeCount = 1; $crumbs = array_slice( $crumbs, 0, $crumbsCount - $beforeCount, true ) + [ $page->post_title => [ 'link' => $link, 'label' => $page->post_title ] ] + array_slice( $crumbs, $crumbsCount - $beforeCount, $beforeCount, true ); } } return $crumbs; } public function paginate_links( $link ) { $page_pattern = '/page/1'; if ( false !== strpos( $link, $page_pattern ) ) { $link = str_replace( $page_pattern, '', $link ); } return $link; } public function filter_title( $title ) { $page = $page = TICA_News::get_page(); if ( ! is_single() && get_the_ID() == $page->ID ) { $category_slug = get_query_var( TICA_News::TAXONOMY_NAME ); if ( $category_slug ) { $term = get_term_by( 'slug', $category_slug, TICA_News::TAXONOMY_NAME ); if ( $term ) { $title['title'] = $term->name; } } } return $title; } }