Ajax
Class for all ajax related functions
Description
Source
File: bp-integrations/learndash/buddypress/Ajax.php
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | class Ajax { protected $bpGroup = null; protected $ldGroup = null; /** * Constructor * * @since BuddyBoss 1.0.0 */ public function __construct() { add_action( 'bp_ld_sync/init' , [ $this , 'init' ]); } /** * Add actions once integration is ready * * @since BuddyBoss 1.0.0 */ public function init() { add_action( 'wp_ajax_bp_ld_group_get_reports' , [ $this , 'ajaxGetReports' ]); add_action( 'wp_ajax_download_bp_ld_reports' , [ $this , 'ajaxDownloadReport' ]); add_action( 'bp_ld_sync/ajax/post_fetch_reports' , [ $this , 'ajaxGetExports' ]); add_action( 'bp_ld_sync/report_columns' , [ $this , 'removeIdsOnNonExport' ], 10, 2); add_action( 'bp_ld_sync/reports_generator_args' , [ $this , 'unsetCompletionOnExport' ]); } /** * Get reports * * @since BuddyBoss 1.0.0 */ public function ajaxGetReports() { $this ->enableDebugOnDev(); $this ->validateRequest(); $generator = $this ->getGenerator(); /** * Hook before the data is fetched, in cause of overwriting the post value * * @since BuddyBoss 1.0.0 */ do_action( 'bp_ld_sync/ajax/pre_fetch_reports' , $generator ); $generator ->fetch(); /** * Hook after the data is fetched, in cause of overwriting results value * * @since BuddyBoss 1.0.0 */ do_action( 'bp_ld_sync/ajax/post_fetch_reports' , $generator ); echo json_encode([ 'draw' => (int) bp_ld_sync()->getRequest( 'draw' ), 'recordsTotal' => $generator ->getPager()[ 'total_items' ], 'recordsFiltered' => $generator ->getPager()[ 'total_items' ], 'data' => $generator ->getData(), ]); header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' )); wp_die(); // wp_send_json_success([ // 'draw' => (int) bp_ld_sync()->getRequest('draw'), // 'results' => $generator->getData(), // 'pager' => $generator->getPager(), // ]); } /** * Unset the completed status when exporting * * @since BuddyBoss 1.0.0 */ public function unsetCompletionOnExport( $args ) { if (bp_ld_sync()->getRequest( 'export' )) { $args [ 'completed' ] = null; } return $args ; } /** * Remove the id fields when fetching for display only * * @since BuddyBoss 1.0.0 */ public function removeIdsOnNonExport( $column , $args ) { if (! isset( $args [ 'report' ])) { unset( $column [ 'user_id' ]); unset( $column [ 'course_id' ]); } return $column ; } /** * Get export data from report generator * * @since BuddyBoss 1.0.0 */ public function ajaxGetExports( $generator ) { if (! bp_ld_sync()->getRequest( 'export' )) { return ; } return $generator ->export(); } /** * Output the export content to header buffer * * @since BuddyBoss 1.0.0 */ public function ajaxDownloadReport() { $hash = bp_ld_sync()->getRequest( 'hash' ); $exports = get_transient( $hash ); $info = get_transient( "{$hash}_info" ); if (! $hash || ! $exports ) { wp_die(__( 'Session has expired, please refresh and try again.' , 'buddyboss' )); } $file = fopen ( 'php://output' , 'w' ); fputcsv ( $file , wp_list_pluck( $info [ 'columns' ], 'label' )); foreach ( $exports as $export ) { fputcsv ( $file , $export ); } header( 'Content-Encoding: ' . DB_CHARSET); header( 'Content-type: text/csv; charset=' .DB_CHARSET); header( 'Content-Disposition: attachment; filename=' . $info [ 'filename' ]); header( 'Pragma: no-cache' ); header( 'Expires: 0' ); fclose( $df ); die (); } /** * Enable error reporting on local development (internal use only) * * @since BuddyBoss 1.0.0 */ protected function enableDebugOnDev() { if ( strpos (get_bloginfo( 'url' ), '.test' ) === false) { return ; } error_reporting (E_ALL); ini_set ( "display_errors" , 1); } /** * Validate the ajax request * * @since BuddyBoss 1.0.0 */ protected function validateRequest() { if (! wp_verify_nonce(bp_ld_sync()->getRequest( 'nonce' ), 'bp_ld_report' )) { wp_send_json_error([ 'message' => __( 'Session has expired, please refresh and try again.' , 'buddyboss' ) ]); } if ( $this ->setRequestGroups() && ( ! $this ->bp_group || ! $this ->ld_group ) ) { wp_send_json_error([ 'message' => __( 'Unable to find selected group.' , 'buddyboss' ) ]); } } /** * Setup the current bp and ld groups on ajax request * * @since BuddyBoss 1.0.0 */ protected function setRequestGroups() { if (! $groupId = bp_ld_sync()->getRequest( 'group' )) { return ; } $bpGroup = groups_get_group( $groupId ); if (! $bpGroup ->id) { return ; } $this ->bpGroup = $bpGroup ; $this ->ldGroup = get_post(bp_ld_sync( 'buddypress' )->helpers->getLearndashGroupId( $groupId )); } /** * Get the generator class based on the request * * @since BuddyBoss 1.0.0 */ protected function getGenerator() { $generators = bp_ld_sync( 'buddypress' )->reports->getGenerators(); $type = bp_ld_sync()->getRequest( 'step' ); return ( new $generators [ $type ][ 'class' ]); } } |
Changelog
Version | Description |
---|---|
BuddyBoss 1.0.0 | Introduced. |
Methods
- __construct — Constructor
- ajaxDownloadReport — Output the export content to header buffer
- ajaxGetExports — Get export data from report generator
- ajaxGetReports — Get reports
- enableDebugOnDev — Enable error reporting on local development (internal use only)
- getGenerator — Get the generator class based on the request
- init — Add actions once integration is ready
- removeIdsOnNonExport — Remove the id fields when fetching for display only
- setRequestGroups — Setup the current bp and ld groups on ajax request
- unsetCompletionOnExport — Unset the completed status when exporting
- validateRequest — Validate the ajax request
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.