Rename plugin to wp-seoprostack-plugin, update file structure
This commit is contained in:
@ -118,7 +118,7 @@ class WP_Allstars_Auto_Upload {
|
||||
*/
|
||||
public function log_error($url, $error) {
|
||||
error_log(sprintf(
|
||||
'[WP ALLSTARS] Auto Upload Images Error - URL: %s, Error: %s',
|
||||
'[SEO Pro Stack] Auto Upload Images Error - URL: %s, Error: %s',
|
||||
$url,
|
||||
$error
|
||||
));
|
69
includes/core/class-seoprostack-autoloader.php
Normal file
69
includes/core/class-seoprostack-autoloader.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
/**
|
||||
* Autoloader for SEO Pro Stack plugin.
|
||||
*
|
||||
* @package SEO_Pro_Stack
|
||||
* @subpackage SEO_Pro_Stack/Core
|
||||
*/
|
||||
|
||||
// If this file is called directly, abort.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class SEOProStack_Autoloader
|
||||
*
|
||||
* Handles autoloading of plugin classes.
|
||||
*/
|
||||
class SEOProStack_Autoloader {
|
||||
/**
|
||||
* Register the autoloader
|
||||
*/
|
||||
public static function register() {
|
||||
spl_autoload_register(array(self::class, 'autoload'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Autoload a class file based on its name
|
||||
*
|
||||
* @param string $class_name The name of the class to load.
|
||||
*/
|
||||
public static function autoload($class_name) {
|
||||
// Check if the class should be loaded by this autoloader
|
||||
if (false === strpos($class_name, 'SEOProStack')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Convert class name to filename
|
||||
$file_path = str_replace('SEOProStack_', '', $class_name);
|
||||
$file_path = str_replace('_', '-', $file_path);
|
||||
$file_path = strtolower($file_path);
|
||||
|
||||
// Add 'class-' prefix
|
||||
$file_path = 'class-' . $file_path . '.php';
|
||||
|
||||
// Base paths to check for classes
|
||||
$base_paths = array(
|
||||
SEOPROSTACK_PLUGIN_DIR . 'includes/',
|
||||
SEOPROSTACK_PLUGIN_DIR . 'includes/core/',
|
||||
SEOPROSTACK_PLUGIN_DIR . 'includes/features/',
|
||||
SEOPROSTACK_PLUGIN_DIR . 'includes/features/auto-upload/',
|
||||
SEOPROSTACK_PLUGIN_DIR . 'admin/',
|
||||
SEOPROSTACK_PLUGIN_DIR . 'admin/settings/',
|
||||
SEOPROSTACK_PLUGIN_DIR . 'admin/settings/ajax/',
|
||||
SEOPROSTACK_PLUGIN_DIR . 'admin/settings/tabs/',
|
||||
);
|
||||
|
||||
// Try to find and load the file
|
||||
foreach ($base_paths as $base_path) {
|
||||
$full_path = $base_path . $file_path;
|
||||
if (file_exists($full_path)) {
|
||||
require_once $full_path;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SEOProStack_Autoloader::register();
|
102
includes/core/class-seoprostack-loader.php
Normal file
102
includes/core/class-seoprostack-loader.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?php
|
||||
/**
|
||||
* Register all actions and filters for the plugin.
|
||||
*
|
||||
* @package SEO_Pro_Stack
|
||||
* @subpackage SEO_Pro_Stack/Core
|
||||
*/
|
||||
|
||||
// If this file is called directly, abort.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register all actions and filters for the plugin.
|
||||
*/
|
||||
class SEOProStack_Loader {
|
||||
|
||||
/**
|
||||
* The array of actions registered with WordPress.
|
||||
*
|
||||
* @var array $actions The actions registered with WordPress.
|
||||
*/
|
||||
protected $actions;
|
||||
|
||||
/**
|
||||
* The array of filters registered with WordPress.
|
||||
*
|
||||
* @var array $filters The filters registered with WordPress.
|
||||
*/
|
||||
protected $filters;
|
||||
|
||||
/**
|
||||
* Initialize the collections used to maintain the actions and filters.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->actions = array();
|
||||
$this->filters = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new action to the collection.
|
||||
*
|
||||
* @param string $hook The name of the WordPress action that is being registered.
|
||||
* @param object $component A reference to the instance of the object.
|
||||
* @param string $callback The name of the function definition on the component.
|
||||
* @param int $priority Optional. The priority at which the function should be fired. Default is 10.
|
||||
* @param int $accepted_args Optional. The number of arguments that should be passed to the callback. Default is 1.
|
||||
*/
|
||||
public function add_action($hook, $component, $callback, $priority = 10, $accepted_args = 1) {
|
||||
$this->actions = $this->add($this->actions, $hook, $component, $callback, $priority, $accepted_args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new filter to the collection.
|
||||
*
|
||||
* @param string $hook The name of the WordPress filter that is being registered.
|
||||
* @param object $component A reference to the instance of the object.
|
||||
* @param string $callback The name of the function definition on the component.
|
||||
* @param int $priority Optional. The priority at which the function should be fired. Default is 10.
|
||||
* @param int $accepted_args Optional. The number of arguments that should be passed to the callback. Default is 1.
|
||||
*/
|
||||
public function add_filter($hook, $component, $callback, $priority = 10, $accepted_args = 1) {
|
||||
$this->filters = $this->add($this->filters, $hook, $component, $callback, $priority, $accepted_args);
|
||||
}
|
||||
|
||||
/**
|
||||
* A utility function that is used to register the actions and hooks.
|
||||
*
|
||||
* @param array $hooks The collection of hooks that is being registered.
|
||||
* @param string $hook The name of the WordPress filter that is being registered.
|
||||
* @param object $component A reference to the instance of the object.
|
||||
* @param string $callback The name of the function definition on the component.
|
||||
* @param int $priority The priority at which the function should be fired.
|
||||
* @param int $accepted_args The number of arguments that should be passed to the callback.
|
||||
* @return array The collection of actions and filters registered.
|
||||
*/
|
||||
private function add($hooks, $hook, $component, $callback, $priority, $accepted_args) {
|
||||
$hooks[] = array(
|
||||
'hook' => $hook,
|
||||
'component' => $component,
|
||||
'callback' => $callback,
|
||||
'priority' => $priority,
|
||||
'accepted_args' => $accepted_args,
|
||||
);
|
||||
|
||||
return $hooks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the filters and actions with WordPress.
|
||||
*/
|
||||
public function run() {
|
||||
foreach ($this->filters as $hook) {
|
||||
add_filter($hook['hook'], array($hook['component'], $hook['callback']), $hook['priority'], $hook['accepted_args']);
|
||||
}
|
||||
|
||||
foreach ($this->actions as $hook) {
|
||||
add_action($hook['hook'], array($hook['component'], $hook['callback']), $hook['priority'], $hook['accepted_args']);
|
||||
}
|
||||
}
|
||||
}
|
69
includes/core/class-seoprostack-plugin.php
Normal file
69
includes/core/class-seoprostack-plugin.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
/**
|
||||
* The core plugin class.
|
||||
*
|
||||
* @package SEO_Pro_Stack
|
||||
* @subpackage SEO_Pro_Stack/Core
|
||||
*/
|
||||
|
||||
// If this file is called directly, abort.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* The core plugin class.
|
||||
*/
|
||||
class SEOProStack_Plugin {
|
||||
|
||||
/**
|
||||
* The loader that's responsible for maintaining and registering all hooks.
|
||||
*
|
||||
* @var SEOProStack_Loader $loader Maintains and registers all hooks.
|
||||
*/
|
||||
protected $loader;
|
||||
|
||||
/**
|
||||
* Define the core functionality of the plugin.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->load_dependencies();
|
||||
$this->define_admin_hooks();
|
||||
$this->define_features();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the required dependencies for this plugin.
|
||||
*/
|
||||
private function load_dependencies() {
|
||||
// The class responsible for orchestrating the actions and filters
|
||||
require_once SEOPROSTACK_PLUGIN_DIR . 'includes/core/class-seoprostack-loader.php';
|
||||
$this->loader = new SEOProStack_Loader();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register all of the hooks related to the admin area.
|
||||
*/
|
||||
private function define_admin_hooks() {
|
||||
// Admin functionality
|
||||
require_once SEOPROSTACK_PLUGIN_DIR . 'admin/class-seoprostack-admin.php';
|
||||
$admin = new SEOProStack_Admin();
|
||||
$admin->initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register all of the hooks related to plugin features.
|
||||
*/
|
||||
private function define_features() {
|
||||
// Auto Upload feature
|
||||
require_once SEOPROSTACK_PLUGIN_DIR . 'includes/features/auto-upload/class-seoprostack-auto-upload.php';
|
||||
$auto_upload = new SEOProStack_Auto_Upload();
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the loader to execute all of the hooks with WordPress.
|
||||
*/
|
||||
public function run() {
|
||||
$this->loader->run();
|
||||
}
|
||||
}
|
136
includes/features/auto-upload/class-seoprostack-auto-upload.php
Normal file
136
includes/features/auto-upload/class-seoprostack-auto-upload.php
Normal file
@ -0,0 +1,136 @@
|
||||
<?php
|
||||
/**
|
||||
* Auto Upload Images functionality
|
||||
*
|
||||
* @package SEO_Pro_Stack
|
||||
*/
|
||||
|
||||
class SEO_Pro_Stack_Auto_Upload {
|
||||
/**
|
||||
* Initialize the class
|
||||
*/
|
||||
public function __construct() {
|
||||
add_filter('content_save_pre', array($this, 'process_content'), 10, 1);
|
||||
// New action name
|
||||
add_action('wp_seoprostack_image_upload_error', array($this, 'log_error'), 10, 2);
|
||||
// Backward compatibility
|
||||
add_action('wp_allstars_image_upload_error', array($this, 'log_error'), 10, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process content for external images
|
||||
*
|
||||
* @param string $content The post content
|
||||
* @return string Modified content with local image URLs
|
||||
*/
|
||||
public function process_content($content) {
|
||||
// Check if auto upload is enabled - support both option names for backward compatibility
|
||||
$seoprostack_options = get_option('wp_seoprostack_workflow_options', null);
|
||||
$allstars_options = get_option('wp_allstars_workflow_options', array('auto_upload_images' => false));
|
||||
|
||||
// Use new options if they exist, otherwise fall back to old options
|
||||
$options = $seoprostack_options !== null ? $seoprostack_options : $allstars_options;
|
||||
|
||||
if (!$options['auto_upload_images']) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
// Regular expression to find image URLs
|
||||
$pattern = '/<img[^>]+src=[\'"]([^\'"]+)[\'"][^>]*>/i';
|
||||
|
||||
return preg_replace_callback($pattern, array($this, 'process_image_url'), $content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process individual image URL
|
||||
*
|
||||
* @param array $matches Regex matches
|
||||
* @return string Updated img tag
|
||||
*/
|
||||
private function process_image_url($matches) {
|
||||
if (empty($matches[1])) {
|
||||
return $matches[0];
|
||||
}
|
||||
|
||||
$url = $matches[1];
|
||||
|
||||
// Skip if already a local URL
|
||||
if ($this->is_local_url($url)) {
|
||||
return $matches[0];
|
||||
}
|
||||
|
||||
try {
|
||||
$local_url = $this->upload_image($url);
|
||||
if ($local_url) {
|
||||
return str_replace($url, $local_url, $matches[0]);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
// Fire both actions for backward compatibility
|
||||
do_action('wp_seoprostack_image_upload_error', $url, $e->getMessage());
|
||||
do_action('wp_allstars_image_upload_error', $url, $e->getMessage());
|
||||
}
|
||||
|
||||
return $matches[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if URL is local
|
||||
*
|
||||
* @param string $url URL to check
|
||||
* @return boolean
|
||||
*/
|
||||
private function is_local_url($url) {
|
||||
$site_url = parse_url(get_site_url(), PHP_URL_HOST);
|
||||
$image_host = parse_url($url, PHP_URL_HOST);
|
||||
return $site_url === $image_host;
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload external image to media library
|
||||
*
|
||||
* @param string $url External image URL
|
||||
* @return string|false Local URL on success, false on failure
|
||||
*/
|
||||
private function upload_image($url) {
|
||||
// Get file info
|
||||
$file_array = array();
|
||||
$file_array['name'] = basename($url);
|
||||
|
||||
// Download file to temp location
|
||||
$file_array['tmp_name'] = download_url($url);
|
||||
|
||||
if (is_wp_error($file_array['tmp_name'])) {
|
||||
throw new Exception('Failed to download image: ' . $file_array['tmp_name']->get_error_message());
|
||||
}
|
||||
|
||||
// Check file type
|
||||
$wp_filetype = wp_check_filetype_and_ext($file_array['tmp_name'], $file_array['name']);
|
||||
if (!$wp_filetype['type']) {
|
||||
unlink($file_array['tmp_name']);
|
||||
throw new Exception('Invalid file type');
|
||||
}
|
||||
|
||||
// Upload the file
|
||||
$attachment_id = media_handle_sideload($file_array, 0);
|
||||
|
||||
if (is_wp_error($attachment_id)) {
|
||||
throw new Exception('Failed to upload image: ' . $attachment_id->get_error_message());
|
||||
}
|
||||
|
||||
return wp_get_attachment_url($attachment_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log errors to WordPress debug log
|
||||
*
|
||||
* @param string $url URL that failed
|
||||
* @param string $error Error message
|
||||
*/
|
||||
public function log_error($url, $error) {
|
||||
error_log(sprintf(
|
||||
'[SEO Pro Stack] Auto Upload Images Error - URL: %s, Error: %s',
|
||||
$url,
|
||||
$error
|
||||
));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user