| 1 |
<?php |
| 2 |
use DWL\Wtm\Classes as ControllerClass; |
| 3 |
use DWL\Wtm\Elementor as ElementorClass; |
| 4 |
|
| 5 |
/** |
| 6 |
* This is main class for the plugin |
| 7 |
*/ |
| 8 |
final class Wp_Team_Manager { |
| 9 |
|
| 10 |
use DWL\Wtm\Traits\Singleton; |
| 11 |
|
| 12 |
/** |
| 13 |
* Plugin version |
| 14 |
* |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
const version = '2.6.6'; |
| 18 |
|
| 19 |
/** |
| 20 |
* Class init. |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
protected function init() { |
| 25 |
|
| 26 |
// Initialize global search performance filters |
| 27 |
\DWL\Wtm\Classes\Helper::init_search_filters(); |
| 28 |
|
| 29 |
// Hooks. |
| 30 |
\add_action( 'init', [ $this, 'initial' ] ); |
| 31 |
\add_action( 'plugins_loaded', [ $this, 'plugins_loaded' ] ); |
| 32 |
\add_action( 'admin_init', [ $this, 'handle_css_generator_and_remove' ] ); |
| 33 |
\add_action( 'wp_enqueue_scripts', [ $this, 'wp_team_assets' ] ); |
| 34 |
\add_action( 'admin_enqueue_scripts', [ $this, 'wp_team_admin_assets' ] ); |
| 35 |
|
| 36 |
// Migration hook - only check on admin pages |
| 37 |
if ( is_admin() ) { |
| 38 |
\add_action( 'admin_init', [ $this, 'check_and_schedule_migration' ] ); |
| 39 |
} |
| 40 |
|
| 41 |
// Background migration hook |
| 42 |
\add_action( 'wtm_run_social_fields_migration', [ $this, 'run_social_fields_migration' ] ); |
| 43 |
|
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
/** |
| 49 |
* Load Textdomain |
| 50 |
* |
| 51 |
* Load plugin localization files. |
| 52 |
* |
| 53 |
* @access public |
| 54 |
*/ |
| 55 |
public function load_plugin_text_domain() { |
| 56 |
load_plugin_textdomain('wp-team-manager', false, dirname(plugin_basename(__FILE__)) . '/languages/'); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Init Hooks. |
| 61 |
* |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
public function initial() { |
| 65 |
|
| 66 |
ControllerClass\Helper::instances( $this->controllers() ); |
| 67 |
$this->load_plugin_text_domain(); |
| 68 |
|
| 69 |
\do_action( 'wtm_loaded' ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Controllers. |
| 74 |
* |
| 75 |
* @return array |
| 76 |
*/ |
| 77 |
public function controllers() { |
| 78 |
|
| 79 |
$controllers = [ |
| 80 |
ControllerClass\PostType::class, |
| 81 |
ControllerClass\TeamMetabox::class, |
| 82 |
ControllerClass\ShortcodeGenerator::class, |
| 83 |
ControllerClass\Shortcodes::class, |
| 84 |
ControllerClass\PublicAssets::class, |
| 85 |
ControllerClass\LiveSearch::class, |
| 86 |
ControllerClass\SearchResultCount::class, |
| 87 |
ControllerClass\SearchWidget::class, |
| 88 |
DWL\Wtm\AI\Admin\AdminUI::class, |
| 89 |
]; |
| 90 |
|
| 91 |
if (tmwstm_fs()->is_paying_or_trial() && class_exists(DWL\Wtm\Classes\LoadMore::class)) { |
| 92 |
$controllers[] = ControllerClass\LoadMore::class; |
| 93 |
} |
| 94 |
|
| 95 |
if ( is_admin() ) { |
| 96 |
$controllers[] = ControllerClass\Admin::class; |
| 97 |
$controllers[] = ControllerClass\AdminAssets::class; |
| 98 |
$controllers[] = ControllerClass\AdminSettings::class; |
| 99 |
$controllers[] = ControllerClass\UnifiedTools::class; |
| 100 |
$controllers[] = ControllerClass\ImportExportTools::class; |
| 101 |
$controllers[] = ControllerClass\MigrationTools::class; |
| 102 |
$controllers[] = ControllerClass\Dashboard::class; |
| 103 |
$controllers[] = ControllerClass\Onboarding::class; |
| 104 |
$controllers[] = ControllerClass\SearchSettings::class; |
| 105 |
$controllers[] = ControllerClass\FreemiusConfig::class; |
| 106 |
$controllers[] = ControllerClass\ChatWidget::class; |
| 107 |
} |
| 108 |
|
| 109 |
if ( did_action( 'elementor/loaded' ) ) { |
| 110 |
$controllers[] = ElementorClass\ElementorWidgets::class; |
| 111 |
} |
| 112 |
|
| 113 |
return $controllers; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Initialize the plugin |
| 118 |
* |
| 119 |
* Validates that Elementor is already loaded. |
| 120 |
* Checks for basic plugin requirements, if one check fail don't continue, |
| 121 |
* if all check have passed include the plugin class. |
| 122 |
* |
| 123 |
* Fired by `plugins_loaded` action hook. |
| 124 |
* |
| 125 |
* @since 1.2.0 |
| 126 |
* @access public |
| 127 |
*/ |
| 128 |
public function plugins_loaded() { |
| 129 |
// Initialize AI Manager (stored to prevent memory leaks) |
| 130 |
$ai_manager = new \DWL\Wtm\AI\AI_Manager(); |
| 131 |
require_once TM_PATH . '/lib/cmb2/init.php'; |
| 132 |
require_once TM_PATH . '/lib/cmb2-radio-image/cmb2-radio-image.php'; |
| 133 |
require_once TM_PATH . '/lib/cmb2-tabs/cmb2-tabs.php'; |
| 134 |
require_once TM_PATH . '/includes/functions.php'; |
| 135 |
require_once TM_PATH . '/includes/Classes/GutenbergBlock.php'; |
| 136 |
require_once TM_PATH . '/includes/Classes/BlockPatterns.php'; |
| 137 |
require_once TM_PATH . '/includes/Classes/FSESupport.php'; |
| 138 |
|
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Check if migration is needed and schedule it |
| 144 |
* Runs only once per version update |
| 145 |
* |
| 146 |
* @since 2.4.4 |
| 147 |
*/ |
| 148 |
public function check_and_schedule_migration() { |
| 149 |
$current_version = get_option( 'wp_team_manager_version' ); |
| 150 |
$migration_completed = get_option( 'team_migration_completed' ); |
| 151 |
|
| 152 |
// Only run migration if: |
| 153 |
// 1. User is upgrading from version < 2.1.14 |
| 154 |
// 2. Migration hasn't been completed yet |
| 155 |
if ( ! $migration_completed && version_compare( $current_version, '2.1.14', '<' ) ) { |
| 156 |
// Check if migration is already scheduled |
| 157 |
if ( ! wp_next_scheduled( 'wtm_run_social_fields_migration' ) ) { |
| 158 |
// Schedule migration to run in 30 seconds (gives time for plugin to fully load) |
| 159 |
wp_schedule_single_event( time() + 30, 'wtm_run_social_fields_migration' ); |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Run social fields migration in background |
| 166 |
* Processes team members in batches to avoid timeouts |
| 167 |
* |
| 168 |
* @since 2.4.4 |
| 169 |
*/ |
| 170 |
public function run_social_fields_migration() { |
| 171 |
// Double-check migration hasn't already completed |
| 172 |
if ( get_option( 'team_migration_completed' ) ) { |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
// Get batch offset |
| 177 |
$batch_offset = get_option( 'team_migration_batch_offset', 0 ); |
| 178 |
$batch_size = 50; // Process 50 members at a time |
| 179 |
|
| 180 |
$args = array( |
| 181 |
'post_type' => 'team_manager', |
| 182 |
'posts_per_page' => $batch_size, |
| 183 |
'offset' => $batch_offset, |
| 184 |
'fields' => 'ids', |
| 185 |
'post_status' => 'any', // Include all statuses |
| 186 |
); |
| 187 |
|
| 188 |
$team_member_ids = get_posts( $args ); |
| 189 |
|
| 190 |
if ( ! empty( $team_member_ids ) ) { |
| 191 |
// Process this batch |
| 192 |
foreach ( $team_member_ids as $team_member_id ) { |
| 193 |
ControllerClass\Helper::team_social_icon_migration( $team_member_id ); |
| 194 |
} |
| 195 |
|
| 196 |
// Update offset for next batch |
| 197 |
update_option( 'team_migration_batch_offset', $batch_offset + $batch_size ); |
| 198 |
|
| 199 |
// Schedule next batch |
| 200 |
wp_schedule_single_event( time() + 10, 'wtm_run_social_fields_migration' ); |
| 201 |
} else { |
| 202 |
// Migration complete |
| 203 |
update_option( 'team_migration_completed', true ); |
| 204 |
delete_option( 'team_migration_batch_offset' ); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Load public assets |
| 210 |
*/ |
| 211 |
|
| 212 |
public function wp_team_assets(){ |
| 213 |
$upload_dir = wp_upload_dir(); |
| 214 |
$css_file = $upload_dir['basedir'] . '/wp-team-manager/team.css'; |
| 215 |
if ( file_exists( $css_file ) ) { |
| 216 |
$version = filemtime( $css_file ); |
| 217 |
wp_enqueue_style( 'team-generated', set_url_scheme( $upload_dir['baseurl'] ) . '/wp-team-manager/team.css', null, $version ); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
/** |
| 223 |
* Load admin assets |
| 224 |
*/ |
| 225 |
|
| 226 |
public function wp_team_admin_assets(){ |
| 227 |
wp_enqueue_script( |
| 228 |
'cmb2-conditional-logic', |
| 229 |
TM_ADMIN_ASSETS.'/js/cmb2-conditional-logic.js', |
| 230 |
array('jquery'), |
| 231 |
'1.1.1', |
| 232 |
true |
| 233 |
); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Hooks into save_post and before_delete_post to manage the generation of custom CSS. |
| 238 |
* |
| 239 |
* The `add_generated_css_after_save_post` method is hooked into the `save_post` action to generate custom CSS after a post of type `dwl_team_generator` is saved. |
| 240 |
* |
| 241 |
* The `remove_generated_css_after_delete_post` method is hooked into the `before_delete_post` action to remove the custom CSS after a post of type `dwl_team_generator` is deleted. |
| 242 |
*/ |
| 243 |
public function handle_css_generator_and_remove(){ |
| 244 |
add_action( 'save_post', [ $this, 'add_generated_css_after_save_post' ], 10, 3 ); |
| 245 |
add_action( 'before_delete_post', [ $this, 'remove_generated_css_after_delete_post' ], 10, 1 ); |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Save generated CSS after saving a post of type `dwl_team_generator` |
| 250 |
* |
| 251 |
* @param int $post_id The ID of the post being saved. |
| 252 |
* @param object $post The post object being saved. |
| 253 |
* @param bool $update Whether the post is being updated or not. |
| 254 |
* |
| 255 |
* @return void |
| 256 |
* |
| 257 |
* @since 1.0.0 |
| 258 |
*/ |
| 259 |
public function add_generated_css_after_save_post( $post_id, $post, $update ) { |
| 260 |
// Bail if autosave |
| 261 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 262 |
return; |
| 263 |
} |
| 264 |
|
| 265 |
// Bail if revision |
| 266 |
if ( wp_is_post_revision( $post_id ) ) { |
| 267 |
return; |
| 268 |
} |
| 269 |
|
| 270 |
// Bail if not the correct post type |
| 271 |
if ( 'dwl_team_generator' !== $post->post_type ) { |
| 272 |
return; |
| 273 |
} |
| 274 |
|
| 275 |
// Bail if user doesn't have permission to edit |
| 276 |
if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
// Generate CSS |
| 281 |
ControllerClass\Helper::generatorShortcodeCss( $post_id ); |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Removes generated CSS after a post of type `dwl_team_generator` is deleted. |
| 286 |
* |
| 287 |
* This method is hooked into the `before_delete_post` action. It checks the post type, |
| 288 |
* and if it matches `dwl_team_generator`, it calls the helper function to remove the associated |
| 289 |
* custom CSS. |
| 290 |
* |
| 291 |
* @param int $post_id The ID of the post being deleted. |
| 292 |
* |
| 293 |
* @return void |
| 294 |
* |
| 295 |
* @since 1.0.0 |
| 296 |
*/ |
| 297 |
public function remove_generated_css_after_delete_post( $post_id ) { |
| 298 |
// Get the post object (before_delete_post only passes post_id) |
| 299 |
$post = get_post( $post_id ); |
| 300 |
|
| 301 |
// Check if post exists and is the correct type |
| 302 |
if ( ! $post || 'dwl_team_generator' !== $post->post_type ) { |
| 303 |
return; |
| 304 |
} |
| 305 |
|
| 306 |
ControllerClass\Helper::removeGeneratorShortcodeCss( $post_id ); |
| 307 |
} |
| 308 |
|
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
Wp_Team_Manager::get_instance(); |