<?php
/*
* Theme Fubctions
*/
// custom post
include (TEMPLATEPATH . '/includes/core/custom-post-type.php');
// video meta
include (TEMPLATEPATH . '/includes/core/post-video-meta.php');
// gallery meta
include (TEMPLATEPATH . '/includes/core/post-gallery-meta.php');
// post views meta
include (TEMPLATEPATH . '/includes/core/post-meta-views.php');
// bootstrap navwalker
include (TEMPLATEPATH . '/includes/functions/wp_bootstrap_navwalker.php');
// include tpanel
include (TEMPLATEPATH . '/tpanel/tpanel.php');
// on active theme
global $pagenow;
if (is_admin() && isset($_GET['activated']) && $pagenow == 'themes.php') {
wp_redirect('admin.php?page=T-Panel');
}
if (!isset($content_width))
$content_width = 970;
// theme setup
function ah_setup() {
//add_theme_support('woocommerce'); // Add theme support for WooCommerce plugin .
add_theme_support('post-thumbnails'); // Add Thumbnails
add_filter('enable_post_format_ui', '__return_false');
load_theme_textdomain('ah_lang', get_template_directory() . '/languages');
register_nav_menus(array(
'top-menu' => __('Header Menu', 'ah_lang'),
));
add_theme_support('html5', array('search-form', 'comment-form', 'comment-list')); // to output valid HTML5.
}
add_action('after_setup_theme', 'ah_setup');
// featured image in feed
function featured_image_in_feed($content) {
global $post;
if (is_feed()) {
if (has_post_thumbnail($post->ID)) {
$output = get_the_post_thumbnail($post->ID, 'medium', array('style', 'float:right; margin:0 0 10px 10px;'));
$content = $output . $content;
}
}
return $content;
}
add_filter('the_content', 'featured_image_in_feed');
// theme sidebar
function ah_sidebar() {
$before_widget = '<div id="%1$s" class="widget %2$s">';
$after_widget = '</div></div><!--/widget-->';
$before_title = '<h2>';
$after_title = '</h2>
<div class="widget-body">';
register_sidebar(array(
'name' => __('ستوونى لاوەكى', 'ah_lang'),
'id' => 'single-sidebar',
'description' => __('تێكردن لێرە', 'ah_lang'),
'before_widget' => $before_widget,
'after_widget' => $after_widget,
'before_title' => $before_title,
'after_title' => $after_title
));
}
add_action('widgets_init', 'ah_sidebar');
// remove search widget
function remove_default_widgets() {
if (function_exists('unregister_widget')) {
unregister_widget('WP_Widget_Search');
}
}
add_action('widgets_init', 'remove_default_widgets');
# Template Comments
function Tatwerat_comment($comment, $args, $depth) {
$GLOBALS['comment'] = $comment;
?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
<div id="comment-<?php comment_ID(); ?>">
<div class="comment-author vcard"> <?php echo get_avatar($comment, $size = '48', $default = '<path_to_url>'); ?> <?php printf(__('<cite class="fn">%s</cite> <span class="says">دەڵێت </span>'), get_comment_author_link()) ?>
<div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars(get_comment_link($comment->comment_ID)) ?>"><?php printf(__('%1$s at %2$s'), get_comment_date(), get_comment_time()) ?></a>
<?php edit_comment_link(__('(Edit)'), ' ', '') ?>
</div>
</div>
<?php if ($comment->comment_approved == '0') : ?>
<em style="margin:5px 60px 0px 0px; color:#c00 !important;">
<?php _e('سەرنجى تۆ پێويستى دەبێت بۆ پێداچوونەوەى بەڕێوەبەر .') ?>
</em> <br />
<?php endif; ?>
<?php comment_text() ?>
<div class="reply">
<?php comment_reply_link(array_merge($args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
</div>
</div>
<?php
}
// blog pagination
function pagination($pages = '', $range = 3) {
$showitems = ($range * 2) + 1;
global $paged;
if (empty($paged))
$paged = 1;
if ($pages == '') {
global $wp_query;
$pages = $wp_query->max_num_pages;
if (!$pages) {
$pages = 1;
}
}
if ($pages != 1) {
echo "<div class='btn-group' role='group'>";
if ($paged > 2 && $paged > $range + 1 && $showitems < $pages)
echo "<a href='" . get_pagenum_link(1) . "' class='btn btn-lg btn-default'>First </a>";
if ($paged > 1 && $showitems < $pages)
echo "<a href='" . get_pagenum_link($paged - 1) . "' class='btn btn-lg btn-default'>Back </a>";
for ($i = 1; $i <= $pages; $i++) {
if (1 != $pages && (!($i >= $paged + $range + 1 || $i <= $paged - $range - 1) || $pages <= $showitems )) {
echo ($paged == $i) ? "<a class='btn btn-lg btn-default active'>" . $i . "</a>" : "<a href='" . get_pagenum_link($i) . "' class='btn btn-lg btn-default inactive'>" . $i . "</a>";
}
}
if ($paged < $pages && $showitems < $pages)
echo "<a href=\"" . get_pagenum_link($paged + 1) . "\" class='btn btn-lg btn-default'>«</a>";
if ($paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages)
echo "<a href='" . get_pagenum_link($pages) . "' class='btn btn-lg btn-default'>»</a>";
echo "</ul></div>";
}
}
// limit content
function limit_content($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt) >= $limit) {
array_pop($excerpt);
$excerpt = implode(" ", $excerpt) . '';
} else {
$excerpt = implode(" ", $excerpt);
}
$excerpt = preg_replace('`\[[^\]]*\]`', '', $excerpt);
echo $excerpt . ' [..]';
}
function ah_content_limit($text, $chars = 120) {
$text = mb_substr($text, 0, $chars, 'UTF-8');
return $text . " [..]";
}
// limit text
function limit_text($str, $n_chars) {
$buff = strip_tags($str);
if (strlen($buff) > $n_chars) {
$cut_index = strpos($buff, ' ', $n_chars);
$buff = substr($buff, 0, ($cut_index === false ? $n_chars : $cut_index + 1)) . ' [..] ';
}
echo $buff;
}
function limit_title($str, $n_chars) {
$buff = strip_tags($str);
if (strlen($buff) > $n_chars) {
$cut_index = strpos($buff, ' ', $n_chars);
$buff = substr($buff, 0, ($cut_index === false ? $n_chars : $cut_index + 1)) . ' .. ';
}
echo $buff;
}
// get nav menus
function ah_nav_menu($location = '') {
//$get_menu = wp_get_nav_menu_object($locations[$menu_name]);
//$menu_items = wp_get_nav_menu_items($get_menu->term_id);
//$url = $menu_item->url;
$options = array(
'theme_location' => $location,
'menu' => '',
'container' => false,
'container_class' => '',
'container_id' => '',
'menu_class' => '',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '%3$s',
'depth' => 0,
'walker' => ''
);
return wp_nav_menu($options);
}
// theme path
function theme_path() {
echo get_template_directory_uri();
}
function get_theme_path() {
return get_template_directory_uri();
}
// check is mobile
function is_mobile() {
return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
}
// wp title
function ah_wp_title($title, $sep) {
global $paged, $page;
if (is_feed())
return $title;
// Add the site name.
$title .= get_bloginfo('name');
// Add the site description for the home/front page.
$site_description = get_bloginfo('description', 'display');
if ($site_description && ( is_home() || is_front_page() )) {
$title = "$title $sep $site_description";
}
// Add a page number if necessary.
if ($paged >= 2 || $page >= 2) {
$title = "$title $sep " . sprintf(__('Page %s', 'ah_lang'), max($paged, $page));
}
return $title;
}
if (!class_exists('All_in_One_SEO_Pack') && !function_exists('wpseo_get_value')) {
//add_filter('wp_title', 'ah_wp_title', 10, 2);
}
// site title
function siteTitle() {
if (is_home()) {
echo bloginfo('name');
} elseif (is_404()) {
echo '404 هيتر هەيە';
} elseif (is_category()) {
echo '';
wp_title('');
} elseif (is_search()) {
echo 'گەران سەرکەوتووبوو';
} elseif (is_day() || is_month() || is_year()) {
echo 'ئەرشیف : ';
wp_title('');
} else {
echo wp_title('');
}
}
// Custom Admin Bar Menus
function ah_admin_bar() {
global $wp_admin_bar;
if (current_user_can('switch_themes')) {
$wp_admin_bar->add_menu(array(
'parent' => 0,
'id' => 'mpanel_page',
'title' => __('Theme - Panel', 'ah_lang'),
'href' => admin_url('admin.php?page=T-Panel')
));
}
}
add_action('wp_before_admin_bar_render', 'ah_admin_bar');
// Post Meta
function post_meta($meta_name) {
global $post;
echo get_post_meta($post->ID, $meta_name, true);
}
function get_meta($meta_name) {
global $post;
return get_post_meta($post->ID, $meta_name, true);
}
// og meta function
function ah_og_meta() {
global $post;
if (has_post_thumbnail()) {
$domsxe = simplexml_load_string(get_the_post_thumbnail());
$post_thumb = $domsxe->attributes()->src;
} else {
$post_thumb = get_template_directory_uri() . '/no-image.gif';
}
//echo $post->post_content;
$description = htmlspecialchars(strip_tags(strip_shortcodes($post->post_content)));
?>
<meta property="og:title" content="<?php the_title(); ?>">
<meta property="og:type" content="article">
<meta property="og:description" content="<?php echo ah_content_limit($description, 100); ?>">
<meta property="og:url" content="<?php the_permalink(); ?>">
<meta property="og:site_name" content="<?php echo get_bloginfo('name') ?>">
<?php
if (!empty($post_thumb))
echo '<meta property="og:image" content="' . $post_thumb . '"> <link rel="image_src" href="' . $post_thumb . '">';
}
// avatar_img
function avatar_img($email, $size = NULL) {
// alternate gravatar code for < 2.5
$default = '';
$grav_url = "http://www.gravatar.com/avatar/" . md5(strtolower($email)) . "?d=" . urlencode($default) . "&s=" . $size;
return $grav_url;
}
// post thumbnail
function post_thumbnail() {
global $post;
$img_url = get_post_meta(get_the_ID(), 'post_image', true);
if (has_post_thumbnail()) {
the_post_thumbnail('evandr_post', array(
'class' => 'postimg',
'title' => get_the_title(),
'alt' => get_the_title(),
));
} elseif ($img_url != '') {
echo '<img class="postimg" src="' . $img_url . '" alt="' . get_the_title() . '">';
} else {
// $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$output = preg_match_all('/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'>]*)/i', htmlspecialchars_decode($post->post_content), $matches);
$first_img = isset($matches [1] [0]) ? $matches [1] [0] : '';
if (empty($first_img)) { //Defines a default image with 0 width
echo '<img class="postimg" src="' . get_template_directory_uri() . '/no-image.gif" alt="' . get_the_title() . '">';
} else {
echo "<img class='postimg' src='$first_img' alt='" . get_the_title() . "'>";
}
}
}
function youtube_id_from_url($url) {
$pattern = '%^# Match any youtube URL
(?:https?://)? # Optional scheme. Either http or https
(?:www\.)? # Optional www subdomain
(?: # Group host alternatives
youtu\.be/ # Either youtu.be,
| youtube\.com # or youtube.com
(?: # Group path alternatives
/embed/ # Either /embed/
| /v/ # or /v/
| /watch\?v= # or /watch\?v=
) # End path alternatives.
) # End host alternatives.
([\w-]{10,12}) # Allow 10-12 for 11 char youtube id.
$%x'
;
$result = preg_match($pattern, $url, $matches);
if (false !== $result) {
return $matches[1];
}
return false;
}
function video_thumbnail($url) {
$post_thumb = '';
if (!empty($url)) {
$post_thumb = 'http://img.youtube.com/vi/' . youtube_id_from_url($url) . '/0.jpg';
echo '<img class="postimg" src="' . $post_thumb . '" alt="' . get_the_title() . '">';
} else {
echo '<img class="postimg" src="' . get_template_directory_uri() . '/no-image.gif" alt="' . get_the_title() . '">';
}
}
function gallery_thumbnail($postid, $count) {
$post_thumb = get_post_meta($postid, 'slideshow_img_' . $count, true);
echo '<img class="postimg" src="' . $post_thumb . '" alt="' . get_the_title() . '">';
}
// Post Video
function ah_post_video($video_url, $height) {
$width = '100%';
$video_link = @parse_url($video_url);
if ($video_link['host'] == 'www.youtube.com' || $video_link['host'] == 'youtube.com') {
parse_str(@parse_url($video_url, PHP_URL_QUERY), $my_array_of_vars);
$video = $my_array_of_vars['v'];
$video_output = '<iframe style="width:' . $width . '; height:' . $height . ';" src="http://www.youtube.com/embed/' . $video . '?rel=0&wmode=opaque" allowfullscreen></iframe>';
} elseif ($video_link['host'] == 'www.youtu.be' || $video_link['host'] == 'youtu.be') {
$video = substr(@parse_url($video_url, PHP_URL_PATH), 1);
$video_output = '<iframe style="width:' . $width . '; height:' . $height . ';" src="http://www.youtube.com/embed/' . $video . '?rel=0&wmode=opaque" allowfullscreen></iframe>';
}
if (!empty($video_output))
echo $video_output;
}
// add scripts and css to admin
function ah_admin_enqueue() {
wp_enqueue_script('custom_script', get_template_directory_uri() . '/assets/admin-scripts/admin.script.js', false, '1.0.0');
wp_enqueue_style('admin_css', get_template_directory_uri() . '/assets/admin-scripts/admin.style.css', false, '1.0.0');
}
add_action('admin_enqueue_scripts', 'ah_admin_enqueue');
// Nav Menus
function Get_Menus($menu, $type) {
//$get_menu = wp_get_nav_menu_object($locations[$menu_name]);
//$menu_items = wp_get_nav_menu_items($get_menu->term_id);
//$url = $menu_item->url;
if ($type == 'menu') {
$options = array(
'theme_location' => '',
'menu' => $menu,
'container' => false,
'container_class' => '',
'container_id' => '',
'menu_class' => '',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '%3$s',
'depth' => 0,
'walker' => ''
);
} else if ($type == 'select') {
$options = array(
'theme_location' => '',
'menu' => $menu,
'container' => '',
'container_class' => '',
'container_id' => '',
'menu_class' => '',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '<option value="' . $url . '">',
'after' => '</option>',
'link_before' => '',
'link_after' => '',
'items_wrap' => '%3$s',
'depth' => 0,
'walker' => ''
);
}
return wp_nav_menu($options);
}
//Delete all featured images by visiting /?delete-featured-images=1
add_action('init', 'foo_bar_delete_featured', 0);
function foo_bar_delete_featured() {
# Check for logged in state
if (!is_user_logged_in())
return;
# Check for admin role
if (!current_user_can('manage_options'))
return;
# Check for query string
if (isset($_GET['delete-featured-images']) && $_GET['delete-featured-images'] == 1) {
global $wpdb;
# Run a DQL to get all featured image rows
$attachments = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id'");
foreach ($attachments as $attachment) {
# Run a DML to remove this featured image row
$wpdb->query("DELETE FROM $wpdb->postmeta WHERE meta_id = '$attachment->meta_id' LIMIT 1");
# Delete attachment DB rows and files
wp_delete_attachment($attachment->meta_value, true);
# Print to screen
//show_message('Attachment #$attachment->meta_value deleted.');
}
exit;
}
}