Skip to content

Commit

Permalink
create post custom field, make poll post_type not public, filter poll…
Browse files Browse the repository at this point in the history
…s in post custom field
  • Loading branch information
romulodl committed Jun 13, 2018
1 parent a9b9017 commit 8235901
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 12 deletions.
89 changes: 80 additions & 9 deletions app/CustomFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,38 @@

class CustomFields {

public function register() {
public function register_poll_fields() {
if ( ! function_exists( 'acf_add_local_field_group' ) ) {
return;
}

$key = 'polls_answers';
$key = 'poll_';

\acf_add_local_field_group(
[
'key' => $key . '_group',
'title' => 'Answers',
'key' => $key . 'group',
'title' => 'Poll Information',
'fields' => [
[
'key' => $key,
'label' => 'Publish this post to Apple News',
'name' => $key,
'key' => $key . 'awswers',
'label' => 'Poll answer list',
'name' => $key . 'answers',
'type' => 'repeater',
'sub_fields' => [
[
'key' => $key . '_text',
'key' => $key . 'text',
'label' => 'Answers for the poll',
'name' => $key . '_text',
'name' => $key . 'text',
'type' => 'text',
],
],
],
[
'key' => $key . 'date_limit',
'label' => 'Date limit for this poll',
'name' => $key . 'date_limit',
'type' => 'date_time_picker',
],
],
'location' => [
[
Expand All @@ -43,4 +49,69 @@ public function register() {
]
);
}

public function register_article_fields( array $post_types ) {
if ( ! function_exists( 'acf_add_local_field_group' ) ) {
return;
}

$locations = $this->set_locations( $post_types );
$key = 'article_poll';

\acf_add_local_field_group(
[
'key' => $key . '_group',
'title' => 'Poll',
'fields' => [
[
'key' => $key,
'name' => $key,
'label' => 'Select a Poll',
'type' => 'post_object',
'post_type' => 'poll',
'placement' => 'left',
],
],
'location' => $locations,
'menu_order' => 3,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
]
);
}

public function load_filters() {
add_filter( 'acf/fields/post_object/query/name=article_poll', [ $this, 'query_polls' ] );
}

public function query_polls( $args ) {
$args['post_status'] = 'publish';
$args['meta_query'] = [
[
'key' => 'poll_date_limit',
'value' => current_time( 'mysql' ),
'compare' => '>',
'type' => 'DATE',
],
];

return $args;
}

private function set_locations( array $post_types ) {
$locations = [];
foreach ( $post_types as $post_type ) {
$locations[] = [
[
'param' => 'post_type',
'operator' => '==',
'value' => $post_type,
],
];
}

return $locations;
}
}
3 changes: 2 additions & 1 deletion app/PostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public function register() {

register_post_type(
'poll', [
'public' => true,
'public' => false,
'show_ui' => true,
'labels' => $labels,
'supports' => [
'title',
Expand Down
24 changes: 22 additions & 2 deletions tests/TestCustomFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,34 @@ protected function setUp() {
Monkey\setUp();
}

public function test_register() {
public function test_register_poll_fields() {
Functions\expect('acf_add_local_field_group')
->once()
->with(Mockery::any());
Patchwork\redefine('function_exists', Patchwork\always(true));

$obj = new CustomFields();
$obj->register();
$obj->register_poll_fields();
}

public function test_register_article_fields() {
Functions\expect('acf_add_local_field_group')
->once()
->with(Mockery::any());
Patchwork\redefine('function_exists', Patchwork\always(true));

$obj = new CustomFields();
$obj->register_article_fields([
'post',
'longform'
]);
}

public function test_load_filters() {
$obj = new CustomFields();
$obj->load_filters();

$this->assertTrue(has_filter('acf/fields/post_object/query/name=article_poll', [$obj, 'query_polls']));
}

protected function tearDown() {
Expand Down

0 comments on commit 8235901

Please sign in to comment.