Skip to content

Commit

Permalink
WPCV-24 add support for origin filter - v0.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
agileware-pengyi committed Nov 8, 2019
1 parent 35a9487 commit 62bfbc5
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 63 deletions.
8 changes: 2 additions & 6 deletions content-views-civicrm.php
Expand Up @@ -2,7 +2,7 @@
/**
* Plugin Name: Content Views CiviCRM
* Description: CiviCRM integraton for Content Views.
* Version: 0.1
* Version: 0.1.2
* Author: Agileware
* Author URI: https://github.com/agileware/content-views-civicrm
* Plugin URI: https://github.com/agileware/content-views-civicrm
Expand All @@ -18,7 +18,7 @@ class Content_Views_CiviCRM {
* @since 0.1
* @var string $version
*/
protected $version = '0.1';
protected $version = '0.1.2';

/**
* Pro dependency.
Expand Down Expand Up @@ -132,10 +132,6 @@ private function check_dependencies() {
if ( ! function_exists( 'civi_wp' ) ) {
return false;
}
// data processor
if ( ! function_exists( 'dataprocessor_get_factory' ) ) {
return false;
}

// good to go
return true;
Expand Down
13 changes: 12 additions & 1 deletion includes/class-cvc-api.php
Expand Up @@ -52,6 +52,7 @@ public function call_values( $entity, $action, $params ) {

/**
* Shorthand for getting data processor info
*
* @param $id
*
* @return mixed
Expand All @@ -60,14 +61,24 @@ public function get_data_processor_by_id( $id ) {
if ( empty( $id ) ) {
throw new Exception( 'No id passed in.' );
}
$dp = $this->call_values( 'DataProcessorOutput', 'get', [
$dp = $this->call_values( 'DataProcessorOutput', 'get', [
'sequential' => 1,
'type' => "api",
'data_processor_id' => $id
] );

return array_shift( $dp );
}

public function is_dp_enabled() {
static $entities = null;
if ( ! $entities ) {
$entities = $this->call_values( 'Entity', 'get', [] );
}

return in_array( 'DataProcessor', $entities );
}

}

}
Expand Down
141 changes: 85 additions & 56 deletions settings/class-cvc-settings-filter.php
Expand Up @@ -32,7 +32,12 @@ public function register_hooks() {
// add contact post type
add_filter( PT_CV_PREFIX_ . 'post_types_list', [ $this, 'filter_post_types_list' ] );
// contact filters
add_filter( PT_CV_PREFIX_ . 'filter_settings_final', [ $this, 'final_filter_settings' ] );
if ( has_filter( PT_CV_PREFIX_ . 'filter_settings_final' ) ) {
// our fork's filter
add_filter( PT_CV_PREFIX_ . 'filter_settings_final', [ $this, 'final_filter_settings' ] );
} else {
add_filter( PT_CV_PREFIX_ . 'custom_filters', [ $this, 'custom_filters' ] );
}
}

/**
Expand All @@ -51,6 +56,23 @@ public function filter_post_types_list( $types ) {
return $types;
}

/**
* This filter can only add one setting block
* and cannot modify other blocks dependencies
* @param $options
*
* @return array
*/
public function custom_filters( $options ) {
if ( !empty( $options ) ) {
// Some other plugins using this filter
return $options;
}
$options = $this->get_civicrm_settings_block();

return $options;
}

public function final_filter_settings( $options ) {
$all_post_types_but_civicrm = array_diff( array_keys( PT_CV_Values::post_types() ), [ 'civicrm' ] );

Expand All @@ -61,69 +83,76 @@ function ( $options, $group ) use ( $all_post_types_but_civicrm ) {
}
$options[] = $group;
if ( $group['label']['text'] == 'Content type' ) {
$options[] = [
'label' => [ 'text' => __( 'CiviCRM filter', 'content-views-civicrm' ) ],
'extra_setting' => [
'params' => [
'wrap-class' => PT_CV_Html::html_panel_group_class(),
'wrap-id' => PT_CV_Html::html_panel_group_id( PT_CV_Functions::string_random() )
]
],
'dependence' => [ 'content-type', [ 'civicrm' ] ],
'params' => [
[
'type' => 'group',
'params' => [
// data processor
[
'label' => [ 'text' => __( 'Data processor', 'content-views-civicrm' ) ],
'params' => [
[
'type' => 'select',
'name' => 'data_processor_id',
'options' => $this->get_data_processor(),
'class' => 'select2',
'std' => '',
'desc' => __( 'Select the data you want to list here.', 'content-views-civicrm' )
]
]
],
// sort
[
'label' => [ 'text' => __( 'Sorting', 'content-views-civicrm' ) ],
'params' => [
[
'type' => 'text',
'name' => 'civicrm_sort',
'std' => '',
'desc' => __( 'Set the sorting order.', 'content-views-civicrm' )
]
]
],
// limit
[
'label' => [ 'text' => __( 'Limit', 'content-views-civicrm' ) ],
'params' => [
[
'type' => 'number',
'name' => 'civicrm_limit',
'std' => '',
'desc' => __( 'Set the limit of the result.', 'content-views-civicrm' )
]
]
],
]
]
]
];
$options[] = $this->get_civicrm_settings_block();
}

return $options;
},
[] );
}

private function get_civicrm_settings_block() {
return [
'label' => [ 'text' => __( 'CiviCRM filter', 'content-views-civicrm' ) ],
'extra_setting' => [
'params' => [
'wrap-class' => PT_CV_Html::html_panel_group_class(),
'wrap-id' => PT_CV_Html::html_panel_group_id( PT_CV_Functions::string_random() )
]
],
'dependence' => [ 'content-type', [ 'civicrm' ] ],
'params' => [
[
'type' => 'group',
'params' => [
// data processor
[
'label' => [ 'text' => __( 'Data processor', 'content-views-civicrm' ) ],
'params' => [
[
'type' => 'select',
'name' => 'data_processor_id',
'options' => $this->get_data_processor(),
'class' => 'select2',
'std' => '',
'desc' => __( 'Select the data you want to list here.', 'content-views-civicrm' )
]
]
],
// sort
[
'label' => [ 'text' => __( 'Sorting', 'content-views-civicrm' ) ],
'params' => [
[
'type' => 'text',
'name' => 'civicrm_sort',
'std' => '',
'desc' => __( 'Set the sorting order.', 'content-views-civicrm' )
]
]
],
// limit
[
'label' => [ 'text' => __( 'Limit', 'content-views-civicrm' ) ],
'params' => [
[
'type' => 'number',
'name' => 'civicrm_limit',
'std' => '',
'desc' => __( 'Set the limit of the result.', 'content-views-civicrm' )
]
]
],
]
]
]
];
}

private function get_data_processor() {
if ( ! $this->cvc->api->is_dp_enabled() ) {
return [];
}
$result = $this->cvc->api->call_values( 'DataProcessorOutput', 'get', [
'sequential' => 1,
'type' => "api",
Expand Down

0 comments on commit 62bfbc5

Please sign in to comment.