~askonomm/framework

35bd59cac80a57e1328612f0e94e1bcb55825085 — Asko N├Ámm 6 months ago 75e2b27
2.6
3 files changed, 55 insertions(+), 80 deletions(-)

M README.md
M framework.php
M frameworkwp.php
M README.md => README.md +1 -61
@@ 605,64 605,4 @@ framework.ajaxAction('send_message', {
})
```

Keep in mind that this expects you to have set up an Ajax Action in the back-end. You can do that with the `ajaxAction` method in PHP, like stated in above documentation. Oh and, all ajaxActions are HTTP POST requests.

## Changelog

### 2.5

- Cleaned up a bunch of code, removed snake_case mirrors, added types. Now only compatible with PHP 7.4.
- Added `input` helper method, for retrieving things in `$_REQUEST`.

### 2.4

- Added `isPostType` and `isPostTypeArchive` conditional callbacks.

### 2.3.1 

- Fixed an issue that caused `isPage` not to work as expected.

### 2.3

- Added `isTax` conditional callback. 
- Added `registerTaxonomy` helper method.

### 2.2

- All the methods now are in camelCase, so it's recommended to use them instead as the snake_case methods will be deprecated in a future release.
- Added new `paged` method for returning the current pagination number.
- The `query` method now also returns the `max_num_pages` as how many pages of this type of content is via the `{{pages}}` variable.
- Added new `pagination` templating helper for displaying pagination in the templates.
- Added new `registerPostType` method for easy registering of custom post types.

### 2.1

- Added `current` method for returning the currently in scope queried object.
- No longer having pre-defined partials for use in templates. Register your partials with `register_template_partials`.
- You can now register your own template helpers by calling `register_template_helpers`.
- Conditional callbacks `is_category` and `is_tag` now expect two arguments, first is $id and the second is callback.
- The first parameter of conditional callbacks (if there are two) expects an asterisk `*` instead of `any`, to match for anything now.
- Added `is_user_logged_in`, `is_author`, `is_search` and `is_404` conditional callbacks.
- Renamed `wp_nav_menu` Handlebars helper to `menu`.
- Renamed `bloginfo` Handlebars helper to `info`.
- Renamed `post_meta` Handlebars helper to `meta`.
- Automatic feed links for posts and pages is now loaded with sensible defaults when calling `init`.

### 2.0.1

- Removed the `{{get_the_date}}` helper in favor of having `{{published_at}}` available with the `query` itself, because `get_the_date` couldn't get the scope of the post and thus displayd the wrong date.

### 2.0

- Framework is now a WordPress plugin, so to install it simply upload the `.zip` file from [releases](https://github.com/digital-baboon/framework/releases) to the admin panel.
- No longer using static methods, so you must first initialize the class with `new` and set it to a variable.
- Added conditional callbacks.
- Added querying content.
- Added templating.

### 1.3

- Added `script` method to the PHP lib for easy adding of scripts.
- Added `style` method to the PHP lib for easy adding of styles.
- Created the JavaScript lib with ability to create Ajax Actions.
- Loading the JavaScript with sensible defaults via `init` in PHP lib.
\ No newline at end of file
Keep in mind that this expects you to have set up an Ajax Action in the back-end. You can do that with the `ajaxAction` method in PHP, like stated in above documentation. Oh and, all ajaxActions are HTTP POST requests.
\ No newline at end of file

M framework.php => framework.php +48 -17
@@ 14,6 14,7 @@ class Framework
{
	private array $templatePartials = [];
	private array $templateHelpers = [];
	private array $templateData = [];

	/**
	 * E-mail sending helper.


@@ 22,36 23,25 @@ class Framework
	 * occurrences of {key} with a key-value set in `data`.
	 * and sends an email to `to' with a subject from `subject`.
	 *
	 * @param string $template
	 * @param array $data Configuration.
	 *
	 * @return void
	 */
	public function sendMessage(array $data): void
	public function sendEmail(string $template, array $data): void
	{
		if(empty($data['to']) || empty($data['subject'])) return;

		// Get to
		$to = $data['to'];

		// Get subject
		$subject = $data['subject'];

		// Get body
		$body = $data['body'];

		// Set headers
		$headers = ['Content-Type: text/html; charset=UTF-8'];

		// Parse body and set data
		if (!empty($data['data']) && is_array($data['data'])) {

			foreach ($data['data'] as $key => $value) {

				$body = str_replace('{' . $key . '}', $value, $body);

			}

		}

		wp_mail($to, $subject, $body, $headers);
		wp_mail($to, $subject, $this->emailTemplate($template, $data), $headers);
	}

	/**


@@ 799,6 789,16 @@ class Framework
	}

	/**
	 * Sets template data for use within all templates.
	 *
	 * @param array $data
	 */
	public function setTemplateData(array $data = []): void
	{
		$this->templateData = $data;
	}

	/**
	 * Get template partials.
	 *
	 * Get all template partials.


@@ 1069,6 1069,7 @@ class Framework
			$template = file_get_contents(get_template_directory() . '/templates/' . $name . '.hbs');

			$phpStr = LightnCandy\LightnCandy::compile($template, [
				'flags' => LightnCandy\LightnCandy::FLAG_ERROR_EXCEPTION,
				'partials' => $this->getTemplatePartials(),
				'helpers' => $this->getTemplateHelpers(),
			]);


@@ 1077,7 1078,37 @@ class Framework

			if ($renderer instanceof Closure) {

				echo $renderer(array_merge($data, $this->getTemplateDefaultData()));
				echo $renderer(array_merge($data, $this->getTemplateDefaultData(), $this->templateData));

			}

			die();

		}
	}

	/**
	 * Returns the template code for an email.
	 *
	 * @param string $name
	 * @param array $data
	 *
	 * @return callable
	 */
	public function emailTemplate(string $name, array $data = []): string
	{
		if (file_exists(get_template_directory() . '/templates/emails/' . $name . '.hbs')) {

			$template = file_get_contents(get_template_directory() . '/templates/emails/' . $name . '.hbs');
			$phpStr = LightnCandy\LightnCandy::compile($template, [
				'flags' => LightnCandy\LightnCandy::FLAG_ERROR_EXCEPTION
			]);

			$renderer = $this->prepareTemplate($phpStr);

			if ($renderer instanceof Closure) {

				return $renderer(array_merge($data, $this->templateData));

			}


M frameworkwp.php => frameworkwp.php +6 -2
@@ 10,5 10,9 @@
 * License: MIT
 */

require __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/framework.php';
if(!version_compare(PHP_VERSION, '7.4.0', '<')) {

	require __DIR__ . '/vendor/autoload.php';
	require_once __DIR__ . '/framework.php';

}
\ No newline at end of file