'0', $text_domain . '_language' => 'en', $text_domain . '_trans_for' => '', ); foreach ($meta_box as $key => $value) { add_site_option( $key, $value ); } } /* load file json map */ global $language_suport; $dir = astrans_plugin_dir . 'inc/maps/'; $list_file = scandir($dir); $list_lang['en'] = 'English'; for ($i=2; $i < count($list_file); $i++) { $string = file_get_contents(astrans_plugin_url . 'inc/maps/' . $list_file[$i]); $map = json_decode($string, true); $short = $map['short']; $list_lang[$short] = $map["lang"]; SlugTransAstrans::$maps[$short] = $map[$short]; } $language_suport = $list_lang; global $astrans_pages; $astrans_pages = 10; /* ================== */ /* Add column to MSQL */ /* ================== */ global $wpdb; /* add column astrans to table wp_posts. Value default 0 ( 0 : false, 1 : true) */ $row = $wpdb->get_results( "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '".$wpdb->prefix."posts' AND column_name = 'astrans'" ); if(empty($row)){ $wpdb->query("ALTER TABLE ".$wpdb->prefix."posts ADD astrans INT(1) NOT NULL DEFAULT 0"); } /* add column astrans to table wp_terms. Value default 0 ( 0 : false, 1 : true) */ $row = $wpdb->get_results( "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '".$wpdb->prefix."terms' AND column_name = 'astrans'" ); if(empty($row)){ $wpdb->query("ALTER TABLE ".$wpdb->prefix."terms ADD astrans INT(1) NOT NULL DEFAULT 0"); } } add_action( 'init', 'setup_astrans' ); } /* register menu */ if(!function_exists('wpdocs_register_astrans_menu_page')){ function wpdocs_register_astrans_menu_page(){ add_menu_page( __( 'Advanced Slug Translate', 'AST' ), /* title plugin */ 'ASTranslate', /* name menu plugin */ 'manage_options', 'astrans', /* slug plugin */ null, /* call function view if plugin */ astrans_plugin_url . '/assets/images/ast.png', /* icon plugin */ 81 /* position plugin after setting */ ); add_submenu_page( 'astrans', 'Setting', 'Setting', 'manage_options', 'astrans', 'index_astrans_plugin' ); add_submenu_page( 'astrans', 'Sync Slug', 'Sync Slug', 'manage_options', 'astrans_sync', 'astrans_sync' ); } add_action( 'admin_menu', 'wpdocs_register_astrans_menu_page' ); } /** * Display a custom menu page */ function index_astrans_plugin(){ require_once dirname( __FILE__ ) . '/inc/index.php'; } function astrans_sync(){ require_once dirname( __FILE__ ) . '/inc/sync.php'; } /* call sidebar */ function hook_astrans_sidebar(){ require_once dirname( __FILE__ ) . '/inc/astrans_sidebar.php'; } add_action( 'astrans_sidebar', 'hook_astrans_sidebar', 1 ); /* add js css admin */ function hook_astrans_admin($hook) { wp_enqueue_media(); wp_enqueue_style( 'astrans_css', astrans_plugin_url . '/assets/css/astrans-admin.css' ); wp_enqueue_script( 'astrans_js', astrans_plugin_url . '/assets/js/astrans-admin.js' ); } add_action( 'admin_enqueue_scripts', 'hook_astrans_admin', 10 ); /* (^ _ ^) code (@ _ @) */ /* Automation Translate */ if(get_site_option('astrans_status') == 1 ){ /* hook slug page & post */ add_filter( 'wp_unique_post_slug', 'custom_unique_post_slug_astrans', 10, 4 ); function custom_unique_post_slug_astrans( $slug, $post_ID, $post_status, $post_type ) { $post = get_post($post_ID); if($post->post_status != 'publish' && $post->post_status != 'private' && $post->post_name == ''){ $p_type = json_decode(str_replace('\"', '"', get_site_option( 'astrans_trans_for' )), true); if($p_type['astrans_pages'] == '1' && $post_type == 'page'){ $slug = trans_slug_astrans($post_ID, $slug, $post_type); }else if( $p_type['astrans_posts'] == '1' && $post_type == 'post'){ $slug = trans_slug_astrans($post_ID, $slug, $post_type); } save_trans_astrans($post_ID, $post_type); } return $slug; } /* hook slug category (not taxonomy) */ add_action('wp_unique_term_slug', 'custom_created_term_astrans', 10, 4); function custom_created_term_astrans( $slug, $term ) { $p_type = json_decode(str_replace('\"', '"', get_site_option( 'astrans_trans_for' )), true); if($p_type['astrans_cate'] == '1' && $term->taxonomy == 'category'){ $lang_site = get_site_option('astrans_language'); $s = SlugTransAstrans::downcode($term->name ,$lang_site ); if($s != $term->name){ $s = str_replace(' ', '-', $s); $s = strtolower($s); $i = 0; $check = $s; while (slug_cate_exit_astrans($check) == false) { $i++; $check = $s . '-' . $i; } return $check; save_trans_astrans($term->term_id ,$term->taxonomy); } } return $slug; } } function trans_slug_astrans($post_ID, $slug_default, $post_type){ $lang_site = get_site_option('astrans_language'); $post = get_post($post_ID); $slug = SlugTransAstrans::downcode($post->post_title ,$lang_site ); if($slug != $post->post_title){ $slug = str_replace(' ', '-', $slug); $slug = strtolower($slug); $check = $slug; $i = 0; while (intval(slug_post_exit_astrans($check, $post_type)) != 0) { $i++; $check = $slug . '-' . $i; if($i >= 100){ break; } } $slug = $check; }else{ $slug = $slug_default; } return $slug; } function slug_cate_exit_astrans($slug, $name = 'category', $id = 0){ if( (term_exists($slug, $name) != '') && ( intval(term_exists($slug, $name)["term_id"]) != intval($id)) ){ return false; }else if( (term_exists($slug, $name) != '') && ( intval(term_exists($slug, $name)["term_id"]) != intval($id)) ){ return true; } return true; } function slug_post_exit_astrans($slug, $post_type = 'post', $id = 0){ global $wpdb; $query = "SELECT ID FROM $wpdb->posts WHERE post_name = '".$slug."' && post_type = '".$post_type."'"; $a = $wpdb->get_results($query); if( (count($a) != 0) && intval($a[0]->ID) != intval($id)){ return $a[0]->ID; }else if(count($a) != 0 && intval($a[0]->ID) == intval($id) ){ return 0; } return 0; } /* Ajax save option astrans */ add_action( 'wp_ajax_save_astrans_astrans', 'save_astrans_astrans'); function save_astrans_astrans() { $status = true; $mess = 'Successfully Saved'; if(isset($_GET['astrans_status']) && $_GET['astrans_status'] != "" ){ $status = strval($_GET['astrans_status']); $language = strval($_GET['astrans_language']); $for = strval($_GET['astrans_trans_for']); update_option( 'astrans_language', $language ); update_option( 'astrans_status', $status ); if($status == '1'){ update_option( 'astrans_trans_for', $for); } $status = true; $mess = 'Successfully Saved'; }else{ $status = false; $mess = 'Errors - Process SAVE can not be completed, please try again or contact support'; } $data = array( 'status' => $status, 'messenger' => $mess ); wp_send_json_success( $data ); } /* Ajax sync load */ add_action( 'wp_ajax_table_sync_astrans', 'table_sync_astrans'); function table_sync_astrans() { global $astrans_pages; if(isset($_GET['pre_page']) && $_GET['pre_page'] != ''){ $astrans_pages = intval($_GET['pre_page']); }else{ $astrans_pages = 10; } if(isset($_GET['post_type']) && $_GET['post_type'] != ''){ $type = strval($_GET['post_type']); }else{ $type = 'post'; } if(!isset($_GET['filter']) || $_GET['filter'] == ''){ $filter = 0; }else{ $filter = intval($_GET['filter']); } if(isset($_GET['keyworks'])){ $keyworks = strval($_GET['keyworks']); }else{ $keyworks = ""; } $paged = $_GET['paged'] != '' ? intval($_GET['paged']) : 1; $args = array( 'type' => $type, 'filter' => $filter, 'keyworks' => $keyworks, 'offset' => 0, ); $posts = get_astrans($args); if($posts != ''){ if($paged == 1 ){ $to = 0; $from = $astrans_pages - 1; if($from >= count($posts) || $astrans_pages == 0){ $from = count($posts) - 1; } }else{ $to = ($paged - 1) * $astrans_pages; $from = $paged * $astrans_pages - 1; if($from >= count($posts)){ $from = count($posts) - 1; } } for ($i= $to; $i <= $from ; $i++) { $post = $posts[$i]; ?>