<?php
/*-----------------------------------------------*/
/*  EXTHEM.ES
/*  PREMIUM WORDRESS THEMES
/*
/*  STOP DON'T TRY EDIT
/*  IF YOU DON'T KNOW PHP 
/*
/*  As Errors In Your Themes
/*  Are Not The Responsibility
/*  Of The DEVELOPERS
/*  @EXTHEM.ES
/*  
/*  Follow Social Media Exthem.es
/*  Youtube : https://www.youtube.com/channel/UCpcZNXk6ySLtwRSBN6fVyLA
/*  Facebook : https://www.facebook.com/groups/exthem.es
/*  Twitter : https://x.com/bangreyblog
/*  Instagram : https://www.instagram.com/exthem.es/
/*  Telegram : https://t.me/exthemes_helps
/*  TikTok : https://www.tiktok.com/@exthemes
/*	More Premium Themes Visit Now On https://exthem.es/
/*-----------------------------------------------*/
if ( ! defined( 'ABSPATH' ) ) exit; 
// Silence is golden.

global $opt_themes;

$title	= $gets_data['title_GP']; 
$post_status	= $opt_themes['apk_status'];
$post_title	= $opt_themes['apk_title']; 
//$post_permalink	= $opt_themes['apk_permalink'];
//$duplicatepost	= $opt_themes['duplicate_post'];


$post_titleX	= $gets_data['title_GP']; 
$title_post	= $gets_data[$post_title];
//$idX	= $post->ID;
$check_title	= get_page_by_title($title_post, 'OBJECT', $types);
 
/* 
if($duplicatepost){
	$post_args	= array(
		'post_title'		=> $title_post,
		'post_name'			=> sanitize_title_with_dashes_and_no_special_chars($gets_data[$post_permalink]),
		'post_content'		=> $post_article,
		'post_status'		=> $post_status,  
		'post_type'			=> $types
	);
	$post_id	= wp_insert_post( $post_args );
} 
*/
if ( ! $check_title ) {
	$post_args	= array(
		'post_title'		=> $title_post, 
		'post_content'		=> $post_article,
		'post_status'		=> $post_status,  
		'post_type'			=> $types
	);
	$post_id	= wp_insert_post( $post_args );
	
    //if ( $post_id && ! is_wp_error( $post_id ) ) {
    //    echo "Berhasil dibuat → ID: " . $post_id;
    //}
	
} else {
	$post_args	= array(
		'ID'	=> $check_title->ID, 
		'post_title'	=> $title_post,
		'post_content'	=> $post_article,
		'post_status'	=> $post_status,  
		'post_type'	=> $types
	);
	$post_id	= wp_update_post( $post_args ); 
	//echo "Sudah ada → ID: " . $post_id;
} 
 
/* 
if ( !empty($gets_data['genres_GP']) ) { 
	$terms	= array();  
	$genres	= $gets_data['genres_GP']; 
	foreach($genres as $term):
	$t_exists		= term_exists( $term, 'category' );
		if( !$t_exists ):
			$t			= wp_insert_term( $term, 'category' );
			$terms[]	= $t['term_id'];
		else:
			$terms[]	= $t_exists['term_id'];
		endif;
	endforeach;
	wp_set_post_terms( $post_id, $terms, 'category' );
} 
*/