get_meta_item( 'annotations', array() ); return ! empty( $annotations['readonly'] ) ? 'read' : 'write'; } /** * @param string $name Ability name. * @param mixed $input Ability input. * @return array{0: string, 1: int} Object type and ID. */ function tlkp_ability_object( $name, $input ) { $input = is_array( $input ) ? $input : array(); $map = array( 'product_id' => 'product', 'order_id' => 'order', 'customer_id' => 'customer', ); foreach ( $map as $key => $type ) { if ( ! empty( $input[ $key ] ) ) { return array( $type, absint( $input[ $key ] ) ); } } $type = ''; if ( false !== strpos( $name, 'product' ) ) { $type = 'product'; } elseif ( false !== strpos( $name, 'order' ) ) { $type = 'order'; } elseif ( false !== strpos( $name, 'customer' ) ) { $type = 'customer'; } return array( $type, ! empty( $input['id'] ) ? absint( $input['id'] ) : 0 ); } /** * @param string $name Ability name. * @param mixed $input Ability input. * @param mixed $result Ability result. */ function tlkp_on_ability_executed( $name, $input, $result ) { if ( ! tlkp_is_logged_ability( $name ) ) { return; } list( $type, $id ) = tlkp_ability_object( $name, $input ); $outcome = 'ok'; $note = ''; if ( is_array( $result ) && isset( $result['tillkeeper']['status'] ) ) { $outcome = (string) $result['tillkeeper']['status']; if ( ! empty( $result['tillkeeper']['reasons'] ) && is_array( $result['tillkeeper']['reasons'] ) ) { $note = implode( ' ', array_map( 'strval', $result['tillkeeper']['reasons'] ) ); } } tlkp_activity_log_record( array( 'ability' => $name, 'kind' => tlkp_ability_kind( $name ), 'object_type' => $type, 'object_id' => $id, 'outcome' => $outcome, 'note' => $note, ) ); } add_action( 'wp_after_execute_ability', 'tlkp_on_ability_executed', 10, 3 );