wp_back/wp-content/themes/simple-theme/functions.php

712 lines
20 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
require get_theme_file_path('/api-posts/api-posts.php');
// ? get variables (globally)
function gV($gv)
{
if ($gv == 'dev') $gv = true;
// else if ($gv == 'critical') $gv = false;
else if ($gv == 'ver') $gv = "1.000";
else if ($gv == 'slug') $gv = "simple-theme";
else $gv = 0;
return $gv;
}
require(get_stylesheet_directory() . '/functions/base.php');
require(get_stylesheet_directory() . '/functions/styles.php');
require(get_stylesheet_directory() . '/functions/scripts.php');
require(get_stylesheet_directory() . '/functions/acf.php');
require(get_stylesheet_directory() . '/functions/get-video.php');
// require(get_stylesheet_directory() . '/functions/name.php');
// Allow SVG
add_filter( 'wp_check_filetype_and_ext', function($data, $file, $filename, $mimes) {
global $wp_version;
if ( $wp_version !== '4.7.1' ) {
return $data;
}
$filetype = wp_check_filetype( $filename, $mimes );
return [
'ext' => $filetype['ext'],
'type' => $filetype['type'],
'proper_filename' => $data['proper_filename']
];
}, 10, 4 );
function cc_mime_types( $mimes ){
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}
add_filter( 'upload_mimes', 'cc_mime_types' );
function fix_svg() {
echo '<style type="text/css">
.attachment-266x266, .thumbnail img {
width: 100% !important;
height: auto !important;
}
</style>';
}
add_action( 'admin_head', 'fix_svg' );
add_theme_support( 'post-thumbnails' );
function get_ajax_blog_posts()
{
global $post;
$blog_posts_offset = ($_POST['offset']) ? $_POST['offset'] : 0;
$numberposts = ($_POST['numberposts']) ? $_POST['numberposts'] : 11;
$banner_display = ($_POST['banner']) ? $_POST['banner'] : false;
$args = array(
'category_name' => 'Blog Post',
'numberposts' => $numberposts,
'offset' => $blog_posts_offset,
);
if(($_POST['tag'])&&($_POST['tag']!='all')){
$args['tag'] = $_POST['tag'];
}
$blog_posts = get_posts($args);
$response = '';
if ($blog_posts) {
$posts_counter = 0;
foreach ($blog_posts as $blog_post) {
$post = $blog_post;
setup_postdata($post);
if($posts_counter==3){
$response .= get_template_part('blog-posts/blog-post-small');
if($blog_posts_offset==0){
$response .= get_template_part('blog-posts/blog-banner');
}
}else if($posts_counter==6){
$response .= get_template_part('blog-posts/blog-post-large');
}else{
$response .= get_template_part('blog-posts/blog-post-small');
}
wp_reset_postdata();
$posts_counter++;
}
}
echo $response;
die;
}
add_action('wp_ajax_get_ajax_blog_posts', 'get_ajax_blog_posts');
add_action('wp_ajax_nopriv_get_ajax_blog_posts', 'get_ajax_blog_posts');
function get_pages_count($tag = false)
{
$args = array(
'category_name' => 'Blog Post',
'numberposts' => -1,
);
if(($tag)&&($tag!='all')){ $args['tag'] = $tag; }
$posts = new WP_Query( $args );
$total = ceil ($posts->found_posts / 11);
return $total;
}
function get_ajax_blog_posts_new()
{
ob_start();
global $post;
$blog_posts_offset = ($_POST['offset']) ? $_POST['offset'] : 0;
$numberposts = ($_POST['numberposts']) ? $_POST['numberposts'] : 11;
$banner_display = ($_POST['banner']) ? $_POST['banner'] : false;
$author = ($_POST['author']) ? $_POST['author'] : -1;
$args = array(
'category_name' => 'Blog Post',
'numberposts' => $numberposts,
'offset' => $blog_posts_offset,
'author' => $author,
);
if(($_POST['tag'])&&($_POST['tag']!='all')){
$args['tag'] = $_POST['tag'];
}
$blog_posts = get_posts($args);
$response = '';
if ($blog_posts) {
$posts_counter = 0;
foreach ($blog_posts as $blog_post) {
$post = $blog_post;
setup_postdata($post);
if($posts_counter==3){
$response .= get_template_part('blog-posts/blog-post-small');
if($blog_posts_offset==0){
$response .= get_template_part('blog-posts/blog-banner');
}
}else if($posts_counter==6){
$response .= get_template_part('blog-posts/blog-post-large');
}else{
$response .= get_template_part('blog-posts/blog-post-small');
}
wp_reset_postdata();
$posts_counter++;
}
}
$response = ob_get_clean();
if (0 == strlen($response))
{
$data['results'] = false;
}
else
{
$data['results'] = true;
}
$data['response'] = $response;
echo json_encode ($data);
die;
}
add_action('wp_ajax_get_ajax_blog_posts_new', 'get_ajax_blog_posts_new');
add_action('wp_ajax_nopriv_get_ajax_blog_posts_new', 'get_ajax_blog_posts_new');
add_action( 'wp_enqueue_scripts', 'myajax_data', 99 );
function myajax_data(){
wp_localize_script( 'main-script', 'myajax',
array(
'url' => admin_url('admin-ajax.php')
)
);
}
function enqueue_my_scripts($hook) {
if ( 'post.php' != $hook ) {
return;
}
wp_enqueue_style( 'post-css', get_template_directory_uri() . '/css/post-style.css' );
}
add_action('admin_enqueue_scripts', 'enqueue_my_scripts');
function load_template_part($template_name, $part_name=null) {
ob_start();
get_template_part($template_name, $part_name);
$var = ob_get_contents();
ob_end_clean();
return $var;
}
function add_blocks_before_h2($content) {
$exploded = explode("<h2", $content);
if ($exploded[2]) {
$exploded[1] = $exploded[1] . load_template_part('blog-posts/blog-telegram');
}
if ($exploded[3]) {
$tagsSlug = [];
$tags = wp_get_post_tags(get_the_ID());
if (!empty($tags)) {
foreach ($tags as $tag) {
$tagsSlug[] = $tag->slug;
}
}
$args = array(
'numberposts' => 1,
'exclude' => get_the_ID(),
'offset' => 0,
'tag' => $tagsSlug
);
$preview_posts = get_posts($args);
global $post;
$post = $preview_posts[0];
setup_postdata( $post );
$post_title = get_the_title();
$post_link = get_permalink();
wp_reset_postdata();
$exploded[2] = $exploded[2] . "<div class='block_read_more'><div class='block_read_more_title'>Читайте также</div><a tabindex=\"0\" href=\"$post_link\">$post_title</a></div>";
}
if ($exploded[4]) {
$exploded[3] = $exploded[3] . load_template_part('blog-posts/blog-register');
}
$result = implode("<h2", $exploded);
return $result;
}
add_filter('the_content', 'add_blocks_before_h2');
add_filter( 'wpseo_remove_reply_to_com', '__return_false' );
function ghub_child_setup() {
add_theme_support( 'editor-styles' );
add_editor_style('style.css');
}
add_action('after_setup_theme', 'ghub_child_setup');
add_filter('wpseo_metadesc', 'update_description');
add_filter('wpseo_opengraph_desc', 'update_description');
function update_description($content) {
global $PAGEN;
if (($PAGEN) && (($PAGEN > 1)))
{
$content .= ". Страница " . $PAGEN;
}
return $content;
}
function update_facebook_description($content) {
global $PAGEN;
if (($PAGEN) && (($PAGEN > 1)))
{
$content .= ". Страница " . $PAGEN;
}
return $content;
}
add_filter( 'wpseo_next_rel_link', 'custom_change_wpseo_next' );
function custom_change_wpseo_next( $link ) {
$link = "";
return $link;
}
if (!function_exists('str_contains')) {
function str_contains($haystack, $needle) {
return $needle !== '' && mb_strpos($haystack, $needle) !== false;
}
}
function get_ajax_blog_single_post()
{
global $post;
$blog_posts_offset = ($_POST['offset']) ? $_POST['offset'] : 0;
$blog_posts_exclude = ($_POST['sameid']);
$args = array(
'category_name' => 'Blog Post',
'numberposts' => 1,
'exclude' => $blog_posts_exclude,
'offset' => $blog_posts_offset,
'meta_query' => array(
'relation' => 'AND',
'post_count_all_rule' => array(
'key' => 'post_count_all',
'compare' => 'EXISTS',
),
),
'orderby' => array(
'date' => 'DESC',
'post_count_all_rule' => 'DESC',
),
);
if(($_POST['tag'])&&($_POST['tag']!='all')){
$args['tag'] = $_POST['tag'];
}
$blog_posts = get_posts($args);
$response = '';
if ($blog_posts) {
ob_start();
foreach ($blog_posts as $blog_post) {
$post = $blog_post;
// var_dump($post);
$ID = $post->ID;
setup_postdata($post);
$title = get_the_title($ID);
$url = get_the_permalink($ID);
$response .= get_template_part('template-parts/ajax_post');
$description = get_post_meta($ID, '_yoast_wpseo_metadesc', true);
wp_reset_postdata();
}
$response = ob_get_clean();
}
if ($response)
{
$results['results'] = true;
$results['content'] = $response;
$results['title'] = $title;
$results['url'] = trim($url);
$results['ID'] = $ID;
$results['description'] = $description;
} else {
$results['results'] = false;
}
echo json_encode($results, JSON_UNESCAPED_UNICODE);
die;
}
add_action('wp_ajax_get_ajax_blog_single_post', 'get_ajax_blog_single_post');
add_action('wp_ajax_nopriv_get_ajax_blog_single_post', 'get_ajax_blog_single_post');
add_action( 'save_post', 'update_blog_meta' );
function update_blog_meta( $post_id ) {
$slug = 'post';
if ( $slug != $_POST['post_type'] ) return;
$rate = get_post_meta ( $post_id, 'post_count_' . $post_id, true);
update_post_meta( $post_id, 'post_count_all', $rate);
}
function allstylesBVN() {
wp_enqueue_style( 'allstyles', get_template_directory_uri() . '/css/allstyles.css', array(), '1.0.1' );
wp_enqueue_style( 'slider-style', get_template_directory_uri() . '/css/lightslider.min.css', array(), 1 );
}
add_action( 'wp_enqueue_scripts', 'allstylesBVN', 200 );
function newstylesBVN() {
wp_enqueue_style( 'newstyles', get_template_directory_uri() . '/css/newstyles.css', array(), rand(0,9999) );
}
add_action( 'wp_enqueue_scripts', 'newstylesBVN', 200 );
function sliderscript() {
wp_enqueue_script( 'slider-script', get_template_directory_uri() . '/js/lightslider.min.js', array(), 1, true );
}
// add_action( 'wp_enqueue_scripts', 'sliderscript', 50 );
function mainScript() {
wp_enqueue_script( 'main-script', get_template_directory_uri() . '/js/main.js', array(), 1, true );
}
// add_action( 'wp_enqueue_scripts', 'mainScript', 100 );
wp_localize_script('main-script', 'siteVars', array(
// 'query' => array(
// 'raw_vars' => $wp_query->query_vars,
// 'vars' => json_encode($wp_query->query_vars), // everything about your loop is here
// 'current_page' => get_query_var('paged') ? get_query_var('paged') : 1,
// 'max_page' => $wp_query->max_num_pages,
// ),
'site' => array(
'url' => site_url(),
'ajax' => admin_url('admin-ajax.php'),
),
// 'translate' => array(
// 'wl_add_text' => __("Added to the wish list", gV('slug')),
// 'wl_remove_text' => __("Removed from the wish list", gV('slug')),
// ),
));
remove_filter('pre_user_description', 'wp_filter_kses');
add_filter( 'pre_user_description', 'wp_filter_post_kses' );
function change_contact_info($contactmethods) {
unset($contactmethods['pinterest']);
unset($contactmethods['soundcloud']);
$contactmethods['tumblr'] = 'Telegram';
$contactmethods['twitter'] = 'Twitter';
$contactmethods['facebook'] = 'Facebook';
$contactmethods['linkedin'] = 'Вконтакте';
$contactmethods['myspace'] = 'Одноклассники';
return $contactmethods;
}
add_filter('user_contactmethods','change_contact_info',10,1);
// Добавляем attached iamge url в REST API endpointЫ с ключом images
function ws_register_images_field() {
register_rest_field(
'post',
'images',
array(
'get_callback' => 'ws_get_images_urls',
'update_callback' => null,
'schema' => null,
)
);
}
add_action( 'rest_api_init', 'ws_register_images_field' );
function ws_get_images_urls( $object, $field_name, $request ) {
$medium = wp_get_attachment_image_src( get_post_thumbnail_id( $object->id ), 'medium' );
$medium_url = $medium['0'];
$large = wp_get_attachment_image_src( get_post_thumbnail_id( $object->id ), 'large' );
$large_url = $large['0'];
return array(
'medium' => $medium_url,
'large' => $large_url,
);
}
add_filter( 'cancel_comment_reply_link', '__return_false' );
// Добавляем имя автора в REST API endpointЫ с ключом author_name
if(!function_exists('add_post_autor_field')){
function add_post_autor_field($response, $post) {
$response->data['author_name'] = get_the_author_meta( 'display_name' , $post->author );
return $response;
}
}
add_filter('rest_prepare_post', 'add_post_autor_field', 10, 3);
// Добавляем галочки к субменю в меню с id=2
add_filter( 'walker_nav_menu_start_el', 'parent_menu_dropdown', 10, 4 );
function parent_menu_dropdown( $item_output, $item, $depth, $args ) {
if ( is_nav_menu( 2 ) && ! empty( $item->classes ) && in_array( 'menu-item-has-children', $item->classes ) ) {
return $item_output . '<span class="mobile-submenu__toggle"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" role="img" aria-hidden="true" class="v-icon__svg"><path d="M7.41,8.58L12,13.17L16.59,8.58L18,10L12,16L6,10L7.41,8.58Z"></path></svg></span>';
}
return $item_output;
}
//Remove Gutenberg Block Library CSS from loading on the frontend
function smartwp_remove_wp_block_library_css(){
wp_dequeue_style( 'wp-block-library' );
wp_dequeue_style( 'wp-block-library-theme' );
wp_dequeue_style( 'wc-blocks-style' ); // Remove WooCommerce block CSS
}
add_action( 'wp_enqueue_scripts', 'smartwp_remove_wp_block_library_css', 100 );
add_action( 'wp_enqueue_scripts', 'my_dequeue_style', 99 );
function my_dequeue_style(){
wp_dequeue_style( 'pld-frontend' );
}
function set_pagination_base () {
global $wp_rewrite;
$wp_rewrite->pagination_base = '%3F' . 'page=';
}
// add_action( 'init', 'set_pagination_base' );
// add_filter( 'paginate_links', function($link){
// if(is_paged()){$link= str_replace('page/1/', '', $link);}
// return $link;
// } );
// add_filter( 'redirect_canonical', function ( $redirect_url ) {
// if ( is_paged() ) {
// return false;
// }
// return $redirect_url;
// }, 10, 1 );
//
// add_action( 'init', function() {
// $GLOBALS['wp_rewrite']->pagination_base = '?PAGEN=';
// });
// Removes the canonical redirection
remove_filter( 'template_redirect', 'redirect_canonical' );
// Add custom rewrite rules
// add_action( 'init', 'my_add_custom_rewrite_rules' );
// function my_add_custom_rewrite_rules() {
// // Slug of the target page
// $page_slug = 'page';
// // Page number to replace
// $page_num = 2;
// // Title you wish to replace the page number with
// $title = 'custom-string';
// // Add the custom rewrite rule
// add_rewrite_rule(
// '^' . $page_slug . '/' . $title . '/?$',
// 'index.php?pagename=' . $page_slug . '&page=' . $page_num, 'top'
// );
// }
// add_action("init","remove_m_parameter");
// function remove_m_parameter()
// {
// if(isset($_GET['tags']))
// {
// unset($_GET['tags']);
// }
// }
//
// вставляем изображение записи под первым параграфом
add_filter( 'the_content', 'insert_featured_image', 20 );
function insert_featured_image( $content ) {
if( !$id ) {
global $post;
$id = get_the_id();
}
$content = preg_replace( "/<\/p>/", "</p>" . get_the_post_thumbnail($id), $content, 1 );
// $content = preg_replace( "/<\/p>/", "</p>" . "", $content, 1 );
$content .= '123';
return $content;
wp_reset_postdata();
}
/**
* Create a custom excerpt string from the first paragraph of the content.
*
* @param integer $id The id of the post
* @return string $excerpt The excerpt string
*/
function wp_first_paragraph_excerpt( $id=null ) {
// Set $id to the current post by default
if( !$id ) {
global $post;
$id = get_the_id();
}
// Get the post content
$content = get_post_field( 'post_content', $id );
$content = apply_filters( 'the_content', strip_shortcodes( $content ) );
// Remove all tags, except paragraphs
$excerpt = strip_tags( $content, '<p></p>' );
// Remove empty paragraph tags
$excerpt = force_balance_tags( $excerpt );
$excerpt = preg_replace( '#<p>\s*+(<br\s*/*>)?\s*</p>#i', '', $excerpt );
$excerpt = preg_replace( '~\s?<p>(\s|&nbsp;)+</p>\s?~', '', $excerpt );
// Get the first paragraph
$excerpt = substr( $excerpt, 0, strpos( $excerpt, '</p>' ) + 4 );
// Remove remaining paragraph tags
$excerpt = strip_tags( $excerpt );
return $excerpt;
}
// //меняем надпись кнопки в форме комментов
// function change_comment_form_submit_label($arg) {
// $arg['label_submit'] = 'Оставить комментарий';
// return $arg;
// }
// add_filter('comment_form_defaults', 'change_comment_form_submit_label', 11);
// add_filter( 'comment_form_default_fields', 'wpse_62742_comment_placeholders' );
/**
* Comment Form Placeholder Comment Field
*/
function placeholder_comment_form_field($fields) {
$replace_comment = __('Комментарий', 'yourdomain');
$fields['comment_field'] = '<p class="comment-form-comment"><label for="comment">' . _x( 'Комментарий', 'noun' ) .
'</label><textarea id="comment" name="comment" cols="45" rows="8" placeholder="'.$replace_comment.'" aria-required="true"></textarea></p>';
return $fields;
}
add_filter( 'comment_form_defaults', 'placeholder_comment_form_field' );
add_filter( 'comment_form_default_fields', 'wpse_62742_comment_placeholders' );
/**
* Change default fields, add placeholder and change type attributes.
*
* @param array $fields
* @return array
*/
function wpse_62742_comment_placeholders( $fields )
{
$fields['author'] = str_replace(
'<input',
'<input placeholder="'
/* Replace 'theme_text_domain' with your themes text domain.
* I use _x() here to make your translators life easier. :)
* See http://codex.wordpress.org/Function_Reference/_x
*/
. _x(
'Имя',
'comment form placeholder',
'theme_text_domain'
)
. '"',
$fields['author']
);
$fields['email'] = str_replace(
'<input id="email" name="email" type="text"',
'<input type="email" placeholder="Email" id="email" name="email"',
$fields['email']
);
return $fields;
}
// add_filter( 'the_content', 'insert_featured_image', 20 );
// function insert_featured_image( $content ) {
// $feat_img = get_the_post_thumbnail($post->ID, 'post-single');
// if ( is_single() && ! is_admin() ) {
// return prefix_insert_after_paragraph( '<div class="incontent-featured-image">' . $feat_img . '</div>', 1, $content );
// }
// return $content;
// }
// Parent Function that makes the magic happen
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}