Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev into release #19

Merged
merged 5 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/phpUnitTestsDev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ jobs:

- name: install phpunit-polyfills
run: npx wp-env run cli --env-cwd=wp-content/plugins/ai-entries composer require yoast/phpunit-polyfills --dev --with-all-dependencies

- name : install wp_mock
run: npx wp-env run cli --env-cwd=wp-content/plugins/ai-entries composer require 10up/wp_mock --dev --with-all-dependencies

- name: run phpUnit tests
run: npx wp-env run tests-cli --env-cwd=wp-content/plugins/ai-entries ./vendor/bin/phpunit ./tests/classes --testdox
run: npx wp-env run tests-cli --env-cwd=wp-content/plugins/ai-entries ./vendor/bin/phpunit ./tests/classes --bootstrap ./tests/bootstrap.php --testdox --colors
5 changes: 4 additions & 1 deletion .github/workflows/phpUnitTestsRelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ jobs:

- name: install phpunit-polyfills
run: npx wp-env run cli --env-cwd=wp-content/plugins/ai-entries composer require yoast/phpunit-polyfills --dev --with-all-dependencies

- name : install wp_mock
run: npx wp-env run cli --env-cwd=wp-content/plugins/ai-entries composer require 10up/wp_mock --dev --with-all-dependencies

- name: run phpUnit tests
run: npx wp-env run tests-cli --env-cwd=wp-content/plugins/ai-entries ./vendor/bin/phpunit ./tests/classes --testdox
run: npx wp-env run tests-cli --env-cwd=wp-content/plugins/ai-entries ./vendor/bin/phpunit ./tests/classes --bootstrap ./tests/bootstrap.php --testdox --colors
2 changes: 1 addition & 1 deletion ai-entries/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
=== AI Entries ===

Contributors: Julio Bermúdez
Tested up to: 6.5.5
Tested up to: 6.6
Stable tag: 1.0.3
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Expand Down
1 change: 0 additions & 1 deletion ai-entries/ai-entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

require_once plugin_dir_path(__FILE__) . 'includes/class-ai-entries.php';

register_activation_hook(__FILE__, ['AIEntries', 'activate']);
register_deactivation_hook(__FILE__, ['AIEntries', 'deactivate']);

AIEntries::instance();
48 changes: 29 additions & 19 deletions ai-entries/includes/class-ai-entries-api.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

