370 lines
10 KiB
PHP
370 lines
10 KiB
PHP
<?php
|
|
/**
|
|
* Plugin Name: Instagram Gallery Sync Pro
|
|
* Plugin URI: https://example.com/instagram-gallery-sync-pro
|
|
* Description: Synchronize and display Instagram photos in beautiful galleries without the official API.
|
|
* Version: 1.0.0
|
|
* Author: Your Name
|
|
* Author URI: https://example.com
|
|
* License: GPL-2.0+
|
|
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
|
|
* Text Domain: instagram-gallery-sync-pro
|
|
* Domain Path: /languages
|
|
* Requires at least: 5.8
|
|
* Requires PHP: 7.4
|
|
*/
|
|
|
|
// Prevent direct access
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Plugin Constants
|
|
*/
|
|
define( 'IGSP_VERSION', '1.0.0' );
|
|
define( 'IGSP_PLUGIN_FILE', __FILE__ );
|
|
define( 'IGSP_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
|
|
define( 'IGSP_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
|
|
define( 'IGSP_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
|
|
define( 'IGSP_UPLOAD_DIR', 'instagram-gallery' );
|
|
|
|
/**
|
|
* Database Table Names
|
|
*/
|
|
global $wpdb;
|
|
define( 'IGSP_TABLE_POSTS', $wpdb->prefix . 'instagram_gallery_posts' );
|
|
define( 'IGSP_TABLE_LOG', $wpdb->prefix . 'instagram_gallery_log' );
|
|
|
|
/**
|
|
* Autoloader for plugin classes
|
|
*/
|
|
spl_autoload_register( function( $class ) {
|
|
// Check if the class uses our namespace prefix
|
|
$prefix = 'IGSP_';
|
|
|
|
if ( strpos( $class, $prefix ) !== 0 ) {
|
|
return;
|
|
}
|
|
|
|
// Convert class name to file name
|
|
$class_name = str_replace( $prefix, '', $class );
|
|
$class_name = strtolower( str_replace( '_', '-', $class_name ) );
|
|
$file = IGSP_PLUGIN_DIR . 'includes/class-' . $class_name . '.php';
|
|
|
|
if ( file_exists( $file ) ) {
|
|
require_once $file;
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Main Plugin Class
|
|
*/
|
|
final class Instagram_Gallery_Sync_Pro {
|
|
|
|
/**
|
|
* Plugin instance
|
|
*
|
|
* @var Instagram_Gallery_Sync_Pro
|
|
*/
|
|
private static $instance = null;
|
|
|
|
/**
|
|
* Plugin components
|
|
*/
|
|
public $database;
|
|
public $logger;
|
|
public $scraper;
|
|
public $image_handler;
|
|
public $cron;
|
|
public $admin;
|
|
public $shortcode;
|
|
public $gutenberg;
|
|
|
|
/**
|
|
* Get plugin instance
|
|
*
|
|
* @return Instagram_Gallery_Sync_Pro
|
|
*/
|
|
public static function get_instance() {
|
|
if ( null === self::$instance ) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
/**
|
|
* Constructor
|
|
*/
|
|
private function __construct() {
|
|
$this->init_hooks();
|
|
}
|
|
|
|
/**
|
|
* Initialize hooks
|
|
*/
|
|
private function init_hooks() {
|
|
// Activation and deactivation hooks
|
|
register_activation_hook( IGSP_PLUGIN_FILE, array( $this, 'activate' ) );
|
|
register_deactivation_hook( IGSP_PLUGIN_FILE, array( $this, 'deactivate' ) );
|
|
|
|
// Initialize plugin after WordPress is loaded
|
|
add_action( 'plugins_loaded', array( $this, 'init_plugin' ) );
|
|
|
|
// Load text domain
|
|
add_action( 'init', array( $this, 'load_textdomain' ) );
|
|
}
|
|
|
|
/**
|
|
* Plugin activation
|
|
*/
|
|
public function activate() {
|
|
// Ensure components are loaded
|
|
$this->load_components();
|
|
|
|
// Create database tables
|
|
$this->database->create_tables();
|
|
|
|
// Create upload directory
|
|
$this->create_upload_directory();
|
|
|
|
// Set default options
|
|
$this->set_default_options();
|
|
|
|
// Schedule cron if enabled
|
|
$this->cron->schedule_sync();
|
|
|
|
// Log activation
|
|
$this->logger->log( 'info', __( 'Plugin activated successfully.', 'instagram-gallery-sync-pro' ) );
|
|
|
|
// Flush rewrite rules
|
|
flush_rewrite_rules();
|
|
}
|
|
|
|
/**
|
|
* Plugin deactivation
|
|
*/
|
|
public function deactivate() {
|
|
// Clear scheduled cron
|
|
$this->cron->unschedule_sync();
|
|
|
|
// Clear transients
|
|
$this->clear_transients();
|
|
|
|
// Log deactivation
|
|
$this->logger->log( 'info', __( 'Plugin deactivated.', 'instagram-gallery-sync-pro' ) );
|
|
|
|
// Flush rewrite rules
|
|
flush_rewrite_rules();
|
|
}
|
|
|
|
/**
|
|
* Initialize plugin components
|
|
*/
|
|
public function init_plugin() {
|
|
$this->load_components();
|
|
$this->init_components();
|
|
}
|
|
|
|
/**
|
|
* Load plugin components
|
|
*/
|
|
private function load_components() {
|
|
// Core components
|
|
$this->database = new IGSP_Database();
|
|
$this->logger = new IGSP_Logger();
|
|
$this->image_handler = new IGSP_Image_Handler();
|
|
$this->scraper = new IGSP_Scraper();
|
|
$this->cron = new IGSP_Cron();
|
|
|
|
// Frontend components
|
|
$this->shortcode = new IGSP_Shortcode();
|
|
$this->gutenberg = new IGSP_Gutenberg_Block();
|
|
|
|
// Admin components (only in admin)
|
|
if ( is_admin() ) {
|
|
$this->admin = new IGSP_Admin();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Initialize components
|
|
*/
|
|
private function init_components() {
|
|
$this->shortcode->init();
|
|
$this->gutenberg->init();
|
|
$this->cron->init();
|
|
|
|
if ( is_admin() && $this->admin ) {
|
|
$this->admin->init();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Load plugin text domain
|
|
*/
|
|
public function load_textdomain() {
|
|
load_plugin_textdomain(
|
|
'instagram-gallery-sync-pro',
|
|
false,
|
|
dirname( IGSP_PLUGIN_BASENAME ) . '/languages'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Create upload directory
|
|
*/
|
|
private function create_upload_directory() {
|
|
$upload_dir = wp_upload_dir();
|
|
$instagram_dir = $upload_dir['basedir'] . '/' . IGSP_UPLOAD_DIR;
|
|
|
|
if ( ! file_exists( $instagram_dir ) ) {
|
|
wp_mkdir_p( $instagram_dir );
|
|
|
|
// Create index.php for security
|
|
file_put_contents( $instagram_dir . '/index.php', '<?php // Silence is golden' );
|
|
|
|
// Create .htaccess for additional security
|
|
$htaccess = "Options -Indexes\n";
|
|
$htaccess .= "<Files *.php>\n";
|
|
$htaccess .= "deny from all\n";
|
|
$htaccess .= "</Files>\n";
|
|
file_put_contents( $instagram_dir . '/.htaccess', $htaccess );
|
|
}
|
|
|
|
// Create thumbnails subfolder
|
|
$thumb_dir = $instagram_dir . '/thumbnails';
|
|
if ( ! file_exists( $thumb_dir ) ) {
|
|
wp_mkdir_p( $thumb_dir );
|
|
file_put_contents( $thumb_dir . '/index.php', '<?php // Silence is golden' );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set default options
|
|
*/
|
|
private function set_default_options() {
|
|
$defaults = array(
|
|
// Instagram settings
|
|
'igsp_username' => '',
|
|
'igsp_max_images' => 12,
|
|
'igsp_save_locally' => 'yes',
|
|
'igsp_image_quality' => 'high',
|
|
'igsp_sync_interval' => 'daily',
|
|
'igsp_last_sync' => '',
|
|
|
|
// Layout settings
|
|
'igsp_layout_type' => 'grid',
|
|
'igsp_columns_desktop' => 3,
|
|
'igsp_columns_tablet' => 2,
|
|
'igsp_columns_mobile' => 1,
|
|
'igsp_spacing' => 10,
|
|
'igsp_padding' => 0,
|
|
'igsp_aspect_ratio' => 'square',
|
|
'igsp_object_fit' => 'cover',
|
|
'igsp_border_radius' => 0,
|
|
'igsp_hover_effect' => 'zoom',
|
|
|
|
// Display settings
|
|
'igsp_display_limit' => 12,
|
|
'igsp_order' => 'newest',
|
|
'igsp_show_caption' => 'no',
|
|
'igsp_caption_position' => 'overlay',
|
|
'igsp_show_instagram_btn' => 'yes',
|
|
'igsp_link_behavior' => 'new_tab',
|
|
'igsp_lightbox' => 'yes',
|
|
'igsp_lazy_loading' => 'yes',
|
|
'igsp_loader_type' => 'spinner',
|
|
|
|
// Styling
|
|
'igsp_primary_color' => '#e1306c',
|
|
'igsp_hover_color' => '#c13584',
|
|
'igsp_text_color' => '#ffffff',
|
|
'igsp_caption_font_size' => 14,
|
|
'igsp_custom_css' => '',
|
|
'igsp_css_prefix' => 'igsp',
|
|
|
|
// Advanced
|
|
'igsp_debug_mode' => 'no',
|
|
'igsp_cache_duration' => 3600,
|
|
'igsp_request_timeout' => 30,
|
|
'igsp_user_agent' => '',
|
|
'igsp_proxy_host' => '',
|
|
'igsp_proxy_port' => '',
|
|
'igsp_auto_delete_days' => 0,
|
|
);
|
|
|
|
foreach ( $defaults as $key => $value ) {
|
|
if ( get_option( $key ) === false ) {
|
|
add_option( $key, $value );
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Clear all transients
|
|
*/
|
|
public function clear_transients() {
|
|
global $wpdb;
|
|
|
|
$wpdb->query(
|
|
"DELETE FROM {$wpdb->options}
|
|
WHERE option_name LIKE '_transient_igsp_%'
|
|
OR option_name LIKE '_transient_timeout_igsp_%'"
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get upload directory path
|
|
*
|
|
* @return string
|
|
*/
|
|
public function get_upload_path() {
|
|
$upload_dir = wp_upload_dir();
|
|
return $upload_dir['basedir'] . '/' . IGSP_UPLOAD_DIR;
|
|
}
|
|
|
|
/**
|
|
* Get upload directory URL
|
|
*
|
|
* @return string
|
|
*/
|
|
public function get_upload_url() {
|
|
$upload_dir = wp_upload_dir();
|
|
return $upload_dir['baseurl'] . '/' . IGSP_UPLOAD_DIR;
|
|
}
|
|
|
|
/**
|
|
* Get plugin settings
|
|
*
|
|
* @param string $key Option key
|
|
* @param mixed $default Default value
|
|
* @return mixed
|
|
*/
|
|
public function get_setting( $key, $default = '' ) {
|
|
return get_option( 'igsp_' . $key, $default );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns the main plugin instance
|
|
*
|
|
* @return Instagram_Gallery_Sync_Pro
|
|
*/
|
|
function igsp() {
|
|
return Instagram_Gallery_Sync_Pro::get_instance();
|
|
}
|
|
|
|
/**
|
|
* Template function for displaying the gallery
|
|
*
|
|
* @param array $args Gallery arguments
|
|
* @return void
|
|
*/
|
|
function display_instagram_gallery( $args = array() ) {
|
|
echo igsp()->shortcode->render( $args );
|
|
}
|
|
|
|
// Initialize the plugin
|
|
igsp();
|