File "commonfunctions.php"

Full Path: /home/ozbarhaber/public_html/19052025___siteeee/wp-content/themes/anka/lib/functions/commonfunctions.php
File size: 15.54 KB
MIME-type: text/x-php
Charset: utf-8

<?php if(!defined('ABSPATH')) exit; ?>
<?php
function safirAd($ads, $class = '') {
	if(wp_is_mobile()) {
		if($x = xoption($ads . 'Mobile')) { ?>
			<div class="<?php echo $class ?>"><?php echo do_shortcode($x) ?></div>
			<?php
		}
	} else {
		if($x = xoption($ads . 'Desktop')) { ?>
			<div class="<?php echo $class ?>"><?php echo do_shortcode($x) ?></div>
			<?php
		}
	}
}

function getSafirAd($ads, $class = '') {
	if(wp_is_mobile()) {
		if($x = xoption($ads . 'Mobile')) {
			return '<div class="' . $class . '">'.  do_shortcode($x) . '</div>';
		}
	} else {
		if($x = xoption($ads . 'Desktop')) {
			return '<div class="' . $class . '">'.  do_shortcode($x) . '</div>';
		}
	}
}

function safirCategoryMeta() {
	?>
	<div class="safirCategoryMeta">
		<?php
		$categories = get_the_category();
		$excludes = xoption("metaExcludeCategory");
		foreach ($categories as $category) {
			if(is_array($excludes)) {
				if(!in_array($category->term_id, $excludes)) {
					?>
					<a class="sfrCatClr cat<?php echo $category->term_id ?>" href="<?php echo get_category_link($category->term_id) ?>"><?php echo $category->name; ?></a>
					<?php
				}
			} else {
				?>
				<a class="sfrCatClr cat<?php echo $category->term_id ?>" href="<?php echo get_category_link($category->term_id) ?>"><?php echo $category->name; ?></a>
				<?php
			}
		}
		?>
	</div>
	<?php
}

function safirMetas($metas, $category = true) {
	if(is_array($metas)) {
		if(count($metas) > 0) {
			?>
			<div class="safirMetas">
				<?php
				foreach ($metas as $meta) {
					switch ($meta) {
						case 'author':
						?>
							<div class="meta author">
								<?php themeIcon("user") ?>
								<?php the_author_posts_link(); ?>
							</div>
						<?php
						break;
						case 'category':
							if($category) :?>
							<div class="meta category">
								<?php themeIcon("list") ?>
								<?php safirCategoryMeta(); ?>
							</div>
							<?php
							endif;
						break;
						case 'date':
						?>
							<div class="meta date">
								<?php themeIcon("calendar") ?>
								<?php the_time(get_option("date_format") . " " . get_option("time_format")); ?>
							</div>
						<?php
						break;
						case 'hit':
						?>
							<div class="meta hit" data-id="<?php echo get_the_ID() ?>">
								<?php themeIcon("hit") ?>
								<span class="count" >
									<?php
									$postHit = get_post_meta(get_the_ID(), "hit", true);
									if ($postHit == NULL) { $postHit = 0; }
									echo number_format($postHit, 0, '', '.');
									?>
								</span>
							</div>
						<?php
						break;
						case 'comment':
						?>
							<div class="meta comment">
								<?php themeIcon("comment") ?>
								<a href="<?php the_permalink(); ?>#comments" rel="nofollow">
								<?php comments_number("0", "1", "%"); ?></a>
							</div>
						<?php
						break;
					}
				}
				?>
			</div>
			<?php
		}
	}
}

function safir_sidebar($sidebar = "general") {
	if(is_category() && $x = get_option("category_" . get_query_var("cat") . "_sidebar")) return;
	if(is_single() && $x = get_post_meta(get_the_ID(), "hideSidebar", true)) return;
	if(is_page() && $x = get_post_meta(get_the_ID(), "hideSidebar", true)) return;
	if( (is_single() && in_category(xoption("resmiilanCategory"))) ) return;
	if( (is_category() && xoption("resmiilanCategory") && is_category(xoption("resmiilanCategory"))) ) return;
	
	if( (wp_is_mobile() && xoption('hideMobileSidebar')) == false && xoption("sidebarPosition") != "noSidebar" ) : ?>
	<aside id="aside">
		<?php global $widgetPlace; $widgetPlace = "sidebar"; ?>
		<div id="panels">
			<?php
			if(is_active_sidebar('sidebar-' . $sidebar)) :
				dynamic_sidebar('sidebar-' . $sidebar);
			else:
				dynamic_sidebar('sidebar-general');
			endif;
			?>
		</div>
	</aside>
	<?php endif;
}

