| 1 |
<?php |
| 2 |
|
| 3 |
namespace F4\TREE\Tree; |
| 4 |
|
| 5 |
use F4\TREE\Core\Helpers as Core; |
| 6 |
use F4\TREE\Tree\Helpers as Tree; |
| 7 |
|
| 8 |
/** |
| 9 |
* Tree |
| 10 |
* |
| 11 |
* The custom Tree funcionality for this plugin |
| 12 |
* |
| 13 |
* @since 1.0.0 |
| 14 |
* @package F4\TREE\Tree |
| 15 |
*/ |
| 16 |
class Hooks { |
| 17 |
/** |
| 18 |
* Initialize the hooks |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
* @access public |
| 22 |
* @static |
| 23 |
*/ |
| 24 |
public static function init() { |
| 25 |
add_action('F4/TREE/Core/set_constants', __NAMESPACE__ . '\\Hooks::set_default_constants', 99); |
| 26 |
add_action('F4/TREE/Core/loaded', __NAMESPACE__ . '\\Hooks::loaded'); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Sets the module default constants |
| 31 |
* |
| 32 |
* @since 1.0.0 |
| 33 |
* @access public |
| 34 |
* @static |
| 35 |
*/ |
| 36 |
public static function set_default_constants() { |
| 37 |
if(!defined('F4_TREE_CSS_URL')) { |
| 38 |
define('F4_TREE_CSS_URL', F4_TREE_URL . 'Tree/assets/css/'); |
| 39 |
} |
| 40 |
|
| 41 |
if(!defined('F4_TREE_JS_URL')) { |
| 42 |
define('F4_TREE_JS_URL', F4_TREE_URL . 'Tree/assets/js/'); |
| 43 |
} |
| 44 |
|
| 45 |
if(!defined('F4_TREE_IGNORE_POST_TYPES')) { |
| 46 |
define('F4_TREE_IGNORE_POST_TYPES', [ |
| 47 |
'attachment', |
| 48 |
'wp_block', |
| 49 |
'wp_navigation', |
| 50 |
'acf-taxonomy', |
| 51 |
'acf-post-type', |
| 52 |
'acf-ui-options-page', |
| 53 |
'acf-field-group', |
| 54 |
'skuld_content', |
| 55 |
'skuld_fieldgroup', |
| 56 |
'skuld_layout', |
| 57 |
'skuld_snippet', |
| 58 |
'shop_order', |
| 59 |
'shop_coupon', |
| 60 |
'wc_reminder_email', |
| 61 |
'br_product_filter', |
| 62 |
'br_filters_group' |
| 63 |
]); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Fires once the module is loaded |
| 69 |
* |
| 70 |
* @since 1.0.0 |
| 71 |
* @access public |
| 72 |
* @static |
| 73 |
*/ |
| 74 |
public static function loaded() { |
| 75 |
add_action('admin_enqueue_scripts', __NAMESPACE__ . '\\Hooks::admin_enqueue_scripts'); |
| 76 |
add_action('in_admin_header', __NAMESPACE__ . '\\Hooks::show_tree_in_backend'); |
| 77 |
|
| 78 |
add_action('wp_ajax_f4_tree_load_tree', __NAMESPACE__ . '\\Hooks::ajax_load_tree'); |
| 79 |
add_action('wp_ajax_f4_tree_move_post', __NAMESPACE__ . '\\Hooks::ajax_move_tree_post'); |
| 80 |
add_action('wp_ajax_f4_tree_refresh', __NAMESPACE__ . '\\Hooks::ajax_refresh_tree'); |
| 81 |
add_filter('page_attributes_dropdown_pages_args', __NAMESPACE__ . '\\Hooks::remove_page_attributes_parent_id'); |
| 82 |
|
| 83 |
add_action('updated_post_meta', __NAMESPACE__ . '\\Hooks::updated_edit_lock', 10, 4); |
| 84 |
add_filter('deleted_post_meta', __NAMESPACE__ . '\\Hooks::updated_edit_lock', 10, 4); |
| 85 |
|
| 86 |
add_action('save_post', __NAMESPACE__ . '\\Hooks::update_post'); |
| 87 |
add_action('delete_post', __NAMESPACE__ . '\\Hooks::update_post'); |
| 88 |
|
| 89 |
add_action('F4/TREE/Core/admin_settings_fields', __NAMESPACE__ . '\\Hooks::add_admin_settings_fields'); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Enqueue admin scripts and styles |
| 94 |
* |
| 95 |
* @since 1.0.0 |
| 96 |
* @access public |
| 97 |
* @static |
| 98 |
*/ |
| 99 |
public static function admin_enqueue_scripts() { |
| 100 |
if(!Tree::is_tree_enabled()) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
// Styles |
| 105 |
wp_enqueue_style( |
| 106 |
'f4-tree', |
| 107 |
F4_TREE_CSS_URL . 'tree.css', |
| 108 |
[], |
| 109 |
F4_TREE_VERSION |
| 110 |
); |
| 111 |
|
| 112 |
wp_enqueue_style( |
| 113 |
'fancytree-custom-theme', |
| 114 |
F4_TREE_CSS_URL . 'fancytree.css', |
| 115 |
[], |
| 116 |
F4_TREE_VERSION |
| 117 |
); |
| 118 |
|
| 119 |
// Scripts |
| 120 |
wp_enqueue_script( |
| 121 |
'jquery-fancytree', |
| 122 |
F4_TREE_JS_URL . 'jquery.fancytree.min.js', |
| 123 |
array('jquery', 'jquery-effects-core', 'jquery-effects-blind'), |
| 124 |
F4_TREE_VERSION, |
| 125 |
true |
| 126 |
); |
| 127 |
|
| 128 |
wp_enqueue_script( |
| 129 |
'jquery-fancytree-dnd', |
| 130 |
F4_TREE_JS_URL . 'jquery.fancytree.dnd.min.js', |
| 131 |
array('jquery-fancytree', 'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-position'), |
| 132 |
F4_TREE_VERSION, |
| 133 |
true |
| 134 |
); |
| 135 |
|
| 136 |
wp_enqueue_script( |
| 137 |
'f4-tree', |
| 138 |
F4_TREE_JS_URL . 'tree.js', |
| 139 |
array('jquery-fancytree', 'jquery-fancytree-dnd'), |
| 140 |
F4_TREE_VERSION, |
| 141 |
true |
| 142 |
); |
| 143 |
|
| 144 |
wp_localize_script( |
| 145 |
'f4-tree', |
| 146 |
'F4_tree_config', |
| 147 |
array( |
| 148 |
'labels' => array( |
| 149 |
'loading' => __('Tree is loading...', 'f4-tree'), |
| 150 |
'error' => __('An error occured', 'f4-tree'), |
| 151 |
) |
| 152 |
) |
| 153 |
); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Show the tree in backend |
| 158 |
* |
| 159 |
* @since 1.0.0 |
| 160 |
* @access public |
| 161 |
* @static |
| 162 |
*/ |
| 163 |
public static function show_tree_in_backend() { |
| 164 |
if(!Tree::is_tree_enabled()) { |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
include F4_TREE_PATH . 'Tree/views/tree.php'; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Ajax load tree |
| 173 |
* |
| 174 |
* @since 1.0.0 |
| 175 |
* @access public |
| 176 |
* @static |
| 177 |
*/ |
| 178 |
public static function ajax_load_tree() { |
| 179 |
$post_id = isset($_REQUEST['post_id']) ? intval($_REQUEST['post_id']) : 0; |
| 180 |
$post_type = isset($_REQUEST['post_type']) ? sanitize_title($_REQUEST['post_type']) : 'page'; |
| 181 |
$lang = isset($_REQUEST['lang']) ? sanitize_title($_REQUEST['lang']) : ''; |
| 182 |
|
| 183 |
Core::maybe_change_current_language($lang); |
| 184 |
|
| 185 |
$treeview = Tree::get_tree($post_id, $post_type); |
| 186 |
$treeview = apply_filters('F4/TREE/Tree/ajax_load_tree', $treeview); |
| 187 |
echo wp_json_encode($treeview); |
| 188 |
|
| 189 |
die(); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Ajax move post |
| 194 |
* |
| 195 |
* @since 1.0.0 |
| 196 |
* @access public |
| 197 |
* @static |
| 198 |
*/ |
| 199 |
public static function ajax_move_tree_post() { |
| 200 |
global $wpdb; |
| 201 |
|
| 202 |
$posts_sorted = json_decode(stripslashes($_REQUEST['posts_sorted'] ?? '[]'), true); |
| 203 |
|
| 204 |
if(!is_array($posts_sorted)) { |
| 205 |
$posts_sorted = []; |
| 206 |
} |
| 207 |
|
| 208 |
$posts_sorted = apply_filters('F4/TREE/Tree/move_tree_post_get_sorted_post_ids', $posts_sorted); |
| 209 |
$lang = isset($_REQUEST['lang']) ? sanitize_title($_REQUEST['lang']) : ''; |
| 210 |
|
| 211 |
Core::maybe_change_current_language($lang); |
| 212 |
|
| 213 |
foreach($posts_sorted as $post_index => $post_item) { |
| 214 |
$wpdb->query( |
| 215 |
$wpdb->prepare( |
| 216 |
"UPDATE $wpdb->posts SET post_parent = %d, menu_order = %d WHERE ID = %d", |
| 217 |
$post_item['parent_post_id'], |
| 218 |
$post_index, |
| 219 |
$post_item['post_id'] |
| 220 |
) |
| 221 |
); |
| 222 |
|
| 223 |
clean_post_cache($post_item['post_id']); |
| 224 |
} |
| 225 |
|
| 226 |
do_action('F4/TREE/Tree/move_tree_post', $posts_sorted); |
| 227 |
|
| 228 |
echo Tree::update_changed_timestamp(); |
| 229 |
|
| 230 |
die(); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Ajax refresh tree |
| 235 |
* |
| 236 |
* @since 1.0.0 |
| 237 |
* @access public |
| 238 |
* @static |
| 239 |
*/ |
| 240 |
public static function ajax_refresh_tree() { |
| 241 |
session_write_close(); |
| 242 |
ignore_user_abort(true); |
| 243 |
|
| 244 |
$last_request_timestamp = isset($_REQUEST['timestamp']) ? intval($_REQUEST['timestamp']) : null; |
| 245 |
$post_id = isset($_REQUEST['post_id']) ? intval($_REQUEST['post_id']) : -1; |
| 246 |
$post_type = isset($_REQUEST['post_type']) ? sanitize_title($_REQUEST['post_type']) : 'page'; |
| 247 |
$lang = isset($_REQUEST['lang']) ? sanitize_title($_REQUEST['lang']) : ''; |
| 248 |
|
| 249 |
Core::maybe_change_current_language($lang); |
| 250 |
|
| 251 |
set_time_limit(299); // 5 min - 1 sec |
| 252 |
|
| 253 |
while(true) { |
| 254 |
echo ' '; |
| 255 |
ob_flush(); |
| 256 |
flush(); |
| 257 |
|
| 258 |
if(connection_status() != CONNECTION_NORMAL) { |
| 259 |
break; |
| 260 |
die(); |
| 261 |
} |
| 262 |
|
| 263 |
clearstatcache(); |
| 264 |
Tree::clear_changed_timestamp_cache(); |
| 265 |
$last_change_timestamp = Tree::get_changed_timestamp(); |
| 266 |
|
| 267 |
if($last_request_timestamp === null || $last_change_timestamp > $last_request_timestamp) { |
| 268 |
wp_cache_init(); |
| 269 |
|
| 270 |
$response = array( |
| 271 |
//'sample-permalink' => get_sample_permalink_html($post_id), // @todo: temporarily disabled because of manual change overwrite |
| 272 |
'sample-permalink' => '', |
| 273 |
'data' => Tree::get_tree($post_id, $post_type), |
| 274 |
'timestamp' => $last_change_timestamp |
| 275 |
); |
| 276 |
|
| 277 |
$responseJSON = wp_json_encode($response); |
| 278 |
|
| 279 |
echo $responseJSON; |
| 280 |
|
| 281 |
break; |
| 282 |
} else { |
| 283 |
sleep(1); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
die(); |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Remove parent id dropdown |
| 292 |
* |
| 293 |
* @since 1.0.0 |
| 294 |
* @access public |
| 295 |
* @static |
| 296 |
*/ |
| 297 |
public static function remove_page_attributes_parent_id($args) { |
| 298 |
if(Tree::is_tree_enabled()) { |
| 299 |
$args['child_of'] = '-10'; |
| 300 |
} |
| 301 |
|
| 302 |
return $args; |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Update last change time if the post edit lock changes |
| 307 |
* |
| 308 |
* @since 1.0.0 |
| 309 |
* @access public |
| 310 |
* @static |
| 311 |
*/ |
| 312 |
public static function updated_edit_lock($meta_id, $post_id, $meta_key, $meta_value) { |
| 313 |
if($meta_key === '_edit_lock' && Tree::is_tree_post_type($post_id)) { |
| 314 |
Tree::update_changed_timestamp(); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Update last change time if post is saved or deleted |
| 320 |
* |
| 321 |
* @since 1.0.0 |
| 322 |
* @access public |
| 323 |
* @static |
| 324 |
*/ |
| 325 |
public static function update_post($post_id) { |
| 326 |
if(Tree::is_tree_post_type($post_id)) { |
| 327 |
Tree::update_changed_timestamp(); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Show tree admin settings |
| 333 |
* |
| 334 |
* @since 1.0.0 |
| 335 |
* @access public |
| 336 |
* @static |
| 337 |
*/ |
| 338 |
public static function add_admin_settings_fields() { |
| 339 |
include 'views/admin-settings-fields.php'; |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
?> |
| 344 |
|