Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 132
0.00% covered (danger)
0.00%
0 / 5
CRAP
n/a
0 / 0
zeroBS_baseExternalSources
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 1
2
jpcrm_get_external_source_info
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
zeroBS_getExternalSourceTitle
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
182
jpcrm_render_external_sources_by_id
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
jpcrm_render_external_sources_info
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 1
110
1<?php
2/*
3 * Jetpack CRM
4 * https://jetpackcrm.com
5 * V2.97.4+
6 *
7 * Copyright 2020 Automattic
8 *
9 * Date: 11/01/2019
10 */
11
12defined( 'ZEROBSCRM_PATH' ) || exit( 0 );
13
14    /*
15        External sources got a bit muddy in DAL1, This file is designed to centralise + simplify :)
16
17    */
18
19    // Init set of external sources
20    /*
21        // Proper way to add to these is (in extension plugin class):
22        // key is that filter has priority below 99
23        // .. can then be used anywhere POST init:99
24
25        public function register_external_source($external_sources = array()){
26            $external_sources['str'] = array('Stripe', 'ico' => 'fa-stripe');
27            return $external_sources;
28        }
29
30        #} adds this as an external source
31        add_filter('zbs_approved_sources' , array($this, 'register_external_source'), 10);
32
33    */
34    global  $zbscrmApprovedExternalSources;
35            $zbscrmApprovedExternalSources = zeroBS_baseExternalSources();
36
37// 2.97.7 wrapped these in a func so they can be less affected than a global ? :/
38// this is called directly now in core to load them, rather than using the global $zbscrmApprovedExternalSources;
39// ... global $zbscrmApprovedExternalSources is still set though, for backward compat, (not sure if some older ext using?)
40function zeroBS_baseExternalSources() {
41    $external_sources = array(
42        'woo'          => array(
43            'WooCommerce',
44            'ico' => 'fa-shopping-cart',
45        ), // fa-shopping-cart is default :) no woo yet.
46        'pay'          => array(
47            'PayPal',
48            'ico' => 'fa-paypal',
49        ),
50        'env'          => array(
51            'Envato',
52            'ico' => 'fa-envira',
53        ), // fa-envira is a look-alike http://fontawesome.io/icon/envira/.
54        'csv'          => array(
55            'CSV Import',
56            'ico' => 'fa-file-text',
57        ),
58        'form'         => array(
59            'Form Capture',
60            'ico' => 'fa-wpforms',
61        ),
62        'gra'          => array(
63            'Gravity Forms',
64            'ico' => 'fa-wpforms',
65        ),
66        'api'          => array(
67            'API',
68            'ico' => 'fa-random',
69        ),
70        'wpa'          => array(
71            'WorldPay',
72            'ico' => 'fa-credit-card',
73        ),
74        'str'          => array(
75            'Stripe',
76            'ico' => 'fa-credit-card',
77        ),
78        'wordpress'    => array(
79            'WordPress',
80            'ico' => 'fa-wpforms',
81        ),
82        'cf7'          => array(
83            'Contact Form 7',
84            'ico' => 'fa-wpforms',
85        ),
86        'jetpack_form' => array(
87            'Jetpack Contact Form',
88            'ico' => 'fa-wpforms',
89        ),
90
91        // Discontinued
92        // 'jvz'          => array( 'JV Zoo', 'ico' => 'fa-paypal' ),
93    );
94
95    $external_sources = apply_filters( 'jpcrm_register_external_sources', $external_sources );
96    return $external_sources;
97}
98
99/*
100* Simple external source info return based on key
101*/
102function jpcrm_get_external_source_info( $source_key ) {
103
104    $external_sources = zeroBS_baseExternalSources();
105
106    if ( isset( $external_sources[ $source_key ] ) ) {
107
108        return $external_sources[ $source_key ];
109
110    }
111
112    return false;
113}
114
115// Returns a simplified 1 line explanation of an external source
116function zeroBS_getExternalSourceTitle( $srcKey = '', $srcUID = '' ) {
117
118    // some old hard typed:
119
120    switch ( $srcKey ) {
121
122        case 'pay': #} paypal
123            return '<i class="fa fa-paypal"></i> PayPal:<br /><span>' . $srcUID . '</span>';
124
125            break;
126
127        #case 'woo': #} Woo
128        case 'env':
129            return '<i class="fa fa-envira"></i> Envato:<br /><span>' . $srcUID . '</span>';
130
131            break;
132
133        case 'form':
134            return '<i class="fa fa-wpforms"></i> Form Capture:<br /><span>' . $srcUID . '</span>';
135
136            break;
137
138        case 'csv':
139            return '<i class="fa fa-file-text"></i> CSV Import:<br /><span>' . $srcUID . '</span>';
140
141            break;
142
143        case 'gra':
144            return '<i class="fa fa-wpforms"></i> Gravity Forms:<br /><span>' . $srcUID . '</span>';
145
146            break;
147
148        case 'api':
149            return '<i class="fa fa-random"></i> API:<br /><span>' . $srcUID . '</span>';
150
151            break;
152
153        default:
154            // see if in $zbs->external_sources
155            global $zbs;
156
157            if ( isset( $zbs->external_sources[ $srcKey ] ) ) {
158
159                $ico = 'fa-users';
160                if ( is_array( $zbs->external_sources[ $srcKey ] ) && isset( $zbs->external_sources[ $srcKey ]['ico'] ) ) {
161                    $ico = $zbs->external_sources[ $srcKey ]['ico'];
162                }
163                $name = ucwords( str_replace( '_', ' ', $srcKey ) );
164                if ( is_array( $zbs->external_sources[ $srcKey ] ) && isset( $zbs->external_sources[ $srcKey ][0] ) ) {
165                    $name = $zbs->external_sources[ $srcKey ][0];
166                }
167
168                return '<i class="fa ' . $ico . '"></i> ' . $name . ':<br /><span>' . $srcUID . '</span>';
169
170            } else {
171
172                #} Generic for now
173                return '<i class="fa fa-users"></i> ' . ucwords( str_replace( '_', ' ', $srcKey ) ) . ':<br /><span>' . $srcUID . '</span>';
174
175            }
176
177            break;
178
179    }
180}
181
182/*
183* Renders HTML describing external sources for an object
184*  Primarily used in object 'external source' metaboxes
185*
186* @param int $object_id (crm contact, company, invoice, or transaction)
187* @param int $object_type_id
188*/
189function jpcrm_render_external_sources_by_id( $object_id, $object_type_id ) {
190
191    global $zbs;
192
193    // get sources, if any
194    $external_sources = $zbs->DAL->getExternalSources(
195        -1,
196        array(
197
198            'objectID'          => $object_id,
199            'objectType'        => $object_type_id,
200            'grouped_by_source' => true,
201            'ignoreowner'       => zeroBSCRM_DAL2_ignoreOwnership( ZBS_TYPE_CONTACT ),
202
203        )
204    );
205
206    // render
207    jpcrm_render_external_sources_info( $external_sources, $object_id, $object_type_id );
208}
209
210/*
211* Renders HTML describing external sources for an object
212*  Primarily used in object 'external source' metaboxes
213*  ... but also used on contact view etc.
214*
215* @param array $external_sources (requires these in 'grouped' format)
216*/
217function jpcrm_render_external_sources_info( $external_sources, $object_id, $object_type_id ) {
218
219    global $zbs;
220
221    // got any to render?
222    if ( isset( $external_sources ) && is_array( $external_sources ) && count( $external_sources ) > 0 ) {
223
224        if ( count( $external_sources ) > 0 ) {
225
226            echo '<div id="jpcrm-external-sources-metabox">';
227
228            // first cycle through sources and stack by origin key (e.g. 'woo'),
229            // so we can group if multiple.
230
231            foreach ( $external_sources as $external_source_group_key => $external_source_group ) {
232
233                // 'woo' => array( 'WooCommerce', 'ico' => 'fa-shopping-cart' )
234                $external_source_group_info = jpcrm_get_external_source_info( $external_source_group_key );
235
236                // got multiple of same source? (e.g. woo customer with multiple orders)
237                $multiple_in_group = ( count( $external_source_group ) > 1 );
238
239                // show group header
240                echo '<div class="jpcrm-external-source-group">';
241
242                    echo '<div class="jpcrm-external-source-group-header ui header">' . ( is_array( $external_source_group_info ) ? '<i class="fa ' . esc_attr( $external_source_group_info['ico'] ) . '"></i>&nbsp;&nbsp;' . $external_source_group_info[0] : $external_source_group_key ) . '</div>';
243
244                foreach ( $external_source_group as $external_source ) {
245
246                    #} Display a "source"
247                    echo '<div class="jpcrm-external-source">';
248
249                        $uid = $external_source['uid'];
250
251                        // company + CSV means uid will be a useless hash, so replace that with name if we have
252                    if ( $external_source['source'] == 'csv' && $object_type_id == ZBS_TYPE_COMPANY ) {
253                        $uid = __( 'Imported based on name', 'zero-bs-crm' );
254                    }
255
256                        // build basic
257                        $external_source_html = zeroBS_getExternalSourceTitle( $external_source['source'], $uid );
258
259                        // filter any given title - can be wired in to give links (e.g. wooc orders)
260                        $external_source_html = apply_filters(
261                            'zbs_external_source_infobox_line',
262                            $external_source_html,
263                            array(
264                                'objtype'   => $object_type_id,
265                                'objid'     => $object_id,
266                                'source'    => $external_source['source'],
267                                'origin'    => $external_source['origin'],
268                                'unique_id' => $uid,
269                            )
270                        );
271
272                        // output
273                        echo $external_source_html;
274
275                    echo '</div>';
276
277                }
278
279                echo '</div>';
280
281            }
282
283            echo '</div>';
284
285        }
286    } else {
287
288        // manually added
289        echo '<p><i class="address book icon"></i> ' . esc_html( sprintf( __( '%s added manually.', 'zero-bs-crm' ), ucwords( $zbs->DAL->objTypeKey( $object_type_id ) ) ) ) . '</p>';
290
291    }
292}