forked from modiqi/git-it-write
替换 Forgejo API 数据源为本地文件系统读取,支持 30+ 仓库自动发现。 新增 WP-Cron 定时同步、WP-CLI 命令、多站点支持、SEO 字段写入。 base_directory 为空时保留原 API 模式向后兼容。 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
129 lines
3.6 KiB
PHP
129 lines
3.6 KiB
PHP
<?php
|
|
/*
|
|
Plugin Name: Git it Write - Forgejo
|
|
Plugin URI: https://feicode.com/WenPai-org/git-it-write
|
|
Description: Publish markdown files present in a Forgejo repository as posts to WordPress automatically
|
|
Author: Aakash Chakravarthy
|
|
Author URI: https://www.aakashweb.com/
|
|
Version: 3.0
|
|
*/
|
|
|
|
define( 'GIW_VERSION', '3.0' );
|
|
define( 'GIW_PATH', plugin_dir_path( __FILE__ ) ); // All have trailing slash
|
|
define( 'GIW_ADMIN_URL', trailingslashit( plugin_dir_url( __FILE__ ) . 'admin' ) );
|
|
|
|
final class Git_It_Write{
|
|
|
|
public static function init(){
|
|
|
|
self::includes();
|
|
|
|
// Register deactivation hook to clean up cron.
|
|
register_deactivation_hook( __FILE__, array( __CLASS__, 'deactivate' ) );
|
|
|
|
}
|
|
|
|
public static function includes(){
|
|
|
|
require __DIR__ . '/vendor/autoload.php';
|
|
|
|
require_once( GIW_PATH . 'includes/utilities.php' );
|
|
require_once( GIW_PATH . 'includes/publisher.php' );
|
|
require_once( GIW_PATH . 'includes/publish-handler.php' );
|
|
require_once( GIW_PATH . 'includes/parsedown.php' );
|
|
require_once( GIW_PATH . 'includes/webhook.php' );
|
|
require_once( GIW_PATH . 'includes/shortcodes.php' );
|
|
|
|
if( self::is_local_mode() ){
|
|
require_once( GIW_PATH . 'includes/local-repository.php' );
|
|
require_once( GIW_PATH . 'includes/repo-config.php' );
|
|
require_once( GIW_PATH . 'includes/discovery.php' );
|
|
require_once( GIW_PATH . 'includes/sync.php' );
|
|
GIW_Sync::init();
|
|
|
|
if( defined( 'WP_CLI' ) && WP_CLI ){
|
|
require_once( GIW_PATH . 'includes/cli.php' );
|
|
}
|
|
}else{
|
|
require_once( GIW_PATH . 'includes/repository.php' );
|
|
}
|
|
|
|
require_once( GIW_PATH . 'admin/admin.php' );
|
|
|
|
}
|
|
|
|
/**
|
|
* Check if local file mode is active (base_directory is set).
|
|
*/
|
|
public static function is_local_mode(){
|
|
$settings = self::general_settings();
|
|
return !empty( $settings['base_directory'] );
|
|
}
|
|
|
|
public static function default_config(){
|
|
return array(
|
|
'username' => '',
|
|
'repository' => '',
|
|
'folder' => '',
|
|
'branch' => 'master',
|
|
'post_type' => '',
|
|
'post_author' => 1,
|
|
'content_template' => '%%content%%',
|
|
'last_publish' => 0
|
|
);
|
|
}
|
|
|
|
public static function default_general_settings(){
|
|
return array(
|
|
'webhook_secret' => '',
|
|
'forgejo_url' => '',
|
|
'forgejo_access_token' => '',
|
|
// Local mode settings
|
|
'base_directory' => '',
|
|
'default_post_type' => 'post',
|
|
'default_post_author' => 1,
|
|
'sync_interval' => 'hourly',
|
|
'target_blog_id' => 0,
|
|
);
|
|
}
|
|
|
|
public static function allowed_file_types(){
|
|
return array(
|
|
'md'
|
|
);
|
|
}
|
|
|
|
public static function all_repositories(){
|
|
|
|
$repos_raw = get_option( 'giw_repositories', array( array() ) );
|
|
$repos = array();
|
|
$default_config = self::default_config();
|
|
|
|
foreach( $repos_raw as $id => $config ){
|
|
array_push( $repos, wp_parse_args( $config, $default_config ) );
|
|
}
|
|
|
|
return $repos;
|
|
|
|
}
|
|
|
|
public static function general_settings(){
|
|
|
|
$settings = get_option( 'giw_general_settings', array() );
|
|
$default_settings = self::default_general_settings();
|
|
|
|
return wp_parse_args( $settings, $default_settings );
|
|
|
|
}
|
|
|
|
public static function deactivate(){
|
|
if( class_exists( 'GIW_Sync' ) ){
|
|
GIW_Sync::unschedule();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
Git_It_Write::init();
|
|
|
|
?>
|