<?php

/**
 * Management of the Lockee shortcode
 */
class Lockee_Shortcode extends Lockee_Singleton
{
  /**
   * Init the Shortcode functionnality
   */
  protected function init() {}

  /**
   * Shortcode handler
   */
  public function shortcode($atts)
  {
    $atts = shortcode_atts(['id' => -1], $atts);
    $id = intval($atts['id']);

    if ($id <= -1) {
      return '<p>' . esc_html__("Lock not found. Please check the shortcode used.", "lockee") . '</p>';
    }

    $lockee_type = get_post_meta($id, 'lockee_type', true);
    $lockee_code = get_post_meta($id, 'lockee_code', true);
    $lockee_options = get_post_meta($id, 'lockee_options', true);

    if (empty($lockee_type) || empty($lockee_code)) {
      return '<p>' . esc_html__("Lock not found. Please check the shortcode used.", "lockee") . '</p>';
    }

    return Lockee_Utils::generateLock('open', $id, $lockee_type, '', $lockee_options);
  }
}
