Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Tusko committed Jan 11, 2020
0 parents commit 4636c53
Show file tree
Hide file tree
Showing 15 changed files with 1,471 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# This file is for unifying the coding style for different editors and IDEs
# editorconfig.org

# WordPress Coding Standards
# https://make.wordpress.org/core/handbook/coding-standards/

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = tab

[{.jshintrc,*.json,*.yml}]
indent_style = space
indent_size = 2

[{*.txt,wp-config-sample.php}]
end_of_line = crlf
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Icon?
.DS_Store
675 changes: 675 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

122 changes: 122 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# ACF CPT Options Pages

Small addon for ACF Options. Adds ACF location for each custom post type.

**New feature** in the major version 2!

Now you can activate/deactivate CPTs and create custom options pages for each CPT.
By default, options pages are activated for all custom post types.

## Installation

+ Upload 'acf-cpt-options-pages' to the `/wp-content/plugins/` directory.
+ Activate the plugin through the`Plugins` menu in WordPress.
+ Go to `Custom fields` submenu `CPT Options page` and activate CPTs what you need or create subpages for that.
+ Create your Custom Field Group, set location rule `Options Page` and choose your CPT options page or subpage.
+ Read the documentation to display your data.

![Preview](http://arsmoon.stream/cpt-acf-options.png)

## Usage

The default functions of [ACF plugin](http://www.advancedcustomfields.com/ "Advanced Custom Fields") (`get_field, the_field, etc.`) can be used to load values from a CPT Options Pages, but second parameter is required to target the CPT options.

This is similar to passing through a `$post_id` parameter to target a specific post object.

The `$post_id` parameter needed is a string containing the `cpt_` and CPT name in the following format; `"cpt_{CPT_NAME}"` and for subpages you can copy generated ID while creating subpages.

## Examples

>In examples `projects` is a Custom Post Type name.
So, let's go!

#### Display a field
```php
<p><?php the_field('field_name', 'cpt_projects'); ?></p>
```

and the subpage field

```php
<p><?php the_field('field_name', 'cpt_projects_testpage'); ?></p>
```

#### Retrieve a field
```php
<?php
$field = get_field('field_name', 'cpt_projects');
// do something with $field
?>
```
#### Display a sub field
```php
<?php if( have_rows('repeater_name', 'cpt_projects') ): ?>
<ul>
<?php while( have_rows('repeater_name', 'cpt_projects') ): the_row(); ?>
<li><?php the_sub_field('the_title'); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
```
#### Display with shortcode

```
[acf field="field_name" post_id="cpt_projects"]
```

> Please read documentation about [shortcodes with ACF](http://www.advancedcustomfields.com/resources/shortcode/ "ACF Shortcode")
## Customization

Add in your `functions.php`

```
function cpt_projects_customize($cptmenu) {
$cptmenu['page_title'] = 'Dev Custom title';
$cptmenu['menu_title'] = 'Dev Custom title';
return $cptmenu;
}
add_filter('cpt_projects_acf_page_args', 'cpt_projects_customize');
```

Don't forget to replace `cpt_projects_` to your custom post type name :)
It works only for first level options pages, not for subpages.

## License

Copyright (c) 2020, [Tusko Trush](https://frontend.im/?github "Front-End Developer")

> See LICENSE for more info.
## Requirements

You must buy ACF PRO or ACF Options Page Addon.


## Translation

**qTranslate-XT**

This plugin is compatible and has included [ACF qTranslate](https://uk.wordpress.org/plugins/acf-qtranslate/ "ACF qTranslate").
Just enjoy!

**qTranslate-X**

> If you are using qTranslate-X, you must install [ACF qTranslate](https://uk.wordpress.org/plugins/acf-qtranslate/ "ACF qTranslate").
**WPML/Polylang**

> If you are using WPML or Polylang, you must add constant `ICL_LANGUAGE_CODE` to `post_id`,
for example: `get_field('archive_title', 'cpt_projects_' . ICL_LANGUAGE_CODE)`.

## Contributors

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
[<img src="https://avatars.githubusercontent.com/u/2039259" width="100px;"/><br /><sub>Tusko Trush</sub>](https://github.com/tusko?github)<br /> Chief :D | [<img src="https://avatars.githubusercontent.com/u/1512067" width="100px;"/><br /><sub>Máté Farkas</sub>](https://github.com/wolfika)<br /> i18n Support | [<img src="https://avatars.githubusercontent.com/u/5536354" width="100px;"/><br /><sub>Sauli Rajala</sub>](https://github.com/saulirajala)<br /> Customizations
--- | --- | ---
<!-- ALL-CONTRIBUTORS-LIST:END -->

---------------
If you have any questions on this please post an issue/question: https://github.com/Tusko/ACF-CPT-Options-Pages/issues
52 changes: 52 additions & 0 deletions assets/acf-cpt-logic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
jQuery(document).ready(function ($) {
var $container = $('.acf-cpt-register'),
subpage_translate = $container.data('subpage'),
checkboxes = $('input[type="checkbox"]', $container),
plusHTML = '<span class="dashicons dashicons-plus-alt"></span>',
trashHTML = '<span class="dashicons dashicons-trash"></span>';

checkboxes.change(function(){
var _t = $(this);
if( _t.prop('checked') ) {
_t.parents('h4').append(plusHTML);
} else {
_t.parents('h4').find('.dashicons').remove();
}
});

$(this).on('click', '.dashicons-plus-alt', function () {
var _n = $(this).prev().find('input').val();
$(this).parents('.cpt-row').append(
'<p class="sub-line"><span>'+ subpage_translate +'</span>' +
'<input type="text" name="cpts['+ _n +'][]">' +
trashHTML +
'</p>'
);
});

$(this).on('click', '.dashicons-trash', function () {
$(this).parent().remove();
});

$('.sub-line input[type="text"]').on('change',function(){
var arr = [],
$siblings = $(this).siblings();
$.each($siblings, function (i, key) {
arr.push($(key).val());
});
if ($.inArray($(this).val(), arr) !== -1)
{
alert("duplicate has been found");
}
});

var timeOutSelect;
$("input[type=text][readonly]").focus(function() {
var save_this = $(this);
clearTimeout(timeOutSelect);
timeOutSelect = window.setTimeout (function(){
save_this.select();
}, 100);
});

});
113 changes: 113 additions & 0 deletions class.acf-cpt-options-pages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
class ACFCPT_OptionsPages {

function __construct() {
//Check if installed ACF Options Page
if ( function_exists( 'acf_add_options_page' ) ) {
$this->init();
} else {
add_action( 'admin_notices', array( $this, 'admin_error_notice' ) );
}
}

public function init(){
add_filter( 'plugin_action_links_' . CPT_ACF_PLUGIN, array($this, 'plugin_action_links') );
add_action( 'init', array( $this, 'load_plugin_textdomain' ), 100, 3 );
add_action( 'admin_menu', array($this, 'options_page_render') );
$this->setup_cpt_options_pages();
}

public function setup_cpt_options_pages() {
$registered = $this->get_registered_cpts();
if(empty($registered)) {
$registered = $this->get_custom_post_types();
}

unset($registered["0"]);

foreach($registered as $k => $v) {
if( ! is_array($v) && post_type_exists($v)) {
$this->register_post_type_options_page($v, $v);
} else {
foreach($v as $sub) {
$this->register_post_type_options_page($sub, $k);
}
}
}
}

public function get_registered_cpts(){
$get_cpts_enabled = get_option('acf-cpt-archives');
$cpts_enabled = unserialize($get_cpts_enabled);

return $cpts_enabled;
}

public function get_custom_post_types() {
return get_post_types(array(
'_builtin' => false,
'has_archive' => true
));
}

public function register_post_type_options_page($name, $cpt) {
$cpt_obj = get_post_type_object( $cpt );
$slug = ($name !== $cpt ? '_' . strtolower(preg_replace('/[^a-zA-Z0-9_]/', '_', $name)) : '');
$cpt_id = 'cpt_' . $cpt . $slug;

$human_page_name = sprintf( __( '%s Options', CPT_ACF_DOMAIN ), ucfirst( ($name ? str_replace('_', ' ', $name) : $cpt_obj->labels->singular_name) ) );

if( defined('ICL_LANGUAGE_CODE') ) {
$cpt_id = $cpt_id . '_' . ICL_LANGUAGE_CODE;
}

$cpt_acf_page = array(
'parent_slug' => 'edit.php?post_type=' . $cpt,
'capability' => 'edit_posts',
'post_id' => $cpt_id,
'position' => false,
'icon_url' => false,
'redirect' => false
);

$cpt_acf_custom = array(
'page_title' => $human_page_name,
'menu_title' => $human_page_name,
'menu_slug' => $slug .'-'. $cpt . '-options',
'capability' => 'edit_posts',
);

$cpt_acf_custom = apply_filters( "{$cpt_id}_acf_page_args", $cpt_acf_custom );
$cpt_acf_page = array_merge($cpt_acf_page, $cpt_acf_custom);

acf_add_options_page( $cpt_acf_page );
}

public function admin_error_notice() {
echo '<div class="update-nag"><p>' . __('Admin Error Notice', CPT_ACF_DOMAIN) . '</p></div>';
}

public function load_plugin_textdomain() {
load_plugin_textdomain(CPT_ACF_DOMAIN, false, CPT_ACF_DOMAIN . '/languages');
}

public function plugin_action_links($links) {
$links[] = '<a href="' . admin_url('edit.php?post_type=acf-field-group&page=' . CPT_ACF_DOMAIN . '-settings') . '" target="_blank">' . __('Settings', CPT_ACF_DOMAIN) . '</a>';
$links[] = '<a href="https://github.com/Tusko/ACF-CPT-Options-Pages#usage" target="_blank">' . __('Documentation', CPT_ACF_DOMAIN) . '</a>';
$links[] = '<a href="https://arsmoon.com/" target="_blank">Supported by Arsmoon</a>';
$links[] = '<a href="https://donatua.com/@tusko" target="_blank">Donate</a>';

return $links;
}

public function options_page_render() {
$hook = add_submenu_page('edit.php?post_type=acf-field-group', __('CPT Options Pages', CPT_ACF_DOMAIN), __('CPT Options Pages', CPT_ACF_DOMAIN), 'manage_options', CPT_ACF_DOMAIN . '-settings', array($this, 'options_page_tpl'));
add_action("load-$hook", function() {
wp_enqueue_script('acf_cpt_logic', CPT_ACF_PLUGIN_DIR . 'assets/acf-cpt-logic.js');
});
}

public function options_page_tpl() {
include 'tpl-settings-page.php';
}
}
46 changes: 46 additions & 0 deletions cpt-acf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Plugin Name: Advanced Custom Fields : CPT Options Pages
* Plugin URI: https://wordpress.org/plugins/acf-cpt-options-pages/
* Description: Enables ACF options pages for a post type archive.
* Author: Tusko Trush
* Author URI: https://frontend.im/
* Version: 2.0.5
* License: GPL v3
* Text Domain: acf-cpt-options-pages
* Domain Path: /languages
*
* CPT ACF Options Pages
* Copyright (C) 2020, Tusko Trush - [email protected]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**/

defined( 'ABSPATH' ) or die( 'No script kiddies please!' );

if(!defined('CPT_ACF_PLUGIN')) {
define( 'CPT_ACF_PLUGIN_DIR', plugin_dir_url( __FILE__ ));
define( 'CPT_ACF_PLUGIN', plugin_basename(__FILE__));
define( 'CPT_ACF_DOMAIN', dirname( plugin_basename( __FILE__ ) ));
}

if(!class_exists('ACFCPT_OptionsPages')) {
include 'class.acf-cpt-options-pages.php';

function acf_cpt_admin_init(){
$ACFCPT_OptionsPages = new ACFCPT_OptionsPages;
}

add_action ('init', 'acf_cpt_admin_init', 99, 3);
}
Binary file added languages/acf-cpt-options-pages-en_US.mo
Binary file not shown.
Loading

0 comments on commit 4636c53

Please sign in to comment.