| 1 |
<?php |
| 2 |
|
| 3 |
use AlexaCRM\WordpressCRM\Admin\Metabox\ShortcodeWizard; |
| 4 |
use AlexaCRM\WordpressCRM\Notifier; |
| 5 |
use AlexaCRM\WordpressCRM\Plugin; |
| 6 |
use Symfony\Component\HttpFoundation\Request; |
| 7 |
|
| 8 |
load_plugin_textdomain( 'integration-dynamics', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); |
| 9 |
|
| 10 |
/* Shortcode Wizard init */ |
| 11 |
// view shortcode |
| 12 |
add_action( 'wordpresscrm_sw_register', function( ShortcodeWizard $shortcodeWizard ) { |
| 13 |
$view = new ShortcodeWizard\Shortcode( 'view', __( 'View', 'integration-dynamics' ) ); |
| 14 |
$view->description = __( 'Renders a Dynamics 365 view as a table.', 'integration-dynamics' ); |
| 15 |
|
| 16 |
$entityField = new ShortcodeWizard\Field\Dropdown( 'entity', __( 'Entity name', 'integration-dynamics' ) ); |
| 17 |
$entityField->description = __( 'Name of the entity to display a view of.', 'integration-dynamics' ); |
| 18 |
$entityField->setValueGenerator( function() { |
| 19 |
try { |
| 20 |
$entities = ACRM()->getMetadata()->getEntitiesList(); |
| 21 |
asort( $entities ); |
| 22 |
|
| 23 |
return $entities; |
| 24 |
} catch ( \Exception $e ) { |
| 25 |
throw $e; |
| 26 |
} |
| 27 |
} ); |
| 28 |
$view->registerField( $entityField ); |
| 29 |
|
| 30 |
$viewField = new ShortcodeWizard\Field\Dropdown( 'name', __( 'Entity View name', 'integration-dynamics' ) ); |
| 31 |
$viewField->description = __( 'Name of the view to display.', 'integration-dynamics' ); |
| 32 |
$viewField->bindingFields = [ 'entity' ]; |
| 33 |
$viewField->setValueGenerator( function( $values ) { |
| 34 |
$client = ACRM()->getSdk(); |
| 35 |
if ( !$client ) { |
| 36 |
throw new \Exception( __( 'Not connected to CRM', 'integration-dynamics' ) ); |
| 37 |
} |
| 38 |
|
| 39 |
$views = []; |
| 40 |
|
| 41 |
if ( !array_key_exists( 'entity', $values ) ) { |
| 42 |
throw new \InvalidArgumentException( __( 'Entity name is not specified', 'integration-dynamics' ) ); |
| 43 |
} |
| 44 |
|
| 45 |
$entityName = trim( $values['entity'] ); |
| 46 |
|
| 47 |
if ( $entityName === '' ) { |
| 48 |
throw new \InvalidArgumentException( __( 'Empty entity name in the request', 'integration-dynamics' ) ); |
| 49 |
} |
| 50 |
|
| 51 |
$entity = $client->entity( $entityName ); |
| 52 |
|
| 53 |
$fetch = '<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false"> |
| 54 |
<entity name="userquery"> |
| 55 |
<attribute name="name" /> |
| 56 |
<attribute name="returnedtypecode" /> |
| 57 |
<filter type="and"> |
| 58 |
<condition attribute="returnedtypecode" operator="eq" value="' . $entity->metadata()->objectTypeCode . '" /> |
| 59 |
</filter> |
| 60 |
</entity> |
| 61 |
</fetch>'; |
| 62 |
|
| 63 |
$userQueries = $client->retrieveMultiple( $fetch ); |
| 64 |
|
| 65 |
$fetch = '<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false"> |
| 66 |
<entity name="savedquery"> |
| 67 |
<attribute name="name" /> |
| 68 |
<attribute name="returnedtypecode" /> |
| 69 |
<filter type="and"> |
| 70 |
<condition attribute="returnedtypecode" operator="eq" value="' . $entity->metadata()->objectTypeCode . '" /> |
| 71 |
</filter> |
| 72 |
</entity> |
| 73 |
</fetch>'; |
| 74 |
|
| 75 |
$savedQueries = $client->retrieveMultiple( $fetch ); |
| 76 |
|
| 77 |
$viewEntities = array_merge( $userQueries->Entities, $savedQueries->Entities ); |
| 78 |
|
| 79 |
foreach ( $viewEntities as $viewEntity ) { |
| 80 |
$views[$viewEntity->displayname] = $viewEntity->displayname; |
| 81 |
} |
| 82 |
|
| 83 |
asort( $views ); |
| 84 |
|
| 85 |
return $views; |
| 86 |
} ); |
| 87 |
$view->registerField( $viewField ); |
| 88 |
|
| 89 |
$countField = new ShortcodeWizard\Field\Number( 'count', __( 'Records per page', 'integration-dynamics' ) ); |
| 90 |
$countField->description = __( '0 disables pagination for the view.', 'integration-dynamics' ); |
| 91 |
$countField->setStaticValueGenerator( function() { |
| 92 |
return 10; |
| 93 |
} ); |
| 94 |
$view->registerField( $countField ); |
| 95 |
|
| 96 |
$shortcodeWizard->registerShortcode( $view ); |
| 97 |
} ); |
| 98 |
|
| 99 |
// field shortcode |
| 100 |
add_action( 'wordpresscrm_sw_register', function( ShortcodeWizard $shortcodeWizard ) { |
| 101 |
$field = new ShortcodeWizard\Shortcode( 'field', __( 'Field', 'integration-dynamics' ) ); |
| 102 |
$field->description = __( 'Renders a field value for the current CRM record on a data-bound page.', 'integration-dynamics' ); |
| 103 |
|
| 104 |
$entityField = new ShortcodeWizard\Field\Hidden( 'entity' ); |
| 105 |
if ( array_key_exists( 'post', $_GET ) ) { |
| 106 |
$entityField->setStaticValueGenerator( function() { |
| 107 |
$postId = (int)$_GET['post']; |
| 108 |
$bindingConfig = ACRM()->getBinding()->getPostBinding( $postId ); |
| 109 |
if ( $bindingConfig === null ) { |
| 110 |
return ''; |
| 111 |
} |
| 112 |
|
| 113 |
return $bindingConfig['entity']; |
| 114 |
} ); |
| 115 |
} |
| 116 |
$field->registerField( $entityField ); |
| 117 |
|
| 118 |
$attributeField = new ShortcodeWizard\Field\Dropdown( 'field', __( 'Attribute name', 'integration-dynamics' ) ); |
| 119 |
$attributeField->description = __( 'Entity attribute to render.', 'integration-dynamics' ); |
| 120 |
$attributeField->bindingFields = [ 'entity' ]; |
| 121 |
$attributeField->setValueGenerator( function( $values ) { |
| 122 |
$client = ACRM()->getSdk(); |
| 123 |
if ( !$client ) { |
| 124 |
return []; |
| 125 |
} |
| 126 |
|
| 127 |
$attributes = []; |
| 128 |
|
| 129 |
$entity = $client->entity( $values['entity'] ); |
| 130 |
|
| 131 |
if ( $entity->attributes ) { |
| 132 |
foreach ( $entity->attributes as $attribute ) { |
| 133 |
$attributes[$attribute->logicalName] = $attribute->logicalName.' ('.$attribute->label.')'; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
asort( $attributes ); |
| 138 |
|
| 139 |
return $attributes; |
| 140 |
} ); |
| 141 |
|
| 142 |
$field->registerField( $attributeField ); |
| 143 |
$shortcodeWizard->registerShortcode( $field ); |
| 144 |
} ); |
| 145 |
|
| 146 |
add_action( 'wp_ajax_wpcrm_log_verbosity', function() { |
| 147 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 148 |
wp_send_json( 'Access denied', 403 ); |
| 149 |
} |
| 150 |
|
| 151 |
$request = ACRM()->request->request; |
| 152 |
|
| 153 |
if ( ! wp_verify_nonce( $request->get( '_wpnonce' ), 'wpcrm_log_verbosity' ) ) { |
| 154 |
wp_send_json( 'Access denied', 403 ); |
| 155 |
} |
| 156 |
|
| 157 |
update_option( 'wpcrm_log_level', $request->get( 'logVerbosity', WORDPRESSCRM_EFFECTIVE_LOG_LEVEL ) ); |
| 158 |
|
| 159 |
wp_send_json_success(); |
| 160 |
} ); |
| 161 |
|
| 162 |
add_action( 'wp_ajax_wpcrm_log', function() { |
| 163 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 164 |
wp_send_json( 'Access denied', 403 ); |
| 165 |
} |
| 166 |
|
| 167 |
if ( ! wp_verify_nonce( ACRM()->request->query->get( '_wpnonce' ), 'wpcrm_log' ) ) { |
| 168 |
wp_send_json( 'Access denied', 403 ); |
| 169 |
} |
| 170 |
|
| 171 |
if ( class_exists( '\ZipArchive' ) && ( $zipPath = tempnam( sys_get_temp_dir(), 'wpcrm' ) ) ) { |
| 172 |
$zip = new ZipArchive(); |
| 173 |
$zip->open( $zipPath, ZipArchive::OVERWRITE ); |
| 174 |
$zip->addGlob( WORDPRESSCRM_STORAGE . '/*.log', 0, [ 'add_path' => DIRECTORY_SEPARATOR, 'remove_all_path' => true ] ); |
| 175 |
$zip->close(); |
| 176 |
|
| 177 |
header( 'Content-Description: File Transfer' ); |
| 178 |
header( 'Content-Type: application/octet-stream' ); |
| 179 |
|
| 180 |
$date = date( 'YmdHi' ); |
| 181 |
header( "Content-Disposition: attachment; filename=integration-dynamics_logs_{$date}.zip" ); |
| 182 |
|
| 183 |
readfile( $zipPath ); |
| 184 |
|
| 185 |
unlink( $zipPath ); |
| 186 |
|
| 187 |
wp_die(); |
| 188 |
} |
| 189 |
|
| 190 |
$logFiles = glob( WORDPRESSCRM_STORAGE . '/*.log' ); |
| 191 |
rsort( $logFiles ); |
| 192 |
$logPath = array_shift( $logFiles ); |
| 193 |
|
| 194 |
header( 'Content-Description: File Transfer' ); |
| 195 |
header( 'Content-Type: application/octet-stream' ); |
| 196 |
|
| 197 |
$filename = basename( $logPath ); |
| 198 |
header( 'Content-Disposition: attachment; filename=' . $filename ); |
| 199 |
|
| 200 |
readfile( $logPath ); |
| 201 |
|
| 202 |
wp_die(); |
| 203 |
} ); |
| 204 |
|
| 205 |
/** |
| 206 |
* Don't texturize Twig shortcode contents. |
| 207 |
*/ |
| 208 |
add_filter( 'no_texturize_shortcodes', function( $shortcodes ) { |
| 209 |
$shortcodes[] = Plugin::PREFIX . 'twig'; |
| 210 |
|
| 211 |
return $shortcodes; |
| 212 |
} ); |
| 213 |
|
| 214 |
add_filter( 'pre_handle_404', function( $preempt, \WP_Query $query ) { |
| 215 |
if ( is_admin() || !( $query->is_singular() && ACRM()->connected() ) ) { |
| 216 |
return $preempt; |
| 217 |
} |
| 218 |
|
| 219 |
$post = $query->post; |
| 220 |
if ( !$post ) { // non-existing post |
| 221 |
return $preempt; |
| 222 |
} |
| 223 |
|
| 224 |
$binding = ACRM()->getBinding(); |
| 225 |
$bindingConfig = $binding->getPostBinding( $post->ID ); |
| 226 |
if ( $bindingConfig === null ) { |
| 227 |
return $preempt; |
| 228 |
} |
| 229 |
|
| 230 |
$shouldTrigger404 = ( $bindingConfig['empty'] === '404' ); |
| 231 |
if ( apply_filters( 'wordpresscrm_data_binding_404', $shouldTrigger404 ) && $binding->getEntity( $post->ID ) === null ) { |
| 232 |
$query->set_404(); |
| 233 |
} |
| 234 |
|
| 235 |
return $preempt; |
| 236 |
}, 10, 2 ); |
| 237 |
|
| 238 |
add_filter( 'option_msdyncrm_options', function( $value, $option ) { |
| 239 |
$pwdHandler = new \AlexaCRM\WordpressCRM\PasswordHandler(); |
| 240 |
if ( !empty( $value['password'] ) ) { |
| 241 |
$value['password'] = $pwdHandler->decrypt( $value['password'] ); |
| 242 |
} |
| 243 |
if ( !empty( $value['clientSecret'] ) ) { |
| 244 |
$value['clientSecret'] = $pwdHandler->decrypt( $value['clientSecret'] ); |
| 245 |
} |
| 246 |
|
| 247 |
return $value; |
| 248 |
}, 10, 2 ); |
| 249 |
|
| 250 |
add_filter( "pre_update_option_msdyncrm_options", function( $value, $old_value, $option ) { |
| 251 |
$pwdHandler = new \AlexaCRM\WordpressCRM\PasswordHandler(); |
| 252 |
if ( !empty( $value['password'] ) ) { |
| 253 |
$value['password'] = $pwdHandler->encrypt( $value['password'] ); |
| 254 |
} |
| 255 |
if ( !empty( $value['clientSecret'] ) ) { |
| 256 |
$value['clientSecret'] = $pwdHandler->encrypt( $value['clientSecret'] ); |
| 257 |
} |
| 258 |
|
| 259 |
return $value; |
| 260 |
}, 10, 3 ); |
| 261 |
|
| 262 |
/** |
| 263 |
* Start initializing the plugin. |
| 264 |
*/ |
| 265 |
$pluginInstance = Plugin::instance(); |
| 266 |
$request = Request::createFromGlobals(); |
| 267 |
$pluginInstance->init( $logger, $request ); |
| 268 |
|
| 269 |
add_action( 'admin_notices', function() { |
| 270 |
$notifications = ACRM()->getNotifier()->getNotifications(); |
| 271 |
|
| 272 |
foreach ( $notifications as $notification ) { |
| 273 |
$classes = [ 'notice', Notifier::getNoticeClass( $notification['type'] ) ]; |
| 274 |
if ( $notification['isDismissible'] ) { |
| 275 |
$classes[] = 'is-dismissible'; |
| 276 |
} |
| 277 |
?> |
| 278 |
<div class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>"> |
| 279 |
<p><?php echo $notification['content']; ?></p> |
| 280 |
</div> |
| 281 |
<?php |
| 282 |
} |
| 283 |
} ); |
| 284 |
|
| 285 |
/** |
| 286 |
* Returns the main plugin object. |
| 287 |
* |
| 288 |
* @return Plugin |
| 289 |
*/ |
| 290 |
function ACRM() { |
| 291 |
return Plugin::instance(); |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Returns the CRM Toolkit object. |
| 296 |
* |
| 297 |
* @return \AlexaCRM\CRMToolkit\Client|null |
| 298 |
*/ |
| 299 |
function ASDK() { |
| 300 |
return ACRM()->getSdk(); |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* Forces a JavaScript redirect when headers have been already sent. |
| 305 |
* |
| 306 |
* Execution is halted after calling this method. |
| 307 |
* |
| 308 |
* @param string $location |
| 309 |
*/ |
| 310 |
function wordpresscrm_javascript_redirect( $location = null ) { |
| 311 |
if ( !headers_sent() ) { |
| 312 |
wp_redirect( $location ); |
| 313 |
exit(); |
| 314 |
} |
| 315 |
|
| 316 |
// Stop buffering the output, flush what's left. |
| 317 |
wp_ob_end_flush_all(); |
| 318 |
|
| 319 |
/* |
| 320 |
* Redirect using front-end JavaScript if the headers have already been sent. |
| 321 |
*/ |
| 322 |
?> |
| 323 |
<script type="text/javascript"> |
| 324 |
<!-- |
| 325 |
( function() { |
| 326 |
var newLocation = '.'; |
| 327 |
newLocation = <?php echo ( $location ) ? json_encode( $location ) : 'window.location.href'; ?>; |
| 328 |
|
| 329 |
if ( newLocation === '.' ) { |
| 330 |
window.location = window.location.href; |
| 331 |
return; |
| 332 |
} |
| 333 |
|
| 334 |
window.location = newLocation; |
| 335 |
}() ); |
| 336 |
//--> |
| 337 |
</script> |
| 338 |
<?php |
| 339 |
exit(); |
| 340 |
} |
| 341 |
|