| 1 |
<?php |
| 2 |
namespace BetterLinks\Integration; |
| 3 |
if ( ! defined( 'ABSPATH' ) ) { exit; } |
| 4 |
|
| 5 |
use BetterLinks\Admin\Cache; |
| 6 |
use BetterLinks\Helper; |
| 7 |
use BetterLinks\Traits\Links; |
| 8 |
|
| 9 |
class FluentBoards { |
| 10 |
use Links; |
| 11 |
|
| 12 |
/** |
| 13 |
* Task IDs already handled this request, so registering both archive hook |
| 14 |
* names below can never delete twice for one archive. |
| 15 |
* |
| 16 |
* @var array<int,bool> |
| 17 |
*/ |
| 18 |
private $handled_archives = array(); |
| 19 |
|
| 20 |
public static function init() { |
| 21 |
$self = new self(); |
| 22 |
add_action( 'fluent_boards/task_deleted', array( $self, 'fbs_task_deleted' ), 10, 1 ); |
| 23 |
// Fluent Boards renamed this hook. 2.0+ fires `fluent_boards/task_archived` |
| 24 |
// (TaskService::archive() and StageService), and `board_task_archived` |
| 25 |
// survives only as an activity-log string — nothing does_action()s it, so |
| 26 |
// listening to that name alone meant auto-delete-on-archive silently |
| 27 |
// stopped firing. Both names stay registered so the integration keeps |
| 28 |
// working against older builds; `handled_archives` makes the overlap safe. |
| 29 |
add_action( 'fluent_boards/task_archived', array( $self, 'fbs_task_archive' ), 10, 1 ); |
| 30 |
add_action( 'fluent_boards/board_task_archived', array( $self, 'fbs_task_archive' ), 10, 1 ); |
| 31 |
add_filter( 'betterlinks__intlfbs_filter_category_from_dashboard', array( $self, 'filter_category_from_dashboard' ), 10, 2 ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Fluent Boards Task Delete |
| 36 |
*/ |
| 37 |
public function fbs_task_deleted( $task ) { |
| 38 |
$settings = Cache::get_json_settings(); |
| 39 |
if ( ! isset( $settings['fbs']['delete_on'] ) || 'task_delete' !== $settings['fbs']['delete_on'] ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
$this->fbs_shorten_link_delete( $task ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Fluent Boards Task Archive |
| 48 |
*/ |
| 49 |
public function fbs_task_archive( $task ) { |
| 50 |
$settings = Cache::get_json_settings(); |
| 51 |
if ( ! isset( $settings['fbs']['delete_on'] ) || 'task_archive' !== $settings['fbs']['delete_on'] ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
$task_id = isset( $task->id ) ? (int) $task->id : 0; |
| 56 |
if ( $task_id ) { |
| 57 |
if ( isset( $this->handled_archives[ $task_id ] ) ) { |
| 58 |
return; |
| 59 |
} |
| 60 |
$this->handled_archives[ $task_id ] = true; |
| 61 |
} |
| 62 |
|
| 63 |
$this->fbs_shorten_link_delete( $task ); |
| 64 |
} |
| 65 |
|
| 66 |
public function fbs_shorten_link_delete( $task ) { |
| 67 |
$page_url = fluent_boards_page_url(); |
| 68 |
$task_url = $page_url . 'boards/' . $task->board_id . '/tasks/' . $task->id; |
| 69 |
|
| 70 |
$link = Helper::get_link_by_permalink( $task_url, 'id, short_url' ); |
| 71 |
if ( ! empty( $link ) ) { |
| 72 |
$args = array( |
| 73 |
'ID' => ( isset( $link['id'] ) ? sanitize_text_field( $link['id'] ) : '' ), |
| 74 |
'short_url' => ( isset( $link['short_url'] ) ? sanitize_text_field( $link['short_url'] ) : '' ), |
| 75 |
); |
| 76 |
$this->delete_link( $args ); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Fluent Boards Category Filter from Dashboard |
| 82 |
* |
| 83 |
* Keeps the Fluent Boards task category off Manage Links unless the admin |
| 84 |
* opts to show it. The IDs are interpolated into the `get_prepare_all_links()` |
| 85 |
* query, so they are cast to int here. |
| 86 |
*/ |
| 87 |
public function filter_category_from_dashboard( $value, $settings ) { |
| 88 |
if ( ! empty( $settings['fbs']['enable_fbs'] ) && ! empty( $settings['fbs']['show_fbs_category'] ) ) { |
| 89 |
return $value; |
| 90 |
} |
| 91 |
$fbs_cat_arr = array(); |
| 92 |
$fbs_cat = Helper::get_term_by_slug( 'fluent-boards' ); |
| 93 |
if ( ! empty( $fbs_cat[0]['ID'] ) ) { |
| 94 |
array_push( $fbs_cat_arr, (int) $fbs_cat[0]['ID'] ); |
| 95 |
} |
| 96 |
|
| 97 |
if ( ! empty( $settings['fbs']['cat_id'] ) ) { |
| 98 |
array_push( $fbs_cat_arr, (int) $settings['fbs']['cat_id'] ); |
| 99 |
} |
| 100 |
|
| 101 |
// Never hide a category the dashboard depends on. The Fluent Boards |
| 102 |
// settings default `cat_id` to "Uncategorized" (ID 1), and that is where |
| 103 |
// every link without a category of its own lives — excluding it drops |
| 104 |
// Uncategorized and all of its links off Manage Links entirely. |
| 105 |
$protected = array_map( 'intval', (array) apply_filters( 'betterlinks/protected_term_ids', array( 1 ) ) ); |
| 106 |
$fbs_cat_arr = array_diff( array_unique( array_filter( $fbs_cat_arr ) ), $protected ); |
| 107 |
|
| 108 |
// `NOT IN ()` is a syntax error that would take the whole links query |
| 109 |
// down, so with nothing left to hide leave the query untouched. |
| 110 |
if ( empty( $fbs_cat_arr ) ) { |
| 111 |
return $value; |
| 112 |
} |
| 113 |
|
| 114 |
return sprintf( 'WHERE bt.ID NOT IN (%1$s)', implode( ',', $fbs_cat_arr ) ); |
| 115 |
} |
| 116 |
} |
| 117 |
|