Skip to content

Commit

Permalink
wp-plugin-boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
mcguffin committed Nov 20, 2017
1 parent bcb4a62 commit a8da94b
Show file tree
Hide file tree
Showing 16 changed files with 427 additions and 45 deletions.
3 changes: 3 additions & 0 deletions acf-quick-edit-fields/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions include/ACFQuickEdit/Admin/Admin.php
Expand Up @@ -139,8 +139,8 @@ function enqueue_edit_assets() {
function enqueue_post_assets() {
global $typenow;
if ( 'acf-field-group' === $typenow ) {
wp_enqueue_script( 'acf-qef-field-group', plugins_url( 'js/acf-qef-field-group.min.js', ACFQUICKEDIT_FILE ), array( 'acf-field-group' ) );
wp_enqueue_style( 'acf-qef-field-group', plugins_url( 'css/acf-qef-field-group.css', ACFQUICKEDIT_FILE ), array( 'acf-field-group' ) );
wp_enqueue_script( 'acf-qef-field-group', plugins_url( 'js/acf-qef-field-group.min.js', ACF_QUICK_EDIT_FILE ), array( 'acf-field-group' ) );
wp_enqueue_style( 'acf-qef-field-group', plugins_url( 'css/acf-qef-field-group.css', ACF_QUICK_EDIT_FILE ), array( 'acf-field-group' ) );
}
}

Expand Down
6 changes: 3 additions & 3 deletions include/ACFQuickEdit/Admin/EditFeature.php
Expand Up @@ -40,12 +40,12 @@ public function init_fields() {
wp_register_style('acf-timepicker', acf_get_dir('assets/inc/timepicker/jquery-ui-timepicker-addon.min.css') );


wp_register_style( 'acf-quickedit', plugins_url( 'css/acf-quickedit.css', ACFQUICKEDIT_FILE ) );
wp_register_style( 'acf-quickedit', plugins_url( 'css/acf-quickedit.css', ACF_QUICK_EDIT_FILE ) );

if ( $content_type == 'taxonomy' ) {
wp_register_script( 'acf-quickedit', plugins_url( 'js/acf-quickedit.min.js', ACFQUICKEDIT_FILE ), array( 'inline-edit-tax', 'acf-input' ), $core->get_version(), true );
wp_register_script( 'acf-quickedit', plugins_url( 'js/acf-quickedit.min.js', ACF_QUICK_EDIT_FILE ), array( 'inline-edit-tax', 'acf-input' ), $core->get_version(), true );
} else if ( $content_type == 'post' ) {
wp_register_script( 'acf-quickedit', plugins_url( 'js/acf-quickedit.min.js', ACFQUICKEDIT_FILE ), array( 'inline-edit-post', 'acf-input' ), $core->get_version(), true );
wp_register_script( 'acf-quickedit', plugins_url( 'js/acf-quickedit.min.js', ACF_QUICK_EDIT_FILE ), array( 'inline-edit-post', 'acf-input' ), $core->get_version(), true );
}

wp_enqueue_media();
Expand Down
16 changes: 8 additions & 8 deletions include/ACFQuickEdit/Admin/Feature.php
Expand Up @@ -114,16 +114,16 @@ public function init_acf_settings() {
* @return array
*/
protected function acf_get_fields( $field_group ) {
$acf_fields = acf_get_fields( $field_group );
$return_fields = array();

foreach ( $acf_fields as $field ) {
//
if ( $field['type'] === 'group' ) {
$return_fields = array_merge( $return_fields, $field['sub_fields'] );
} else {
$return_fields[] = $field;
if ( $acf_fields = acf_get_fields( $field_group ) ) {
foreach ( $acf_fields as $field ) {
if ( $field['type'] === 'group' ) {
$return_fields = array_merge( $return_fields, $field['sub_fields'] );
} else {
$return_fields[] = $field;
}
}

}
return $return_fields;

Expand Down
99 changes: 99 additions & 0 deletions include/ACFQuickEdit/Ajax/AjaxHandler.php
@@ -0,0 +1,99 @@
<?php

namespace ACFQuickEdit\Ajax;

if ( ! defined('ABSPATH') ) {
die('FU!');
}

use ACFQuickEdit\Core;

class AjaxHandler {

private $_action = null;

private $options = null;

private $_nonce = null;

/**
* @param string $action
* @param array $args
*/
public function __construct( $action, $args ) {

$this->_action = $action;

$defaults = array(
'public' => false,
'use_nonce' => true,
'capability' => 'manage_options',
'callback' => null,
);

$this->options = (object) wp_parse_args( $args, $defaults );

if ( $this->public ) {
$this->options->capability = false;
add_action( "wp_ajax_nopriv_{$this->action}", array( $this, 'ajax_callback' ) );
}

add_action( "wp_ajax_{$this->action}", array( $this, 'ajax_callback' ) );
}

public function __get( $prop ) {
if ( $prop === 'nonce' ) {
return $this->get_nonce();
} else if ( $prop === 'action' ) {
return $this->_action;
} else if ( isset( $this->options->$prop ) ) {
return $this->options->$prop;
}
}

private function get_nonce() {
if ( is_null( $this->_nonce ) ) {
$this->_nonce = wp_create_nonce( '_nonce_' . $this->action );
}
return $this->_nonce;
}

private function verify_nonce( $nonce ) {
return wp_verify_nonce( $nonce, '_nonce_' . $this->action );
}

public function ajax_callback() {
$params = wp_parse_args( $_POST, array(
'nonce' => false,
));

// check nonce
if ( $this->use_nonce && ( ! $params['nonce'] || ! $this->verify_nonce($_POST['nonce']) ) ) {
return false;
}
// check capability
if ( $this->capability !== false && ! current_user_can( $this->capability ) ) {
return false;
}

$response = array( 'success' => false );

if ( is_callable( $this->callback ) ) {
if ( $result = call_user_func( $this->callback, $params ) ) {
$response = $result;
};
}

echo json_encode( $response );

exit();
}

public function __destruct( ) {
if ( $this->public ) {
remove_action( "wp_ajax_nopriv_{$this->action}", array( $this, 'ajax_callback' ) );
}
remove_action( "wp_ajax_{$this->action}", array( $this, 'ajax_callback' ) );
}

}
22 changes: 13 additions & 9 deletions include/ACFQuickEdit/AutoUpdate/AutoUpdate.php
Expand Up @@ -2,6 +2,11 @@

namespace ACFQuickEdit\AutoUpdate;

if ( ! defined('ABSPATH') ) {
die('FU!');
}


use ACFQuickEdit\Core;

abstract class AutoUpdate extends Core\Singleton {
Expand All @@ -24,18 +29,18 @@ protected function __construct() {
*/
public function upgrade_completed( $wp_upgrader, $hook_extra ) {

$plugin = plugin_basename( ACFQUICKEDIT_FILE );
$plugin = plugin_basename( ACF_QUICK_EDIT_FILE );

if ( $hook_extra['action'] === 'update' && $hook_extra['type'] === 'plugin' && in_array( $plugin, $hook_extra['plugins'] ) ) {

$plugin_info = get_plugin_data( ACFQUICKEDIT_FILE );
$plugin_info = get_plugin_data( ACF_QUICK_EDIT_FILE );

$old_version = get_option( 'acf_quick_edit_version' );
$new_version = $plugin_info['Version'];

do_action( 'acf_quick_edit_upgraded', $new_version, $old_version );

update_option('acf_quick_edit_version', $plugin_info['Version'] );
update_option( 'acf_quick_edit_version', $plugin_info['Version'] );

}
}
Expand All @@ -45,7 +50,7 @@ public function upgrade_completed( $wp_upgrader, $hook_extra ) {
* @filter upgrader_source_selection
*/
public function source_selection( $source, $remote_source, $wp_upgrader, $hook_extra ) {
if ( isset( $hook_extra['plugin'] ) && $hook_extra['plugin'] === plugin_basename( ACFQUICKEDIT_FILE ) ) {
if ( isset( $hook_extra['plugin'] ) && $hook_extra['plugin'] === plugin_basename( ACF_QUICK_EDIT_FILE ) ) {
// $source: filepath
// $remote_source download dir
$source_dirname = pathinfo( $source, PATHINFO_FILENAME);
Expand All @@ -60,7 +65,6 @@ public function source_selection( $source, $remote_source, $wp_upgrader, $hook_e
}
return $source;
}

/**
* @action admin_init
*/
Expand Down Expand Up @@ -90,11 +94,11 @@ public function pre_set_transient( $transient ) {

// get own version
if ( $release_info = $this->get_release_info() ) {
$plugin = plugin_basename( ACFQUICKEDIT_FILE );
$slug = basename( ACFQUICKEDIT_DIRECTORY );
$plugin_info = get_plugin_data( ACFQUICKEDIT_FILE );
$plugin = plugin_basename( ACF_QUICK_EDIT_FILE );
$slug = basename(ACF_QUICK_EDIT_DIRECTORY);
$plugin_info = get_plugin_data( ACF_QUICK_EDIT_FILE );

if ( version_compare( $release_info['version'], $plugin_info['Version'], '>' ) ) {
if ( version_compare( $release_info['version'], $plugin_info['Version'] , '>' ) ) {
$transient->response[ $plugin ] = (object) array(
'id' => $release_info['id'],
'slug' => $slug,
Expand Down
8 changes: 7 additions & 1 deletion include/ACFQuickEdit/AutoUpdate/AutoUpdateGithub.php
Expand Up @@ -2,6 +2,12 @@

namespace ACFQuickEdit\AutoUpdate;

if ( ! defined('ABSPATH') ) {
die('FU!');
}

use ACFQuickEdit\Core;

class AutoUpdateGithub extends AutoUpdate {

private $github_repo = null;
Expand Down Expand Up @@ -42,7 +48,7 @@ public function get_release_info() {
private function get_github_repo() {
if ( is_null( $this->github_repo ) ) {
$this->github_repo = false;
$data = get_file_data( ACFQUICKEDIT_FILE, array('GithubRepo'=>'Github Repository') );
$data = get_file_data( ACF_QUICK_EDIT_FILE, array('GithubRepo'=>'Github Repository') );
if ( ! empty( $data['GithubRepo'] ) ) {
$this->github_repo = $data['GithubRepo'];
}
Expand Down
45 changes: 45 additions & 0 deletions include/ACFQuickEdit/Compat/Sample.php
@@ -0,0 +1,45 @@
<?php

namespace ACFQuickEdit\Compat;

if ( ! defined('ABSPATH') ) {
die('FU!');
}


use ACFQuickEdit\Core;


class Sample extends Core\PluginComponent {

protected function __construct() {
}

/**
* @inheritdoc
*/
public function activate(){

}

/**
* @inheritdoc
*/
public function deactivate(){

}

/**
* @inheritdoc
*/
public function uninstall() {
// remove content and settings
}

/**
* @inheritdoc
*/
public function upgrade( $new_version, $old_version ) {
}

}
6 changes: 3 additions & 3 deletions include/ACFQuickEdit/Core/Core.php
Expand Up @@ -7,7 +7,7 @@
if ( ! defined( 'ABSPATH' ) )
die('Nope.');

class Core extends Singleton {
class Core extends Plugin {

private $post_field_prefix = 'acf_qed_';

Expand All @@ -27,7 +27,7 @@ public function get_version() {
$version = null;
if ( ! $version = get_option('acf_quickedit_version') ) {
if ( function_exists('get_plugin_data') ) {
$plugin_data = get_plugin_data( ACFQUICKEDIT_FILE );
$plugin_data = get_plugin_data( ACF_QUICK_EDIT_FILE );
$version = $plugin_data['Version'];
}
}
Expand Down Expand Up @@ -59,7 +59,7 @@ public function init_compat() {
* @action plugins_loaded
*/
public function load_textdomain() {
load_plugin_textdomain( 'acf-quick-edit-fields' , false, ACFQUICKEDIT_DIRECTORY . '/languages/' );
load_plugin_textdomain( 'acf-quick-edit-fields' , false, ACF_QUICK_EDIT_DIRECTORY . '/languages/' );
}

/**
Expand Down

0 comments on commit a8da94b

Please sign in to comment.