Please let me know if you cannot access the link above and I will provide more context and detail.
Thanks in advance.
Bruce
A content editor for Joomla!®
You need to be logged in to post in the forum - Log In
An active JCE Pro Subscription is required to post in the forum - Buy a Subscription
Please create a new Ticket and we will get back to you as soon as we can.
Everybody will be able to see its contents. Do not include usernames, passwords or any other sensitive information.
Latest post by Ryan on Friday, 15 September 2023 19:36 BST
<?xml version="1.0" encoding="utf-8"?>
<extension version="4.0" type="plugin" group="task" method="upgrade">
<name>plg_task_testarticlepublish</name>
<author>Author Name</author>
<creationDate>2023-09-15</creationDate>
<version>1.0.0</version>
<description>Description of the plugin</description>
<files>
<filename plugin="testarticlepublish">testarticlepublish.php</filename>
</files>
</extension>
b. Plugin File: testarticlepublish.php
<?php
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\CMSPlugin;
class PlgTaskTestarticlepublish extends CMSPlugin
{
public function onTaskExecute($taskContext, $params)
{
// Ensure this task is for us
if ($taskContext !== 'plg_task_testarticlepublish')
{
return;
}
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select('*')
->from($db->quoteName('#__content'))
->where($db->quoteName('state') . ' = 0') // Unpublished
->where($db->quoteName('title') . ' LIKE ' . $db->quote('%test%')) // Title contains "test"
->where($db->quoteName('publish_up') . ' > ' . $db->quote(Factory::getDate()->toSql())) // Publish date in the future
->order('created ASC')
->setLimit(1);
$article = $db->setQuery($query)->loadObject();
if ($article)
{
$article->state = 1; // Set to published
$db->updateObject('#__content', $article, 'id');
// Trigger the onContentAfterSave event
Factory::getApplication()->triggerEvent('onContentAfterSave', ['com_content.article', &$article, false]);
}
}
}
Perhaps you might find this useful?
Ryan Demmer
Lead Developer / CEO / CTO
Just because you're not paranoid doesn't mean everybody isn't out to get you.
<?php
// We are a valid entry point.
const _JEXEC = 1;
if (!defined('JPATH_CLI'))
{
define('JPATH_CLI', __DIR__);
}
require dirname(__DIR__) . '/includes/defines.php';
require dirname(__DIR__) . '/includes/framework.php';
use Joomla\CMS\Factory;
use Joomla\CMS\Application\CliApplication;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class PublishTestArticleCommand extends Command
{
protected static $defaultName = 'article:publish-test';
protected function configure()
{
$this->setDescription('Publish the oldest unpublished article with "test" in the title and future publish date.');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select('*')
->from($db->quoteName('#__content'))
->where($db->quoteName('state') . ' = 0') // Unpublished
->where($db->quoteName('title') . ' LIKE ' . $db->quote('%test%')) // Title contains "test"
->where($db->quoteName('publish_up') . ' > ' . $db->quote(Factory::getDate()->toSql())) // Publish date in the future
->order('created ASC')
->setLimit(1);
$article = $db->setQuery($query)->loadObject();
if ($article)
{
$article->state = 1; // Set to published
$db->updateObject('#__content', $article, 'id');
// Use the new event dispatching system
$dispatcher = Factory::getContainer()->get('dispatcher');
$event = new \Joomla\CMS\Event\ContentAfterSave('onContentAfterSave', ['context' => 'com_content.article', 'item' => $article, 'isNew' => false]);
$dispatcher->dispatch('onContentAfterSave', $event);
$output->writeln('<info>Article published successfully!</info>');
}
else
{
$output->writeln('<comment>No matching article found.</comment>');
}
return Command::SUCCESS;
}
}
// Create the application.
$cli = CliApplication::getInstance('PublishTestArticleCommand');
$cli->addCommand(new PublishTestArticleCommand);
$cli->execute();
Ryan Demmer
Lead Developer / CEO / CTO
Just because you're not paranoid doesn't mean everybody isn't out to get you.
Interesting approach. I have not tried ChatGPT for anything serious yet. Interestingly, the two examples above are just variations of what I've been doing all along. The CLI example does add a command, but I don't think the ->execute method executes the command. I think it executes adding the command to the application. Both examples more or less confirm that my code is on the right track.
Ryan Demmer
Lead Developer / CEO / CTO
Just because you're not paranoid doesn't mean everybody isn't out to get you.
Ryan Demmer
Lead Developer / CEO / CTO
Just because you're not paranoid doesn't mean everybody isn't out to get you.
Your post is being submitted…