| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace FL\Assistant\Providers; |
| 5 |
|
| 6 |
|
| 7 |
use FL\Assistant\Hooks\Actions\OnEditUserProfile; |
| 8 |
use FL\Assistant\Hooks\Actions\OnEnqueueScripts; |
| 9 |
use FL\Assistant\Hooks\Actions\OnPersonalOptionsUpdate; |
| 10 |
use FL\Assistant\Hooks\Actions\OnWPBeforeAdminBarRender; |
| 11 |
use FL\Assistant\Hooks\Actions\OnBeforeDeletePost; |
| 12 |
use FL\Assistant\Hooks\Actions\OnDeleteTerm; |
| 13 |
use FL\Assistant\Hooks\Filters\OnHeartbeatReceived; |
| 14 |
use FL\Assistant\Hooks\Filters\OnFLBuilderUIBarButtons; |
| 15 |
use FL\Assistant\System\Contracts\ServiceProviderAbstract; |
| 16 |
use FL\Assistant\Data\Transformers\NotationsTransformer; |
| 17 |
use FL\Assistant\Data\Repository\NotationsRepository; |
| 18 |
use FL\Assistant\Data\Repository\LabelsRepository; |
| 19 |
use FL\Assistant\Data\Transformers\LabelsTransformer; |
| 20 |
|
| 21 |
class HooksServiceProvider extends ServiceProviderAbstract { |
| 22 |
|
| 23 |
/** |
| 24 |
* @throws \FL\Assistant\System\Container\InjectionException |
| 25 |
*/ |
| 26 |
public function bootstrap() { |
| 27 |
|
| 28 |
register_activation_hook( |
| 29 |
FL_ASSISTANT_FILE, function () { |
| 30 |
do_action( 'fl_assistant_activate' ); |
| 31 |
} |
| 32 |
); |
| 33 |
|
| 34 |
$this->actions(); |
| 35 |
$this->filters(); |
| 36 |
} |
| 37 |
|
| 38 |
public function actions() { |
| 39 |
|
| 40 |
// Enqueue Assistant frontend |
| 41 |
$enqueue_scripts = $this->injector->make( OnEnqueueScripts::class ); |
| 42 |
add_action( 'wp_enqueue_scripts', $enqueue_scripts ); |
| 43 |
add_action( 'admin_enqueue_scripts', $enqueue_scripts ); |
| 44 |
|
| 45 |
// setup user profile meta fields - shows on YOUR profile, not on others. |
| 46 |
add_action( 'show_user_profile', $this->injector->make( OnEditUserProfile::class ) ); |
| 47 |
add_action( 'personal_options_update', $this->injector->make( OnPersonalOptionsUpdate::class ) ); |
| 48 |
|
| 49 |
// Add Assistant Toolbar Item |
| 50 |
add_action( 'wp_before_admin_bar_render', $this->injector->make( OnWPBeforeAdminBarRender::class ) ); |
| 51 |
|
| 52 |
// post actions |
| 53 |
add_action( 'before_delete_post', $this->injector->make( OnBeforeDeletePost::class ) ); |
| 54 |
|
| 55 |
// taxonomy actions |
| 56 |
add_action( 'delete_term', $this->injector->make( OnDeleteTerm::class ) ); |
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
public function filters() { |
| 61 |
|
| 62 |
// setup heartbeat |
| 63 |
add_filter( 'heartbeat_received', $this->injector->make( OnHeartbeatReceived::class ), 11, 2 ); |
| 64 |
|
| 65 |
add_filter( 'fl_builder_ui_bar_buttons', $this->injector->make( OnFLBuilderUIBarButtons::class ) ); |
| 66 |
|
| 67 |
// Setup custom WP admin post list columns |
| 68 |
$this->enqueue_custom_admin_columns(); |
| 69 |
} |
| 70 |
|
| 71 |
public function enqueue_custom_admin_columns() { |
| 72 |
|
| 73 |
$types = get_post_types( [ 'public' => true ], 'objects' ); |
| 74 |
|
| 75 |
foreach ( $types as $type => $info ) { |
| 76 |
add_filter( "manage_{$type}_posts_columns", [ $this, 'add_columns' ] ); |
| 77 |
add_action( "manage_{$type}_posts_custom_column", [ $this, 'render_column' ], 10, 2 ); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
public function add_columns( $columns ) { |
| 82 |
$columns['fl_assistant'] = __( 'Assistant', 'fl-assistant' ); |
| 83 |
return $columns; |
| 84 |
} |
| 85 |
|
| 86 |
public function render_column( $column, $post_id ) { |
| 87 |
$vars = '--fl-asst-red: #FF5335; --fl-asst-blue: #1BADF8; --fl-asst-green: #00D281; --fl-asst-yellow: #FFD000; --fl-asst-orange: #FF9500; --fl-asst-purple: #CC73E1; --fl-asst-pink: #FF2968;'; |
| 88 |
$styles = "{$vars} padding: 2px 5px; display: inline-flex; flex: 0 0 auto; border-radius: 20px; border: 1px solid #d2d2d2; align-items: center; color: black; background: white; margin: 2px 5px 3px; margin-left:0;"; |
| 89 |
$dot = 'width: 10px; height: 10px; flex: 0 0 10px; border-radius: 7px; background: blue; margin-right: 5px;'; |
| 90 |
|
| 91 |
if ( 'fl_assistant' === $column ) { |
| 92 |
$transformer = new NotationsTransformer; |
| 93 |
$repository = new NotationsRepository( $transformer ); |
| 94 |
$labels = $repository->get_labels( 'post', $post_id ); |
| 95 |
|
| 96 |
$labels_repo = new LabelsRepository; |
| 97 |
$labels_transformer = new LabelsTransformer( $labels_repo ); |
| 98 |
$terms = $labels_repo->query( [ 'hide_empty' => false ] )->get_terms(); |
| 99 |
|
| 100 |
// Assemble term meta dataset |
| 101 |
$term_meta = []; |
| 102 |
foreach ( $terms as $key => $term ) { |
| 103 |
$item = call_user_func( $labels_transformer, $term ); |
| 104 |
$term_meta[ $item['id'] ] = $item; |
| 105 |
} |
| 106 |
|
| 107 |
// Output labels |
| 108 |
foreach ( $labels as $label ) { |
| 109 |
$term = $term_meta[ $label['label_id'] ]; |
| 110 |
if ( isset( $term ) ) { |
| 111 |
print "<span style='{$styles}'><span style='{$dot}; background-color: {$term['color']}'></span>{$term['label']}</span>"; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|