form-record-user-journey-box.php
176 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\User_Journey\Admin\Meta_Boxes; |
| 5 | |
| 6 | use JFB_Modules\User_Journey\Admin\View_Columns\User_Journey_Date_Column; |
| 7 | use JFB_Modules\User_Journey\Admin\View_Columns\User_Journey_Url_Column; |
| 8 | use JFB_Modules\User_Journey\Admin\View_Columns\User_Journey_Title_Column; |
| 9 | use JFB_Modules\User_Journey\Admin\View_Columns\User_Journey_Query_Column; |
| 10 | use JFB_Modules\User_Journey\Admin\View_Columns\User_Journey_Duration_Column; |
| 11 | use JFB_Modules\User_Journey\Rest_Endpoints\Fetch_User_Journey_Box_Endpoint; |
| 12 | use JFB_Modules\User_Journey\Models\User_Journey_Model; |
| 13 | use JFB_Modules\Form_Record\Query_Views\Record_View; |
| 14 | use JFB_Modules\User_Journey\Query_Views\User_Journey_View; |
| 15 | use JFB_Modules\User_Journey\Query_Views\User_Journey_View_Count; |
| 16 | use Jet_Form_Builder\Admin\Exceptions\Empty_Box_Exception; |
| 17 | use Jet_Form_Builder\Admin\Single_Pages\Meta_Boxes\Base_Table_Box; |
| 18 | use Jet_Form_Builder\Admin\Table_Views\Column_Base; |
| 19 | use Jet_Form_Builder\Exceptions\Query_Builder_Exception; |
| 20 | |
| 21 | // If this file is called directly, abort. |
| 22 | if ( ! defined( 'WPINC' ) ) { |
| 23 | die; |
| 24 | } |
| 25 | |
| 26 | class Form_Record_User_Journey_Box extends Base_Table_Box { |
| 27 | |
| 28 | protected $show_overflow = true; |
| 29 | |
| 30 | public function get_title(): string { |
| 31 | return __( 'User Journey', 'jet-form-builder' ); |
| 32 | } |
| 33 | |
| 34 | public function get_dependencies(): array { |
| 35 | return array( |
| 36 | new User_Journey_Model(), |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @return Column_Base[] |
| 42 | */ |
| 43 | public function get_columns(): array { |
| 44 | return array( |
| 45 | 'date' => new User_Journey_Date_Column(), |
| 46 | 'title' => new User_Journey_Title_Column(), |
| 47 | 'url' => new User_Journey_Url_Column(), |
| 48 | 'query' => new User_Journey_Query_Column(), |
| 49 | 'duration' => new User_Journey_Duration_Column(), |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @param array $args |
| 55 | * |
| 56 | * @return array |
| 57 | * @throws Empty_Box_Exception |
| 58 | */ |
| 59 | public function get_raw_list( array $args ): array { |
| 60 | try { |
| 61 | $per_page = isset( $args['limit'] ) ? (int) $args['limit'] : 10; |
| 62 | $current_page = isset( $args['page'] ) ? (int) $args['page'] : 1; |
| 63 | $offset = isset( $args['offset'] ) ? (int) $args['offset'] : 0; |
| 64 | |
| 65 | $query_args = array( |
| 66 | 'record_id' => $this->get_id(), |
| 67 | 'limit' => $per_page, |
| 68 | 'paged' => $current_page, |
| 69 | 'offset' => $offset, |
| 70 | ); |
| 71 | |
| 72 | $journey_items = User_Journey_View::find( |
| 73 | array( 'record_id' => $this->get_id() ) |
| 74 | )->set_table_args( $query_args ) |
| 75 | ->query() |
| 76 | ->query_all(); |
| 77 | |
| 78 | if ( empty( $journey_items ) ) { |
| 79 | return array(); |
| 80 | } |
| 81 | |
| 82 | $record_created_at = $this->get_form_record_status_by_id( $journey_items[0]['record_id'] ); |
| 83 | $journey_items = $this->add_journey_durations_to_journey( $journey_items, $record_created_at ); |
| 84 | |
| 85 | return $journey_items; |
| 86 | |
| 87 | } catch ( Query_Builder_Exception $exception ) { |
| 88 | throw new Empty_Box_Exception( |
| 89 | esc_html( $exception->getMessage() ), |
| 90 | // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 91 | ...$exception->get_additional() |
| 92 | ); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | public function get_prepared_list( array $custom_list = array() ): array { |
| 97 | return apply_filters( |
| 98 | 'jet-form-builder/user-journey/list', |
| 99 | $this->prepare_list( $custom_list ) |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | public function get_total(): int { |
| 104 | return User_Journey_View_Count::count( |
| 105 | array( |
| 106 | 'record_id' => $this->get_id(), |
| 107 | ) |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | public function get_rest_url(): string { |
| 112 | return Fetch_User_Journey_Box_Endpoint::dynamic_rest_url( |
| 113 | array( |
| 114 | 'id' => $this->get_id(), |
| 115 | ) |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | public function get_rest_methods(): string { |
| 120 | return Fetch_User_Journey_Box_Endpoint::get_methods(); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Get form record status by ID |
| 125 | * |
| 126 | * @param int $record_id The ID of the form record |
| 127 | * @return string|null The status of the record or null if not found |
| 128 | */ |
| 129 | public function get_form_record_status_by_id( $record_id ) { |
| 130 | try { |
| 131 | $record = Record_View::find( array( 'id' => $record_id ) ) |
| 132 | ->query() |
| 133 | ->query_one(); |
| 134 | |
| 135 | return $record['created_at'] ?? null; |
| 136 | } catch ( Query_Builder_Exception $e ) { |
| 137 | return null; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | public function add_journey_durations_to_journey( $journey_items, string $record_created_at ) { |
| 142 | if ( empty( $journey_items ) ) { |
| 143 | return array(); |
| 144 | } |
| 145 | |
| 146 | $result = array(); |
| 147 | |
| 148 | foreach ( $journey_items as $index => $item ) { |
| 149 | if ( isset( $journey_items[ $index + 1 ] ) ) { |
| 150 | $current_timestamp = $item['timestamp'] ?? 0; |
| 151 | $next_timestamp = $journey_items[ $index + 1 ]['timestamp'] ?? 0; |
| 152 | $duration = (int) ( ( $next_timestamp - $current_timestamp ) / 1000 ); |
| 153 | |
| 154 | $item['duration'] = $duration; |
| 155 | } else { |
| 156 | $timezone = wp_timezone(); |
| 157 | |
| 158 | $record_created_at_dt = \DateTime::createFromFormat( |
| 159 | 'Y-m-d H:i:s', |
| 160 | $record_created_at, |
| 161 | $timezone |
| 162 | ); |
| 163 | |
| 164 | $record_timestamp = (int) $record_created_at_dt->format( 'U' ) * 1000; |
| 165 | $current_timestamp = $item['timestamp'] ?? 0; |
| 166 | $duration = (int) ( ( $record_timestamp - $current_timestamp ) / 1000 ); |
| 167 | $item['duration'] = $duration; |
| 168 | } |
| 169 | |
| 170 | $result[] = $item; |
| 171 | } |
| 172 | |
| 173 | return $result; |
| 174 | } |
| 175 | } |
| 176 |