class AIEntries_API {
class AIEntries_API
{

public static function call($question, $api_key, $category_name, $iterator = "") {
public static function call($question, $api_key, $category_name, $iterator = "")
{
// URL for the API call
$url = 'https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent?key=' . $api_key;

Expand Down Expand Up @@ -58,7 +60,8 @@ public static function call($question, $api_key, $category_name, $iterator = "")
return self::create_new_entry($article['title'], $article['content'], $category_name);
}

private static function create_new_entry($title, $content, $category_name) {
private static function create_new_entry($title, $content, $category_name)
{
if (current_user_can('publish_posts')) {
$category_id = get_term_by('name', $category_name, 'category');
if (!$category_id) {
Expand All @@ -85,13 +88,19 @@ private static function create_new_entry($title, $content, $category_name) {
} else {
$base64_image = self::generate_post_image_with_AI($title);
self::set_featured_image_from_base64($base64_image, $post_id);

wp_clear_scheduled_hook('AIEntries_daily_cron_job');

wp_schedule_event(strtotime('now') + (1 * 60 * 60) , 'hourly', 'AIEntries_daily_cron_job');

return get_post($post_id);
}
}
return new WP_Error('permission_error', 'You do not have permission to publish posts.');
}

private static function generate_post_image_with_AI($title) {
private static function generate_post_image_with_AI($title)
{
$base_url = 'https://api.stability.ai';
$url = "$base_url/v1/generation/stable-diffusion-v1-6/text-to-image";
$api_key_stable_diffusion = get_option('AIEntries_api_key_stable_diffusion', '');
Expand Down Expand Up @@ -124,49 +133,50 @@ private static function generate_post_image_with_AI($title) {
return $body_request['artifacts'][0]['base64'];
}

private static function set_featured_image_from_base64($base64_image, $post_id) {
private static function set_featured_image_from_base64($base64_image, $post_id)
{
if (!is_int($post_id)) {
return false;
}

// Inicializar WP_Filesystem
WP_Filesystem();

global $wp_filesystem;

$upload_dir = wp_upload_dir();
$file_path = $upload_dir['path'] . '/' . uniqid() . '.jpg';

// Usar WP_Filesystem para escribir el contenido en el archivo
if (!$wp_filesystem->put_contents($file_path, base64_decode($base64_image), FS_CHMOD_FILE)) {
return false;
}

$mime_type = mime_content_type($file_path);

if (strpos($mime_type, 'image') === false) {
return false;
}

$filetype = wp_check_filetype(basename($file_path), null);

$attachment = array(
'guid' => $upload_dir['url'] . '/' . basename($file_path),
'post_mime_type' => $filetype['type'],
'post_title' => sanitize_file_name(basename($file_path)),
'post_content' => '',
'post_status' => 'inherit'
'post_status' => 'inherit',
);

$attach_id = wp_insert_attachment($attachment, $file_path, $post_id);

require_once ABSPATH . 'wp-admin/includes/image.php';

$attach_data = wp_generate_attachment_metadata($attach_id, $file_path);
wp_update_attachment_metadata($attach_id, $attach_data);
set_post_thumbnail($post_id, $attach_id);

return true;
}

}
84 changes: 82 additions & 2 deletions ai-entries/includes/class-ai-entries-cron.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
<?php

class AIEntries_Cron {
class AIEntries_Cron
{
// Function to check and run the function every 5 minutes
public static function my_six_hour_function()
{

AIEntries_Cron::daily_task(); // Execute the function

}

// Function to check and run the function every 6 hours
public static function check_six_hour_function()
{
$last_executed_time = get_transient('last_six_hour_execution');

// Check if it's time to run the function (6 hours have passed)
if (!$last_executed_time || $last_executed_time < strtotime('-6 hours')) {
AIEntries_Cron::my_six_hour_function(); // Execute the function
set_transient('last_six_hour_execution', strtotime('now')); // Update the last execution time
}
}

public static function daily_task() {
public function daily_task()
{
$question = get_option('AIEntries_question', '');
$num_calls = get_option('AIEntries_num_calls', 1);
$api_key = get_option('AIEntries_api_key', '');
Expand All @@ -14,4 +35,63 @@ public static function daily_task() {
}
}
}

public static function show_all_cron_tasks()
{
// Obtener las tareas cron
$cron = _get_cron_array();

if (empty($cron)) {
return 'No tasks scheduled.';
}

$output = '<table border="1 | 0" style="text-align:center;width:100%">';
$output .= '<tr><th> <h2>Next Excecution</h2> </th><th> <h2>Hook Name</h2> </th><th> <h2>Function Name</h2> </th></tr>';

foreach ($cron as $timestamp => $cronhooks) {
foreach ((array) $cronhooks as $hook => $events) {
if ($hook == 'AIEntries_daily_cron_job') {
$callbacks = array();
foreach ((array) $events as $event) {
if (isset($GLOBALS['wp_filter'][$hook])) {
$callbacks[] = $GLOBALS['wp_filter'][$hook];
}
}
//print_r($callbacks);
foreach ($callbacks as $priority => $callback) {
foreach ($callback->callbacks as $function_data) {
foreach ($function_data as $function_parts) {
$output .= '<tr>';
$output .= '<td><p>' . esc_html(gmdate('Y-m-d H:i:s', $timestamp)) . '</p></td>';
$output .= '<td><p>' . esc_html($hook) . '</p></td>';
$output .= '<td><p>' . esc_html(strval($function_parts['function'][1] ? $function_parts['function'][1] : $function_parts['function'][0])) . '</p></td>';
$output .= '</tr>';
}

}
}
/* // Suponiendo que $array contiene el array proporcionado
foreach ($callbacks as $priority => $callbacks) {
foreach ($callbacks as $callback_name => $callback_info) {
$function_info = $callback_info['function'];

// Obtener el objeto/clase y el nombre de la función
$function_object = $function_info[0];
$function_name = $function_info[1];

echo 'Prioridad: ' . $priority . '<br>';
echo 'Función: ' . $function_object . '::' . $function_name . '<br>';

}
} */

}

}
}
if (!wp_next_scheduled('AIEntries_daily_cron_job')) {
echo "\n \n No excecutions scheduled for: " . esc_html($hook_name);
}
echo esc_html($output);
}
}
1 change: 1 addition & 0 deletions ai-entries/includes/class-ai-entries-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public static function add_menu_page()
'AIEntries-settings',
[self::class, 'settings_page']
);
return true;
}

public static function settings_page()
Expand Down
30 changes: 16 additions & 14 deletions ai-entries/includes/class-ai-entries.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
<?php

class AIEntries {
class AIEntries
{

private static $instance = null;

private function __construct() {
private function __construct()
{
$this->includes();
$this->init_hooks();
}

public static function createInstance() {
public static function createInstance()
{
return new self();
}

public static function instance() {
public static function instance()
{
if (is_null(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}

private function includes() {
private function includes()
{
require_once plugin_dir_path(__FILE__) . 'class-ai-entries-settings.php';
require_once plugin_dir_path(__FILE__) . 'class-ai-entries-api.php';
require_once plugin_dir_path(__FILE__) . 'class-ai-entries-cron.php';
}

private function init_hooks() {
private function init_hooks()
{
add_action('admin_menu', ['AIEntries_Settings', 'add_menu_page']);
add_action('AIEntries_daily_cron_job', ['AIEntries_Cron', 'daily_task']);
add_action('wp', ['AIEntries_Cron', 'check_six_hour_function']);
add_action('AIEntries_daily_cron_job', ['AIEntries_Cron', 'daily_task']);
}

public static function activate() {
if (!wp_next_scheduled('AIEntries_daily_cron_job')) {
wp_schedule_event(time(), 'daily', 'AIEntries_daily_cron_job');
}
}

public static function deactivate() {
public static function deactivate()
{
$timestamp = wp_next_scheduled('AIEntries_daily_cron_job');
wp_unschedule_event($timestamp, 'AIEntries_daily_cron_job');
}
Expand Down
12 changes: 10 additions & 2 deletions ai-entries/includes/settings-page.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?php
require_once dirname(__FILE__) . '/class-ai-entries-cron.php';
?>
<div class="wrap">
<h2>AIEntries Settings</h2>


<p>This plugin runs once a day according to the following parameters:</p>

<form method="post" action="">
<?php wp_nonce_field('aic_entries_settings_nonce', 'aic_entries_nonce');?>
<label for="question">
Expand Down Expand Up @@ -43,7 +47,11 @@
<?php foreach ($responses as $response): ?>
<pre><a href="<?php echo esc_html(get_post_permalink($response->ID)); ($response->ID); ?>" target="_blank"><?php echo esc_html(get_the_title($response->ID)); ?></a></pre>
<?php endforeach;?>
<?php endif;?>
<?php endif;?>
<p style="color: red;"><b>DISCLAIMER: this is a work in progress. The quantity of posts created by this plugin depends on your API key limitations</b></p>
<p><a target="_blank" href="https://github.com/berchj/AIEntries">mantain and scale this plugin</a></p>
<h3>Wordpress Cron tasks scheduled by this plugin:</h3>
<?php
echo esc_html(AIEntries_Cron::show_all_cron_tasks());
?>
</div>
Loading