-
Notifications
You must be signed in to change notification settings - Fork 2
/
synchronizing-civicrm-data-to-custom-posts.php
112 lines (103 loc) · 3.6 KB
/
synchronizing-civicrm-data-to-custom-posts.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
<?php
/*
Plugin Name: Synchronizing CiviCRM data to Custom Posts
Description: Provides a tool for synchronizing CiviCRM data to custom posts in Wordpress. You can use this plugin with Connector to CiviCRM with CiviMcRestFace (https://wordpress.org/plugins/connector-civicrm-mcrestface/)
Version: 1.0.4
Author: Jaap Jansma
License: AGPL3
License URI: https://www.gnu.org/licenses/agpl-3.0.html
Text Domain: synchronizing-civicrm-data-to-custom-posts
*/
/**
* Copyright (C) 2021 Jaap Jansma ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
define('SYNC_CIVICRM_CUSTOM_POST_PATH', plugin_dir_path( __FILE__ ));
define('SYNC_CIVICRM_CUSTOM_POST_LANG', 'SYNC_CIVICRM_CUSTOM_POST');
require_once(SYNC_CIVICRM_CUSTOM_POST_PATH.'SyncCiviCustomPost.class.php');
SyncCiviCustomPost::loadAll();
if (is_admin()) {
require_once(SYNC_CIVICRM_CUSTOM_POST_PATH . 'admin/admin.php');
}
/**
* Wrapper function for the CiviCRM api's.
* We use profiles to connect to different remote CiviCRM.
*
* @param $profile
* @param $entity
* @param $action
* @param $params
* @param array $options
* @param bool $ignore
*
* @return array|mixed|null
*/
function sync_civicrm_custom_post_api_wrapper($profile, $entity, $action, $params, $options=[], $ignore=false) {
$profiles = sync_civicrm_custom_post_get_profiles();
if (isset($profiles[$profile])) {
if (isset($profiles[$profile]['file'])) {
require_once($profiles[$profile]['file']);
}
$result = call_user_func($profiles[$profile]['function'], $profile, $entity, $action, $params, $options);
} else {
$result = ['error' => 'Profile not found', 'is_error' => 1];
}
if (!empty($result['is_error']) && $ignore) {
return null;
}
return $result;
}
/**
* Call the CiviCRM api through CiviMcRestFace.
*
* @param $profile
* @param $entity
* @param $action
* @param $params
* @param array $options
*
* @return mixed
*/
function sync_civicrm_custom_post_wpcmrf_api($profile, $entity, $action, $params, $options = []) {
$profile_id = substr($profile, 15);
$call = wpcmrf_api($entity, $action, $params, $options, $profile_id);
return $call->getReply();
}
/**
* Returns a list of possible profiles
* @return array
*/
function sync_civicrm_custom_post_get_profiles() {
static $profiles = null;
if (is_array($profiles)) {
return $profiles;
}
$profiles = array();
require_once(SYNC_CIVICRM_CUSTOM_POST_PATH.'includes/class-local-civicrm.php');
$profiles = Sync_CiviCRM_CustomPost_Connector_LocalCiviCRM::loadProfile($profiles);
if (function_exists('wpcmrf_get_core')) {
$core = wpcmrf_get_core();
$wpcmrf_profiles = $core->getConnectionProfiles();
foreach($wpcmrf_profiles as $profile) {
$profile_name = 'wpcmrf_profile_'.$profile['id'];
$profiles[$profile_name] = [
'title' => $profile['label'],
'function' => 'sync_civicrm_custom_post_wpcmrf_api',
];
}
}
$profiles = apply_filters('sync_civicrm_custom_post_get_profiles', $profiles);
return $profiles;
}