ãã¯ããããããŸã.LLMO察çã®åæé ãšããŠãããŒã¯ããŠã³èšæ³ãæœããŸãã.ã·ã³ã°ã«ããŒãžã«ããŒã¯ããŠã³èšæ³ã®ãªã³ã¯ãããããã®èšäºã«ãããšæããŸã.
ãã®ãªã³ã¯ãã¯ãªãã¯ãããšããŒã¯ããŠã³èšæ³ã衚瀺ãããŸã.LLMOèšçœ®åŸãLLMs.txtãèšçœ®ããã°è¯ããããã®ã§ããã¡ããè¿æ¥äžã«å¯Ÿå¿ããäºå®ã§ã.å°ããã®èšäºãUPãããŠããé ã«ã¯LLMs.txtã®èšçœ®ãçµãã£ãŠããæ°ãããŸã.
远䌞ïŒLLMs.txtã®èšçœ®ãçµãããŸãããã«ãŒãã«èšçœ®ããŠããŸã.LLMs.txtã¯ããŒã¯ããŠã³èšæ³ãèšèŒãããŠããURLïŒãªã³ã¯ïŒãäžè¡ãã€è¡šç€ºãããããªåœ¢åŒã§è¯ããããã§ã.
ããããã£ãããŒããã«ãã©ã°ã€ã³ã«ããŠè²°ããŸããã®ã§ã裟åãã§ã.äžèšã®ãã©ã°ã€ã³ãå°å
¥ãããšå
šèšäºURLïŒãã©ã¡ãŒã¿ãŒïŒããŒã¯ããŠã³èšæ³ãšèªèãããïŒïŒãçºè¡ãããŸã.
â»ããŒã¯ããŠã³èšæ³ããããŒãžã¯ãèªèº«ã§äœãå¿
èŠããããŸã.
<?php
/*
Plugin Name: LLMS URL Exporter (Fixed)
Description: å
¬éèšäºã®URLãuploads/llms.txt ã«æ¥ä»éé ã§åºåããŸãã管çç»é¢ã®ãã¿ã³ã§éåæïŒAJAXïŒå®è¡ãæ°èŠå
¬éæã¯å
é ã«è¿œèšã
Version: 1.1
Author: taoka toshiaki
*/
if ( ! defined( 'ABSPATH' ) ) exit;
class LLMS_URL_Exporter {
private $file;
public function __construct() {
$this->file = $_SERVER['DOCUMENT_ROOT'] . '/llms.txt';
add_action( 'admin_menu', [ $this, 'add_admin_page' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_script' ] );
// AJAX (管çç»é¢ããã®åŒã³åºã = ãã°ã€ã³ãŠãŒã¶ãŒçš)
add_action( 'wp_ajax_llms_generate_urls', [ $this, 'generate_urls' ] );
// æçš¿ãå
¬éã«ãªã£ããšãïŒç¶æ
é·ç§»ãç£èŠïŒ
add_action( 'transition_post_status', [ $this, 'on_transition_post_status' ], 10, 3 );
register_activation_hook( __FILE__, [ $this, 'create_file_on_activate' ] );
}
public function create_file_on_activate() {
$dir = dirname( $this->file );
if ( ! is_dir( $dir ) ) {
wp_mkdir_p( $dir );
}
if ( ! file_exists( $this->file ) ) {
@file_put_contents( $this->file, "" );
}
}
public function add_admin_page() {
add_menu_page(
'LLMS URL Exporter',
'LLMS Export',
'manage_options',
'llms-url-exporter',
[ $this, 'admin_page_html' ]
);
}
public function enqueue_admin_script( $hook ) {
// 管çç»é¢ã®åœè©²ããŒãžã ãã«èªã¿èŸŒã
if ( $hook !== 'toplevel_page_llms-url-exporter' ) return;
// jQuery ã¯ç®¡çç»é¢ã«æ¢ã«ååš
wp_enqueue_script( 'llms-admin', plugins_url( 'llms-admin.js', __FILE__ ), [ 'jquery' ], '1.0', true );
wp_localize_script( 'llms-admin', 'LLMS_Ajax', [
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'llms_generate_urls_nonce' ),
] );
// ããå€éšãã¡ã€ã«ãçšæããªããªããadmin_page_html å
ã® inline script ããã©ãŒã«ããã¯ã«ãªããŸãã
}
public function admin_page_html() {
?>
<div class="wrap">
<h1>LLMS URL Exporter</h1>
<p>å
¬éäžã®å
šæçš¿URLã <code>/llms.txt</code> ã«æ¥ä»éé ã§æžãåºããŸãã</p>
<button id="llms-generate-btn" class="button button-primary">å
šèšäºURLãæžãåºã</button>
<div id="llms-result" style="margin-top:12px;"></div>
</div>
<?php
// ãã©ãŒã«ããã¯çšã®ã€ã³ã©ã€ã³ã¹ã¯ãªããïŒããå€éš JS ãäœããªãå Žåã«æå¹ïŒ
$nonce = wp_create_nonce( 'llms_generate_urls_nonce' );
$ajax_url = admin_url( 'admin-ajax.php' );
?>
<script type="text/javascript">
jQuery(function($){
$('#llms-generate-btn').on('click', function(){
var $res = $('#llms-result');
$res.text('åŠçäž...');
var data = {
action: 'llms_generate_urls',
_ajax_nonce: (typeof LLMS_Ajax !== 'undefined' ? LLMS_Ajax.nonce : '<?php echo esc_js( $nonce ); ?>')
};
var url = (typeof LLMS_Ajax !== 'undefined' ? LLMS_Ajax.ajax_url : '<?php echo esc_js( $ajax_url ); ?>');
$.post(url, data, function(response){
if ( response && response.success ) {
$res.html( response.data.message );
} else if ( response && response.data && response.data.message ) {
$res.html('ãšã©ãŒ: ' + response.data.message);
} else {
$res.html('äžæãªã¬ã¹ãã³ã¹: ' + JSON.stringify(response));
}
}).fail(function(jqXHR, textStatus, errorThrown){
$res.html('AJAX ãšã©ãŒ: ' + textStatus + ' ' + errorThrown + ' (status: ' + jqXHR.status + ')');
});
});
});
</script>
<?php
}
// AJAX ãã³ãã©
public function generate_urls() {
// nonce ãšæš©éãã§ãã¯
check_ajax_referer( 'llms_generate_urls_nonce', '_ajax_nonce' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( [ 'message' => 'æš©éããããŸããã' ], 403 );
}
$args = [
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => -1,
'no_found_rows' => true,
'fields' => 'ids',
];
$posts = get_posts( $args );
$urls = [];
foreach ( $posts as $pid ) {
$permalink = get_permalink( $pid );
if ( $permalink ) $urls[] = $permalink.'?md=1';
}
$content = implode("\n", $urls);
$written = @file_put_contents( $this->file, $content );
if ( $written === false ) {
$err = error_get_last();
$msg = isset( $err['message'] ) ? $err['message'] : 'äžæãªæžã蟌ã¿ãšã©ãŒ';
wp_send_json_error( [ 'message' => 'ãã¡ã€ã«æžã蟌ã¿ã«å€±æããŸãã: ' . $msg ] );
}
wp_send_json_success( [ 'message' => 'llms.txt ã« ' . count( $urls ) . ' ä»¶ã®URLãæžãåºããŸããã' ] );
}
// å
¬éå€å®ïŒç¶æ
é·ç§»ã§ publish ã«ãªã£ããšãã«å
é ãžè¿œå
public function on_transition_post_status( $new_status, $old_status, $post ) {
if ( $post->post_type !== 'post' ) return;
if ( $new_status === 'publish' && $old_status !== 'publish' ) {
$url = get_permalink( $post->ID ).'?md=1';
$current = '';
if ( file_exists( $this->file ) ) {
$current = file_get_contents( $this->file );
}
// æ¢åã®åäžURLãããã°åé€ããŠå
é ãž
$lines = array_filter( array_map( 'trim', explode("\n", $current ) ) );
$lines = array_values( array_diff( $lines, [ $url ] ) );
array_unshift( $lines, $url );
$new_content = implode("\n", $lines);
@file_put_contents( $this->file, $new_content );
}
}
}
new LLMS_URL_Exporter();