| 1 |
<?php |
| 2 |
|
| 3 |
defined('ABSPATH') || die; |
| 4 |
|
| 5 |
require_once(plugin_dir_path(__FILE__) . '/incl/utilities-main.php'); |
| 6 |
require_once(plugin_dir_path(__FILE__) . '/incl/block-settings-main.php'); |
| 7 |
require_once(plugin_dir_path(__FILE__) . '/incl/block-controls-main.php'); |
| 8 |
require_once(plugin_dir_path(__FILE__) . '/incl/advanced-gutenberg-main.php'); |
| 9 |
new AdvancedGutenbergMain(); |
| 10 |
|
| 11 |
if (! function_exists('advg_language_domain_init')) { |
| 12 |
/** |
| 13 |
* Load language translations |
| 14 |
* |
| 15 |
* @return void |
| 16 |
*/ |
| 17 |
function advg_language_domain_init() |
| 18 |
{ |
| 19 |
$domain = 'advanced-gutenberg'; |
| 20 |
|
| 21 |
load_plugin_textdomain($domain, false, dirname(plugin_basename(__FILE__)) . '/languages'); |
| 22 |
} |
| 23 |
} |
| 24 |
add_action('init', 'advg_language_domain_init', 0); |
| 25 |
|
| 26 |
if (! function_exists('advg_check_legacy_widget_block_init')) { |
| 27 |
/** |
| 28 |
* Check if widget blocks exists in current user role through advgb_blocks_user_roles option, |
| 29 |
* either in inactive_blocks or active_blocks array. |
| 30 |
* https://github.com/publishpress/PublishPress-Blocks/issues/756#issuecomment-932358037 |
| 31 |
* |
| 32 |
* @return void |
| 33 |
* @since 3.1.4.2 - Added support for core/widget-group block |
| 34 |
* |
| 35 |
* This function can be used in future to add new blocks not available on widgets.php |
| 36 |
* |
| 37 |
* @since 2.11.0 |
| 38 |
*/ |
| 39 |
function advg_check_legacy_widget_block_init() |
| 40 |
{ |
| 41 |
if (! current_user_can('edit_theme_options')) { |
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
$widget_blocks = [ |
| 46 |
'core/legacy-widget', |
| 47 |
'core/widget-group' |
| 48 |
]; |
| 49 |
|
| 50 |
global $wp_version; |
| 51 |
global $pagenow; |
| 52 |
if (( $pagenow === 'widgets.php' || $pagenow === 'customize.php' ) && $wp_version >= 5.8) { |
| 53 |
$advgb_blocks_list = get_option('advgb_blocks_list') && ! empty(get_option('advgb_blocks_list')) ? get_option('advgb_blocks_list') : []; |
| 54 |
$advgb_blocks_user_roles = get_option('advgb_blocks_user_roles') && ! empty(get_option('advgb_blocks_user_roles')) ? get_option('advgb_blocks_user_roles') : []; |
| 55 |
$current_user = wp_get_current_user(); |
| 56 |
$current_user_role = $current_user->roles[0]; |
| 57 |
|
| 58 |
if (count($advgb_blocks_list) && count($advgb_blocks_user_roles)) { |
| 59 |
foreach ($widget_blocks as $item) { |
| 60 |
if ( |
| 61 |
is_array($advgb_blocks_user_roles[ $current_user_role ]['active_blocks']) |
| 62 |
&& is_array($advgb_blocks_user_roles[ $current_user_role ]['inactive_blocks']) |
| 63 |
&& ! in_array($item, $advgb_blocks_user_roles[ $current_user_role ]['active_blocks']) |
| 64 |
&& ! in_array($item, $advgb_blocks_user_roles[ $current_user_role ]['inactive_blocks']) |
| 65 |
&& ! empty($current_user_role) |
| 66 |
) { |
| 67 |
array_push( |
| 68 |
$advgb_blocks_user_roles[ $current_user_role ]['active_blocks'], |
| 69 |
$item |
| 70 |
); |
| 71 |
update_option('advgb_blocks_user_roles', $advgb_blocks_user_roles, false); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
add_action('admin_init', 'advg_check_legacy_widget_block_init'); |
| 79 |
|
| 80 |
// @todo - Remove this in 4.0 |
| 81 |
if (! function_exists('advgb_some_specific_updates')) { |
| 82 |
function advgb_some_specific_updates() |
| 83 |
{ |
| 84 |
// Run the updates from here |
| 85 |
$advgb_current_version = get_option('advgb_version', '0.0.0'); |
| 86 |
global $wpdb; |
| 87 |
|
| 88 |
// Migrate to Block Access by User Roles |
| 89 |
if (version_compare($advgb_current_version, '2.10.2', 'lt') && ! get_option('advgb_blocks_user_roles')) { |
| 90 |
// Migrate Block Access Profiles to Block Access by Roles |
| 91 |
global $wpdb; |
| 92 |
$profiles = $wpdb->get_results( |
| 93 |
'SELECT * FROM ' . $wpdb->prefix . 'posts |
| 94 |
WHERE post_type="advgb_profiles" AND post_status="publish" ORDER BY post_date_gmt DESC' |
| 95 |
); |
| 96 |
|
| 97 |
if (! empty($profiles)) { |
| 98 |
// Let's extract the user roles associated to Block Access profiles (we can't get all the user roles with regular WP way) |
| 99 |
$user_role_accesses = array(); |
| 100 |
foreach ($profiles as $profile) { |
| 101 |
$postID = $profile->ID; |
| 102 |
$user_role_accesses[] = get_post_meta($postID, 'roles_access', true); |
| 103 |
} |
| 104 |
|
| 105 |
$user_role_accesses = call_user_func_array('array_merge', $user_role_accesses); |
| 106 |
$user_role_accesses = array_unique($user_role_accesses); |
| 107 |
|
| 108 |
// Find the most recent profile of each user role |
| 109 |
$blocks_by_role_access = array(); |
| 110 |
foreach ($user_role_accesses as $user_role_access) { |
| 111 |
$profiles = $wpdb->get_results( |
| 112 |
'SELECT * FROM ' . $wpdb->prefix . 'posts |
| 113 |
WHERE post_type="advgb_profiles" AND post_status="publish" ORDER BY post_date_gmt DESC' |
| 114 |
); |
| 115 |
|
| 116 |
if (! empty($profiles)) { |
| 117 |
$centinel[ $user_role_access ] = false; // A boolean to get the first profile (newest) and skip the rest |
| 118 |
foreach ($profiles as $profile) { |
| 119 |
if ($centinel[ $user_role_access ] === false) { |
| 120 |
$postID = $profile->ID; |
| 121 |
$roles_access = get_post_meta($postID, 'roles_access', true); |
| 122 |
$blocks = get_post_meta($postID, 'blocks', true); |
| 123 |
|
| 124 |
if (in_array($user_role_access, $roles_access)) { |
| 125 |
$blocks_by_role_access[ $user_role_access ] = $blocks; |
| 126 |
$centinel[ $user_role_access ] = true; |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
// Migrate Block Access by Profile to Block Access by Role |
| 134 |
if ($blocks_by_role_access) { |
| 135 |
update_option('advgb_blocks_user_roles', $blocks_by_role_access, false); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
// Existing sites: keep legacy blocks and gallery lightbox enabled unless explicitly saved otherwise. |
| 141 |
if (get_option('advgb_legacy_settings_migrated', false) === false && get_option('advgb_settings') !== false) { |
| 142 |
AdvancedGutenbergMain::upgradeLegacySettings(); |
| 143 |
update_option('advgb_legacy_settings_migrated', 1, false); |
| 144 |
} |
| 145 |
|
| 146 |
if ( |
| 147 |
$advgb_current_version !== '0.0.0' |
| 148 |
&& version_compare($advgb_current_version, '3.7.6', 'lt') |
| 149 |
&& get_option('advgb_settings') !== false |
| 150 |
) { |
| 151 |
AdvancedGutenbergMain::fillMissingLegacyBlocksState(true); |
| 152 |
} |
| 153 |
|
| 154 |
// Set version if needed |
| 155 |
if ($advgb_current_version !== ADVANCED_GUTENBERG_VERSION) { |
| 156 |
update_option('advgb_version', ADVANCED_GUTENBERG_VERSION); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
add_action('init', 'advgb_some_specific_updates'); |
| 161 |
} |
| 162 |
|