I’m trying to make a plugin so that you can show events that have been made on a different site in your own WordPress site and I’m getting a fatal error and don’t know how to go further now.
Fatal error: Uncaught ArgumentCountError: Too few arguments to function Spond::__construct(), 0 passed in C:\wamp64\www\stage\wordpress wpm\wp-content\plugins\spond-plugin\spond-plugin.php on line 122 and exactly 2 expected in C:\wamp64\www\stage\wordpress wpm\wp-content\plugins\spond-plugin\s
This is my code:
<?php
/*
Plugin Name: Spond Event
Description: Display Spond events on your WordPress site.
Version: 1.0
Author: Merel Rose de Vries
*/
// spond-plugin.php
session_start();
include_once(plugin_dir_path(__FILE__) . 'spond-class.php');
function spond_shortcode($atts) {
// Check if the form is submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['logout']) && $_POST['logout'] == 1) {
// Logout action: unset the session variable
unset($_SESSION['spond_logged_in']);
return 'Logged out successfully.';
}
$username = isset($_POST['username']) ? sanitize_text_field($_POST['username']) : '';
$password = isset($_POST['password']) ? sanitize_text_field($_POST['password']) : '';
// Assuming the form is submitted, redirect with form data as query parameters
wp_redirect(add_query_arg(array('username' => $username, 'password' => $password), get_permalink()));
exit();
}
// If there are query parameters, attempt to log in
if (isset($_GET['username']) && isset($_GET['password'])) {
$username = sanitize_text_field($_GET['username']);
$password = sanitize_text_field($_GET['password']);
// Your Spond class instantiation
$spond = new Spond($username, $password);
$spond->login();
// Set a session variable to indicate the user is logged in
$_SESSION['spond_logged_in'] = true;
// Get and display events
ob_start();
$spond->get_events('74ED14831B694B679A0CB778A33D860F');
return ob_get_clean();
}
// Check if the user has already logged in
if (isset($_SESSION['spond_logged_in']) && $_SESSION['spond_logged_in']) {
// User has already logged in, keep the session active
return display_events();
}
// User is not logged in, display the login form
ob_start();
your_plugin_page();
return ob_get_clean();
}
// Register the shortcode
add_shortcode('spond_events', 'spond_shortcode');
// Add menu item
add_action('admin_menu', 'spondplugin');
function spondplugin() {
add_menu_page(
'Spond-plugin',
'Spond-plugin',
'manage_options',
'your_plugin_menu',
'your_plugin_page'
);
add_submenu_page(
'your_plugin_menu',
'Submenu Page',
'Submenu Page',
'manage_options',
'your-submenu-page',
'your_submenu_page'
);
}
// Create plugin page content
function your_plugin_page() {
echo '<div class="wrap">';
echo '<h1>Spond Evenementen</h1>';
echo '</div>';
// Check if the user is logged in
if (isset($_SESSION['spond_logged_in']) && $_SESSION['spond_logged_in']) {
// Display logout button
echo '<form action="" method="post">';
echo '<input type="hidden" name="logout" value="1">';
echo '<button type="submit">Logout</button>';
echo '</form>';
// Display events
display_events();
} else {
// Display the login form
echo '<form action="" method="post">';
echo '<div>';
echo '<div class="container">';
echo '<label for="username"><b>Username</b></label>';
echo '<input type="text" placeholder="Enter Username" name="username" required>';
echo '<label for="password"><b>Password</b></label>';
echo '<input type="password" placeholder="Enter Password" name="password" required>';
echo '<button type="submit">fetch</button>';
echo '</div>';
echo '</form>';
}
}
// ...
// Display events function
function display_events() {
ob_start();
$spond = new Spond(); // Instantiate with default username and password
$spond->get_events('74ED14831B694B679A0CB778A33D860F');
echo ob_get_clean();
}
?>
<?php
// spond-class.php
echo '<style>';
include "style.css";
echo '</style>';
require_once plugin_dir_path(__FILE__) . 'vendor/autoload.php';
class Spond {
private $username;
private $password;
private $api_url = "https://api.spond.com/core/v1/";
private $token;
private $groups;
private $clientsession;
private $accessToken;
private $auth;
private $auth_headers;
public function __construct($username, $password) {
$this->username = $username;
$this->password = $password;
$this->clientsession = new \GuzzleHttp\Client([
'verify' => false,
]);
}
public function authHeaders() {
return [
"content-type" => "application/json",
"Authorization" => "Bearer " . $this->accessToken,
"auth" => $this->auth,
];
}
public function login() {
$login_url = $this->api_url . "login";
$data = ["email" => $this->username, "password" => $this->password];
$options = [
'http' => [
'header' => "Content-type: application/json",
'method' => 'POST',
'content' => json_encode($data),
],
];
$context = stream_context_create($options);
$response = file_get_contents($login_url, false, $context);
$login_result = json_decode($response, true);
$this->accessToken = $login_result["loginToken"]; // Assuming loginToken is the access token
return $this;
}
public function getGroups() {
if (!$this->token) {
$this->login();
}
$url = $this->api_url . "groups/";
$response = $this->clientsession->get($url, ["headers" => $this->authHeaders()]);
$this->groups = json_decode($response->getBody(), true);
return $this->groups;
}
public function getGroup($uid) {
if (!$this->token) {
$this->login();
}
if (!$this->groups) {
$this->getGroups();
}
foreach ($this->groups as $group) {
if ($group["id"] == $uid) {
return $group;
}
}
throw new Exception("IndexError");
}
private function flattenArray($array) {
$result="";
foreach ($array as $key => $value) {
if (is_array($value)) {
//Flatten nested arrays
$result .= $this->flattenArray($value);
} else {
$result .= "$key: $value, ";
}
}
return rtrim($result, ', ');
}
public function get_events($group_id) {
if (!$this->token) {
$this->login();
}
$url = $this->api_url . "sponds/?max=100&groupId=" . $group_id;
$headers = $this->auth_headers;
$response = $this->clientsession->get($url, ["headers" => $this->authHeaders()]);
$events = json_decode($response->getBody(), true);
// Check if events array is not empty
if (!empty($events)) {
echo "<table><th>Evenementen</th>";
foreach ($events as $event) {
echo "<tr>";
echo "<td>" . (isset($event['heading']) ? $event['heading'] : 'N/A') . "</td>";
// convert to date
$startTimestamp = isset($event['startTimestamp']) ? $event['startTimestamp'] : 'N/A';
$startTimestamp = $this->date($startTimestamp);
echo "<td>$startTimestamp</td>";
// Convert to hour & minutes
$startTimestamp = isset($event['startTimestamp']) ? $event['startTimestamp'] : 'N/A';
$startTimestamp = $this->convertTimestamp2($startTimestamp);
echo "<td>$startTimestamp</td>";
// Convert to hour & minutes
$endTimestamp = isset($event['endTimestamp']) ? $event['endTimestamp'] : 'N/A';
$endTimestamp = $this->convertTimestamp2($endTimestamp);
echo "<td>$endTimestamp</td>";
echo "</tr>";
}
echo "</table>";
}
}
private function date($timestamp) {
$dateTime = new DateTime($timestamp);
return $dateTime->format('Y-m-d');
}
private function convertTimestamp2($timestamp) {
$dateTime = new DateTime($timestamp);
return $dateTime->format('H:i');
}
}
?>
$spond = new Spond(); // Instantiate with default username and password
doesn’t match your class constructor. Either adapt this line of code, or adapt your constructor.Thanks for the help its sort of fixed now