Skip to content

Commit

Permalink
Merge pull request #18 from harryfinn/refactor-cmb2-fields
Browse files Browse the repository at this point in the history
Refactor CMB2Fields class
  • Loading branch information
Harry Finn committed Jun 9, 2017
2 parents cc81d3c + 0a05b6c commit 76e9607
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 101 deletions.
81 changes: 49 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ If you do not have a version of `php` installed or is below version `7.x.x`,
please upgrade it. `php 7` is recommended due to the performance improvements,
view an upgrade guide [here](https://developerjack.com/blog/2015/12/11/Installing-PHP7-with-homebrew/)

### Installing WP-CLI

The WordPress installation and general development is made easier through the
use of WP-CLI tool. Follow the commands below to install it prior to setting
up the project.

```TXT
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
```

Finally, to check the WP-CLI is running correctly, run `wp --info` which should
present you with the current PHP & WP-CLI versions installed if successful.

## To create a new WordPress project:

```TXT
Expand All @@ -44,6 +59,32 @@ skeleton.

You can now branch from master to make changes in this repo.

## Install/Update CMB2 within project

This project uses the [CMB2](https://github.com/WebDevStudios/CMB2) library
to help generate and manage custom metaboxes within the WP admin. It is
included within this project via submodule.

Should you ever need add CMB2 library to the project (this is not required when
using this repo as a starter framework), run the following command within the
relevant theme directory you wish to add the CMB2 library into:

`git submodule add [email protected]:WebDevStudios/CMB2.git`

When setting up the project for the first time (or installing CMB2 for the
first time), checkout the develop branch and run the following command
`git submodule update --init`. This will pull the CMB2 library into the
correct folder within the project's theme.

If you need to update the CMB2 library (following and update to the library
repo), run the following command: `git submodule update`.

NB: The CMB2 files are not committed directly within this repo as there should
be no reason to change the library code unless updating the CMB2 library after
a new release.

## Starting your new themes

To create a new WordPress theme repo from this skeleton run the following
command to delete the current git files.

Expand Down Expand Up @@ -101,47 +142,23 @@ the master branch then run the `pre-deploy` command - for Live/Production
- `yarn caniuse` - will check the compiled `app.css` file against the
`CanIUse` API - For informational purposes only

When adding node packages (via [Yarn](https://yarnpkg.com/en/) to this repo,
the `--dev` option should be used for any packages that are required to run
the development environment, prior to the build/compilation of the app.
When adding node packages (via [Yarn](https://yarnpkg.com/en/) to this repo,
the `--dev` option should be used for any packages that are required to run
the development environment, prior to the build/compilation of the app.

Otherwise, if a package affects the way in which the code is written, it
should sit under the `dependencies` section. For example, the
`auto-reload-brunch` offers local reloading of pages to show instant styling
and js changes, which would not be suitable for a live server environment,
Otherwise, if a package affects the way in which the code is written, it
should sit under the `dependencies` section. For example, the
`auto-reload-brunch` offers local reloading of pages to show instant styling
and js changes, which would not be suitable for a live server environment,
therefore should have the `--dev` flag used to add it.

## Install/Update CMB2 within project

This project uses the [CMB2](https://github.com/WebDevStudios/CMB2) library
to help generate and manage custom metaboxes within the WP admin. It is
included within this project via submodule.

Should you ever need add CMB2 library to the project (this is not required when
using this repo as a starter framework), run the following command within the
relevant theme directory you wish to add the CMB2 library into:

`git submodule add [email protected]:WebDevStudios/CMB2.git`

When setting up the project for the first time (or installing CMB2 for the
first time), checkout the develop branch and run the following command
`git submodule update --init`. This will pull the CMB2 library into the
correct folder within the project's theme.

If you need to update the CMB2 library (following and update to the library
repo), run the following command: `git submodule update`.

NB: The CMB2 files are not committed directly within this repo as there should
be no reason to change the library code unless updating the CMB2 library after
a new release.

## Testing

The server will allow for devices connected on the same wifi to access the
site. On your device go to the IP address + `xip.io:8080`.
To find your IP address hold `ALT` and click on the wifi icon in your toolbar.

To ensure local assets are correctly parsed when testing locally, ensure the
To ensure local assets are correctly parsed when testing locally, ensure the
`BRUNCH_LOCAL_ASSETS` constant is set to true.

## Additional Skeleton file notes
Expand Down
179 changes: 110 additions & 69 deletions includes/class.cmb2fields.php
Original file line number Diff line number Diff line change
@@ -1,59 +1,37 @@
<?php

class CMB2Fields {
public $post_id,
$field_prefix,
$img_width,
public $img_width,
$img_height,
$post_id,
$render_args;

public $cmb_prefix = CMB2_PREFIX;

public $field_defaults = [
'multi' => false,
'multi_val' => null,
'is_single' => true,
'placeholder' => false
];
private $_initialised_post_id;

public function __construct($post_id) {
$this->post_id = $post_id;
$this->_initialised_post_id = $post_id;
}

public function set_image_size($width, $height) {
$this->img_width = $width;
$this->img_height = $height;
}

public function field($field_name, array $field_args = null) {
$field_args = array_merge(
$this->field_defaults,
(!empty($field_args) ? $field_args : [])
);

$field_value = $this->empty_field_check(
$this->field_prefix . $field_name,
$field_args['multi'],
$field_args['multi_val'],
$field_args['is_single']
);

if(strpos($field_name, 'image') !== false && empty($field_value) &&
$field_args['placeholder'] === true) {
if(!empty($field_args['image'])) {
return $this->get_placeholder(
$field_args['image']['w'],
$field_args['image']['h']
);
} else {
return $this->get_placeholder($this->img_width, $this->img_height);
}
} else {
return $field_value;
public function field($field_name, array $field_args = []) {
$field_value = CMB2Field::fetch($this->post_id, $field_name, $field_args);

if(strpos($field_name, 'image') !== false && empty($field_value)) {
return $this->image_field_placeholder(
$field_name,
$field_args
);
}

return $field_value;
}

public function get_featured_image($size = 'full') {
public function get_featured_image($size = 'full', $placeholder = false) {
if(has_post_thumbnail($this->post_id)) {
$featured_image = wp_get_attachment_image_src(
get_post_thumbnail_id($this->post_id),
Expand All @@ -63,17 +41,29 @@ public function get_featured_image($size = 'full') {
return $featured_image[0];
}

if($this->field_defaults['placeholder']) {
return $this->generate_placeholder_from($size);
}
if($placeholder) return $this->generate_placeholder_from($size);

return false;
}

public function render($template_name, array $render_args = null) {
public function render($template_name, array $render_args = [], $echo = true) {
$this->render_args = $render_args;
$template_filename = $this->template_filename($template_name);

extract($render_args);
ob_start();

if($this->template_exists($template_filename)) {
include(locate_template($template_filename));

$this->restore_previous_post_id();
} else {
echo "No template found for: $template_filename ($template_name)";
}

include(locate_template($template_name));
if(!$echo) return ob_get_clean();

echo ob_get_clean();
}

public function template_exists($template) {
Expand All @@ -86,48 +76,99 @@ public function format_content($content) {
return apply_filters('the_content', $content);
}

protected function get_cmb2_field($field, $is_single = true) {
return get_post_meta($this->post_id, $field, $is_single);
}

protected function empty_field_check($metabox_field, $multi = null, $multi_val = null, $is_single = true) {
if($multi === true) {
return !empty($this->get_cmb2_field(
$this->cmb_prefix . $metabox_field,
$is_single
)) ?
($this->get_cmb2_field(
$this->cmb_prefix . $metabox_field,
$is_single
) == $multi_val ? true : false) :
false;
} else {
return !empty($this->get_cmb2_field(
$this->cmb_prefix . $metabox_field,
$is_single
)) ?
$this->get_cmb2_field(
$this->cmb_prefix . $metabox_field,
$is_single
) :
'';
}
public function set_new_post_id(int $new_post_id) {
$this->post_id = $new_post_id;
}

protected function get_placeholder($width, $height) {
return 'http://placehold.it/' . $width . 'x' . $height;
}

protected function get_post_object($post_id = null) {
return get_post(!empty($post_id) ? $post_id : $this->post_id);
$_post_id = !empty($post_id) ? $post_id : $this->post_id;

return get_post($_post_id);
}

protected function generate_placeholder_from($size) {
global $_wp_additional_image_sizes;

$placeholder_size = $size == 'full' ? 'post-thumbnail' : $size;
$width = $_wp_additional_image_sizes[$placeholder_size]['width'];
$height = $_wp_additional_image_sizes[$placeholder_size]['height'];

return $this->get_placeholder($width, $height);
}

protected function restore_previous_post_id() {
$this->post_id = $this->_initialised_post_id;
}

protected function template_filename($template_name) {
return strpos($template_name, '-tpl.php') === false ?
$template_name . '-tpl.php' :
$template_name;
}

protected function image_field_placeholder($field_args) {
if($field_args['placeholder'] === true) {
if(!empty($field_args['image'])) {
return $this->get_placeholder(
$field_args['image']['w'],
$field_args['image']['h']
);
}

return $this->get_placeholder($this->img_width, $this->img_height);
}

return false;
}
}

class CMB2Field {
public static $cmb_prefix = CMB2_PREFIX,
$field,
$field_args = [
'is_single' => true,
'is_tax_term' => false,
'placeholder' => false
],
$post_id;

public static function fetch(int $post_id, $field, array $field_args = []) {
self::$field = $field;
self::$field_args = array_merge(self::$field_args, $field_args);
self::$post_id = $post_id;

if(self::$field_args['is_tax_term']) {
return self::fetch_tax_term_meta();
}

return self::fetch_post_meta();
}

private static function fetch_post_meta() {
$value = get_post_meta(
self::$post_id,
self::$cmb_prefix . self::$field,
self::$field_args['is_single']
);

return self::empty_field_check($value);
}

private static function fetch_tax_term_meta() {
$value = get_term_meta(
self::$post_id,
self::$field,
self::$field_args['is_single']
);

return self::empty_field_check($value);
}

private static function empty_field_check($value) {
return !empty($value) ? $value : false;
}
}

0 comments on commit 76e9607

Please sign in to comment.