Files

65 lines
3.2 KiB
PHP

<?php
/**
* Batch 33 — LifterLMS tools. Net-new, GPL-2.0-or-later. See NOTICE.md.
* Inert unless LifterLMS (LLMS_VERSION) is active. Courses = 'course' CPT, memberships = 'llms_membership'.
*/
namespace AskTamMCP\Extensions;
defined('ABSPATH') || exit;
class Batch33 {
public static function register($registry): void {
if (!defined('LLMS_VERSION')) return;
$def = function (string $name, string $desc, array $props, array $required, string $scope, string $method) {
$schema = ['type' => 'object', 'properties' => $props ?: new \stdClass()];
if ($required) $schema['required'] = $required;
return ['name' => $name, 'description' => $desc, 'scope' => $scope,
'handler' => 'AskTamMCP\\Extensions\\Batch33::' . $method, 'inputSchema' => $schema];
};
$registry->register($def('llms_detect', 'Report whether LifterLMS is active and its version.', [], [], 'read', 'detect'));
$registry->register($def('llms_list_courses', 'List LifterLMS courses (id, title, status).',
['per_page' => ['type' => 'integer', 'default' => 50]], [], 'read', 'listCourses'));
$registry->register($def('llms_get_course', 'Get one LifterLMS course: title and price.',
['course_id' => ['type' => 'integer']], ['course_id'], 'read', 'getCourse'));
$registry->register($def('llms_list_memberships', 'List LifterLMS memberships (id, title).',
['per_page' => ['type' => 'integer', 'default' => 50]], [], 'read', 'listMemberships'));
}
public static function detect(array $args): array {
return ['active' => defined('LLMS_VERSION'), 'version' => defined('LLMS_VERSION') ? LLMS_VERSION : null];
}
public static function listCourses(array $args): array {
$posts = get_posts(['post_type' => 'course', 'post_status' => ['publish', 'draft'],
'numberposts' => (int) ($args['per_page'] ?? 50), 'orderby' => 'title', 'order' => 'ASC']);
$out = [];
foreach ($posts as $p) $out[] = ['id' => $p->ID, 'title' => $p->post_title, 'status' => $p->post_status];
return ['count' => count($out), 'courses' => $out];
}
public static function getCourse(array $args): array {
$id = (int) ($args['course_id'] ?? 0);
$p = get_post($id);
if (!$p || $p->post_type !== 'course') throw new \RuntimeException("LifterLMS course not found: {$id}");
$price = null;
if (function_exists('llms_get_post')) {
$c = llms_get_post($id);
if (is_object($c) && method_exists($c, 'get_price')) $price = $c->get_price();
}
if ($price === null) $price = get_post_meta($id, '_llms_price', true);
return ['id' => $id, 'title' => $p->post_title, 'status' => $p->post_status, 'price' => $price];
}
public static function listMemberships(array $args): array {
$posts = get_posts(['post_type' => 'llms_membership', 'post_status' => ['publish', 'draft'],
'numberposts' => (int) ($args['per_page'] ?? 50), 'orderby' => 'title', 'order' => 'ASC']);
$out = [];
foreach ($posts as $p) $out[] = ['id' => $p->ID, 'title' => $p->post_title];
return ['count' => count($out), 'memberships' => $out];
}
}