function safirShowElement($item) {
	$result = false;
	$isMobile = wp_is_mobile();
	$data = xoption($item);
	switch ($data) {
		case 'both':
			$result = true;
			break;

		case 'desktop':
			if(!$isMobile) $result = true;
			break;

		case 'mobile':
			if($isMobile) $result = true;
			break;

	}
	return $result;
}

function catch_that_image() {
	global $post, $posts;
	$first_img = '';
	ob_start();
	ob_end_clean();
	$output = preg_match_all('/<img [^>]*src=["|\']([^"|\']+)/i', $post->post_content, $matches);
	if(isset($matches [1] [0])) $first_img = $matches [1] [0];
	return $first_img;
}

function safirthumb($size = "large") {

	if(xoption("highQualityImages")) $size = "full";

	$ID = get_post_thumbnail_id();
	$alt = get_the_title();

	$imageData = wp_get_attachment_image_src($ID, $size);
	if(is_array($imageData)) {
		$image = $imageData[0];
		$width = $imageData[1];
		$height = $imageData[2];
	} else {
		$image = catch_that_image();
		$width = 160;
		$height = 90;
		if($image == "") $image = xoption("noimageforpost");
	}
	?>
	<div class="safirthumb">
		<div class="thumbnail">
			<div class="center">
				<img <?php safirLazyThumb($image) ?> alt="<?php echo $alt ?>" width="<?php echo $width ?>" height="<?php echo $height ?>" />
			</div>
		</div>
	</div>
	<?php
}

function safirLazyThumb($image) {
	if(xoption("lazyload")) {
		echo 'src="' . xoption("noimageforpost") . '" data-src="' . $image . '" class="lazy" loading="lazy"';
	} else {
		echo 'src="' . $image . '"';
	}
}

// Safir Top Category
function safir_top_category($catID) {
	$curr_cat = get_category_parents($catID, false, '/' ,true);
	$curr_cat = explode('/',$curr_cat);
	$idObj = get_category_by_slug($curr_cat[0]);
	return $idObj->term_id;
}

function safir_category_nav_class( $classes, $item ){
    if( 'category' == $item->object ){
        $classes[] = 'color' . $item->object_id;
    }
    return $classes;
}
add_filter( 'nav_menu_css_class', 'safir_category_nav_class', 10, 2 );

function file_get_contents_curl($url) {
	$ch = curl_init();

	curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

	$data = curl_exec($ch);
	curl_close($ch);

	return $data;
}

function disable_wp_emojicons() {
	remove_action( 'admin_print_styles', 'print_emoji_styles' );
	remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
	remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
	remove_action( 'wp_print_styles', 'print_emoji_styles' );
	remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
	remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
	remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
}
add_action( 'init', 'disable_wp_emojicons' );

add_filter( 'get_the_archive_title', function ($title) {
    if ( is_category() ) {
        $title = single_cat_title( '', false );
    }

    return $title;

});

add_filter( 'the_content', 'safir_insert_post_ads');
function safir_insert_post_ads( $content ) {
	if ( !in_category(xoption("resmiilanCategory")) && !is_attachment() && is_single() && ! is_admin() ) {
		return safir_insert_after_paragraph($content );
	}
	return $content;
}

function safir_insert_after_paragraph( $content ) {
	$closing_p = '</p>';
	$paragraphs = explode( $closing_p, $content );
	foreach ($paragraphs as $index => $paragraph) {

		if ( trim( $paragraph ) ) {
			//$paragraphs[$index] .= $closing_p;
		}

		if ( xoption("singleAdAra1Pos") == $index) {
			$paragraphs[$index] .= "<div class=ads>" . getSafirAd("singleAdAra1") . "</div>";
		}

		if ( xoption("singleAdAra2Pos") == $index) {
			$paragraphs[$index] .= "<div class=ads>" . getSafirAd("singleAdAra2") . "</div>";
		}
	}

	return implode( '', $paragraphs );
}

remove_action('wp_head', 'wp_generator');

function safirText2($content, $wordsCount) {
	$string = safirBracket(strip_tags($content));
	$array = explode(" ", $string);
	if (count($array)>$wordsCount) :
		array_splice($array, $wordsCount);
		$string = implode(" ", $array)."...";
	endif;
	echo $string;
}

