184 lines
5.0 KiB
PHP
184 lines
5.0 KiB
PHP
<?php
|
|
/**
|
|
* Plugin Name: WP Agentic Writer
|
|
* Plugin URI: https://github.com/wp-agentic-writer
|
|
* Description: Plan-first AI writing workflow for WordPress. Scribble → Research → Plan → Execute → Revise
|
|
* Version: 0.1.3
|
|
* Author: WP Agentic Writer
|
|
* Author URI: https://github.com/wp-agentic-writer
|
|
* License: GPL-2.0+
|
|
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
|
|
* Text Domain: wp-agentic-writer
|
|
* Domain Path: /languages
|
|
* Requires at least: 6.6
|
|
* Requires PHP: 7.4
|
|
*
|
|
* @package WP_Agentic_Writer
|
|
*/
|
|
|
|
// Exit if accessed directly.
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
// Plugin version.
|
|
define( 'WP_AGENTIC_WRITER_VERSION', '0.1.3' );
|
|
|
|
// Plugin file path.
|
|
define( 'WP_AGENTIC_WRITER_FILE', __FILE__ );
|
|
|
|
// Plugin directory path.
|
|
define( 'WP_AGENTIC_WRITER_DIR', plugin_dir_path( __FILE__ ) );
|
|
|
|
// Plugin directory URL (plugin_dir_url already includes trailing slash).
|
|
define( 'WP_AGENTIC_WRITER_URL', untrailingslashit( plugin_dir_url( __FILE__ ) ) . '/' );
|
|
|
|
// Include autoloader.
|
|
require_once WP_AGENTIC_WRITER_DIR . 'includes/class-autoloader.php';
|
|
|
|
// Initialize the plugin.
|
|
function wp_agentic_writer_init() {
|
|
// Load plugin text domain.
|
|
load_plugin_textdomain(
|
|
'wp-agentic-writer',
|
|
false,
|
|
dirname( plugin_basename( __FILE__ ) ) . '/languages/'
|
|
);
|
|
|
|
// Always initialize Gutenberg sidebar for REST API routes.
|
|
WP_Agentic_Writer_Gutenberg_Sidebar::get_instance();
|
|
|
|
// Always initialize cost tracker hooks (REST API calls need this).
|
|
WP_Agentic_Writer_Cost_Tracker::get_instance();
|
|
|
|
// Check if we're on the admin side.
|
|
if ( is_admin() ) {
|
|
// Initialize settings - V2 is now the default.
|
|
$use_settings_v2 = true;
|
|
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
|
if ( isset( $_GET['wpaw_settings_v2'] ) ) {
|
|
$use_settings_v2 = ( $_GET['wpaw_settings_v2'] === '1' );
|
|
}
|
|
|
|
// Also check if this is an AJAX request for V2 endpoints.
|
|
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
|
if ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_POST['action'] ) ) {
|
|
$v2_ajax_actions = array(
|
|
'wpaw_get_cost_log_data',
|
|
'wpaw_get_header_stats',
|
|
'wpaw_test_api_connection',
|
|
'wpaw_refresh_models',
|
|
);
|
|
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
|
if ( in_array( $_POST['action'], $v2_ajax_actions, true ) ) {
|
|
$use_settings_v2 = true;
|
|
}
|
|
}
|
|
|
|
if ( $use_settings_v2 ) {
|
|
require_once WP_AGENTIC_WRITER_DIR . 'includes/class-settings-v2.php';
|
|
WP_Agentic_Writer_Settings_V2::get_instance();
|
|
} else {
|
|
WP_Agentic_Writer_Settings::get_instance();
|
|
}
|
|
|
|
// Initialize admin columns.
|
|
WP_Agentic_Writer_Admin_Columns::get_instance();
|
|
}
|
|
|
|
// Debug: Log plugin URL (only when SCRIPT_DEBUG is enabled).
|
|
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
|
|
error_log( 'WP Agentic Writer URL: ' . WP_AGENTIC_WRITER_URL );
|
|
error_log( 'WP Agentic Writer DIR: ' . WP_AGENTIC_WRITER_DIR );
|
|
}
|
|
}
|
|
add_action( 'plugins_loaded', 'wp_agentic_writer_init' );
|
|
|
|
// Activation hook.
|
|
register_activation_hook( __FILE__, 'wp_agentic_writer_activate' );
|
|
|
|
/**
|
|
* Plugin activation.
|
|
*
|
|
* @since 0.1.0
|
|
*/
|
|
function wp_agentic_writer_activate() {
|
|
// Set default options.
|
|
$default_options = array(
|
|
'openrouter_api_key' => '',
|
|
'planning_model' => 'google/gemini-2.0-flash-exp',
|
|
'execution_model' => 'anthropic/claude-sonnet-4-20250514',
|
|
'image_model' => 'openai/gpt-4o',
|
|
'web_search_enabled' => false,
|
|
'search_engine' => 'auto',
|
|
'search_depth' => 'medium',
|
|
'cost_tracking_enabled' => true,
|
|
'chat_history_limit' => 20,
|
|
'preferred_languages' => array( 'auto', 'English', 'Indonesian' ),
|
|
'custom_languages' => array(),
|
|
);
|
|
|
|
add_option( 'wp_agentic_writer_settings', $default_options );
|
|
|
|
// Create cost tracking table.
|
|
wp_agentic_writer_create_cost_table();
|
|
}
|
|
|
|
/**
|
|
* Create cost tracking table.
|
|
*
|
|
* @since 0.1.0
|
|
*/
|
|
function wp_agentic_writer_create_cost_table() {
|
|
global $wpdb;
|
|
|
|
$table_name = $wpdb->prefix . 'wpaw_cost_tracking';
|
|
$charset_collate = $wpdb->get_charset_collate();
|
|
|
|
$sql = "CREATE TABLE $table_name (
|
|
id bigint(20) NOT NULL AUTO_INCREMENT,
|
|
post_id bigint(20) NOT NULL,
|
|
model varchar(255) NOT NULL,
|
|
action varchar(50) NOT NULL,
|
|
input_tokens int(11) NOT NULL,
|
|
output_tokens int(11) NOT NULL,
|
|
cost decimal(10,6) NOT NULL,
|
|
created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
|
PRIMARY KEY (id),
|
|
KEY post_id (post_id)
|
|
) $charset_collate;";
|
|
|
|
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
|
|
dbDelta( $sql );
|
|
}
|
|
|
|
// Deactivation hook.
|
|
register_deactivation_hook( __FILE__, 'wp_agentic_writer_deactivate' );
|
|
|
|
/**
|
|
* Plugin deactivation.
|
|
*
|
|
* @since 0.1.0
|
|
*/
|
|
function wp_agentic_writer_deactivate() {
|
|
// Cleanup if needed.
|
|
}
|
|
|
|
// Uninstall hook.
|
|
register_uninstall_hook( __FILE__, 'wp_agentic_writer_uninstall' );
|
|
|
|
/**
|
|
* Plugin uninstall.
|
|
*
|
|
* @since 0.1.0
|
|
*/
|
|
function wp_agentic_writer_uninstall() {
|
|
// Delete options.
|
|
delete_option( 'wp_agentic_writer_settings' );
|
|
|
|
// Delete cost tracking table.
|
|
global $wpdb;
|
|
$table_name = $wpdb->prefix . 'wpaw_cost_tracking';
|
|
$wpdb->query( "DROP TABLE IF EXISTS $table_name" );
|
|
}
|