// Bracket Remover
function safirBracket($x) {
	$active = false;
	$result = "";
	$len = strlen($x);
	if($len>1) {
		for ($i=0; $i<=$len-1; $i++) {
			if(substr($x, $i, 1) == '[') {
				$active = true;
			}
			elseif (substr($x, $i, 1) == "]") {
				$active = false;
			}
			if($active == false && substr($x, $i, 1) != "]") {
				$result .= substr($x, $i, 1);
			}
		}
		return $result;
	}
}


add_filter( 'wp_mail_content_type', 'set_content_type' );
function set_content_type( $content_type ) {
	return 'text/html';
}

function safirText($wordsCount, $echo = true, $excerpt = true) {
	global $post;
	if($excerpt && $x = get_the_excerpt()) :
		$string = safirBracket(strip_tags($x));
	else :
		$string = safirBracket(strip_tags(get_the_content()));
	endif;
	$array = explode(" ", $string);
	if (count($array)>$wordsCount) :
		array_splice($array, $wordsCount);
		$string = implode(" ", $array)."...";
	endif;
	if($echo) :
		echo $string;
	else :
		return $string;
	endif;
}

add_filter( 'sanitize_file_name', 'safir_special_chars' );
function safir_special_chars ( $filename ) {
	$search = array('Ç','ç','Ğ','ğ','ı','İ','Ö','ö','Ş','ş','Ü','ü',' ');
	$replace = array('c','c','g','g','i','i','o','o','s','s','u','u','-');
	$new = str_replace($search, $replace, $filename);
	return $new;
}

// Uploading

function safir_upload_user_file( $file = array() ) {

	require_once( ABSPATH . 'wp-admin/includes/admin.php' );

	  $file_return = wp_handle_upload( $file, array('test_form' => false ) );

	  if( isset( $file_return['error'] ) || isset( $file_return['upload_error_handler'] ) ) {
		  return false;
	  } else {

		  $filename = $file_return['file'];

		  $attachment = array(
			  'post_mime_type' => $file_return['type'],
			  'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
			  'post_content' => '',
			  'post_status' => 'inherit',
			  'guid' => $file_return['url']
		  );

		  $attachment_id = wp_insert_attachment( $attachment, $file_return['url'] );

		  require_once(ABSPATH . 'wp-admin/includes/image.php');
		  $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
		  wp_update_attachment_metadata( $attachment_id, $attachment_data );

		  if( 0 < intval( $attachment_id ) ) {
			  return $attachment_id;
		  }
	  }

	  return false;
}

// Admin Bar

function remove_admin_bar()	{
	add_filter( 'show_admin_bar', '__return_false' );
}

if(!current_user_can("administrator") && xoption("profileAdminBar")) add_action('init', 'remove_admin_bar');

function upload_user_file( $file = array() ) {

	require_once( ABSPATH . 'wp-admin/includes/admin.php' );

	  $file_return = wp_handle_upload( $file, array('test_form' => false ) );

	  if( isset( $file_return['error'] ) || isset( $file_return['upload_error_handler'] ) ) {
		  return false;
	  } else {

		  $filename = $file_return['file'];

		  $attachment = array(
			  'post_mime_type' => $file_return['type'],
			  'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
			  'post_content' => '',
			  'post_status' => 'inherit',
			  'guid' => $file_return['url']
		  );

		  $attachment_id = wp_insert_attachment( $attachment, $file_return['url'] );

		  require_once(ABSPATH . 'wp-admin/includes/image.php');
		  $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
		  wp_update_attachment_metadata( $attachment_id, $attachment_data );

		  if( 0 < intval( $attachment_id ) ) {
			return $attachment_id;
		  }
	  }

	  return false;
}

// Register Scripts and Styles

	function safir_enqueue_style($name, $file) {
		wp_enqueue_style($name, get_template_directory_uri() . '/' . $file, [], filemtime(get_template_directory() . "/" . $file));
	}
	
	function safir_enqueue_script($name, $file) {
		wp_enqueue_script($name, get_template_directory_uri() . '/' . $file, ['jquery'], filemtime(get_template_directory() . '/' . $file), true );
	}
	
	function safir_register_script($name, $file) {	
		wp_register_script($name, get_template_directory_uri() . '/' . $file, ['jquery'], filemtime(get_template_directory() . '/' . $file), true );
	}


// Admin Icons 

	function safirAdminIconSelector($name, $icon) {
		?>
		<div class="safirAdminIconSelector">
			<div class="iconSelected">
				<?php safirIcon($icon); ?>
			</div>
			<input class="iconInput" autocomplete="off" name="<?php echo $name; ?>" id="<?php echo $name; ?>" type="text" value="<?php if ($x = $icon) echo($x); ?>" />
			<div class="iconList" style="display:none;">
				<div class="close">X</div>
				<?php
				global $safirIcons;
				foreach($safirIcons as $k) { ?>
					<div class="item <?php if($k == $icon) { echo 'active'; } ?>" data-icon="<?php echo $k; ?>">
						<div class="inner">
							<?php safirIcon($k); ?>
						</div>
					</div>
				<?php } ?>
			</div>
		</div>
		<?php
	}

	function safirIcon($icon = "icon1", $echo = true) {
		$icon = str_replace("icon:", "", $icon);
		$icon = trim(str_replace("metinsiz", "", $icon));
		if($echo) {
			echo '<span class="safiricon icon"><svg class="safiricon-'.$icon.'"><use href="#safiricon-'.$icon.'"></use></svg></span>';
		} else {
			return '<span class="safiricon icon"><svg class="safiricon-'.$icon.'"><use href="#safiricon-'.$icon.'"></use></svg></span>';
		}
	}

	function themeIcon($icon = "add", $echo = true) {
		$icon = str_replace("icon:", "", $icon);
		if($echo) {
			echo '<span class="themeicon icon"><svg class="themeicon-'.$icon.'"><use href="#themeicon-'.$icon.'"></use></svg></span>';
		} else {
			return '<span class="themeicon icon"><svg class="themeicon-'.$icon.'"><use href="#themeicon-'.$icon.'"></use></svg></span>';
		}
	}


// Resmi İlan Filter
add_action( 'pre_get_posts', 'safir_resmiilan_date_filter' );
function safir_resmiilan_date_filter( $query ) {

	if( $query->is_main_query() && !is_admin() && xoption('resmiilanCategory') && is_category(xoption('resmiilanCategory')) )  {
		$query->set( 'posts_per_page', -1 );
		$date = isset($_GET["tarih"]) ? $_GET["tarih"] : date("Y-m-d");
		if($date = strtotime($date)) {
			$query->set( 'date_query',
				[
					'year'  => date("Y", $date),
					'month' => date("m", $date),
					'day'   => date("d", $date),
				],
			);
		}
	}
}

// Safir Logo

	function safirLogo() {
		?>
		<a class="safir-logo" href="<?php bloginfo('url'); ?>">
			<?php if($x = xoption("logo")) : ?>
				<img src="<?php echo $x; ?>" alt="<?php bloginfo('name'); ?>" title="<?php bloginfo('name'); ?>" width="<?php echo xoption('logoWidth'); ?>" height="<?php echo xoption('logoHeight'); ?>" />
			<?php else: ?>
				<?php bloginfo('name'); ?>
			<?php endif; ?>
		</a>
		<?php
	}

function safir_enqueue_custom_fonts($fonts = "all") {
	global $safirFonts;
	global $safirSelectedFonts;
	if($fonts == "selected") $fontsToLoad = $safirSelectedFonts;
	if($fonts == "all") $fontsToLoad = $safirFonts;
	
	foreach ($fontsToLoad as $font => $data) {
		switch($data["source"]) {
			case "googlefonts":
				wp_enqueue_style('safir-'.$font.'-font', "https://fonts.googleapis.com/css2?family=". $data["urldata"] .":wght@300;400;500;600;700;900&display=swap");
			break;
			case "adobefonts":
				wp_enqueue_style('safir-'.$font.'-font', "https://use.typekit.net/". $data["urldata"] .".css");
			break;
		}
	}

}

// Fix Custom Page Permalinks
add_filter( 'page_link', 'safir_custom_page_link_filter', 10, 3 );
function safir_custom_page_link_filter( $link, $post_id, $sample ) {
	if (get_option('permalink_structure') != '' && substr($link, -1) != "/") {
		return $link . "/";
	} else {
		return $link;
	}
}

// Bileşen Linki

add_action('admin_bar_menu', 'safirWidgetsMenu', 400);
function safirWidgetsMenu() {
	global $wp_admin_bar;
	if (!is_super_admin() || !is_admin_bar_showing() || !is_home()) return;
	$wp_admin_bar->add_menu(
		[
			'id' => 'safirwidgetsmenu',
			'title' => '<span class="ab-icon dashicons dashicons-admin-home" style="margin-top: 2px"></span> Anasayfayı Düzenle',
			'href' => admin_url('widgets.php'),
		]
	);
}