Automattic\WooCommerce\Admin\API\Reports\Export

Controller{}WC 1.0

Reports Export controller.

Хуки из класса

Использование

$Controller = new Controller();
// use class methods

Методы

  1. ERROR: no method name found on line `/**`
  2. ERROR: no method name found on line `'methods' => \WP_REST_Server::EDITABLE,`
  3. public array( $this, 'get_items_permissions_check' )
  4. ERROR: no method name found on line `),`
  5. public register_rest_route(
  6. public __( 'Sorry, there is no export with that ID.', 'woocommerce' )
  7. ERROR: no method name found on line ``
  8. ERROR: no method name found on line `}`
  9. ERROR: no method name found on line ``
  10. ERROR: no method name found on line ``
  11. ERROR: no method name found on line `),`
  12. ERROR: no method name found on line ``
  13. public empty( $request['report_args'] ) ? array()
  14. public export_items( $request )
  15. public export_status( $request )
  16. ERROR: no method name found on line ``
  17. ERROR: no method name found on line ``
  18. ERROR: no method name found on line ``
  19. ERROR: no method name found on line ``
  20. ERROR: no method name found on line ``
  21. ERROR: no method name found on line ``
  22. ERROR: no method name found on line ``
  23. ERROR: no method name found on line ``
  24. ERROR: no method name found on line `'export_id' => $export_id,`
  25. ERROR: no method name found on line ``
  26. ERROR: no method name found on line ``
  27. ERROR: no method name found on line ``
  28. ERROR: no method name found on line `'action' => ReportExporter::DOWNLOAD_EXPORT_ACTION,`
  29. ERROR: no method name found on line `);`
  30. ERROR: no method name found on line `'type' => 'object',`
  31. ERROR: no method name found on line ``
  32. ERROR: no method name found on line ``
  33. ERROR: no method name found on line `);`
  34. ERROR: no method name found on line ``
  35. ERROR: no method name found on line `}`
  36. ERROR: no method name found on line ``
  37. ERROR: no method name found on line ``
  38. ERROR: no method name found on line ``
  39. ERROR: no method name found on line ``
  40. ERROR: no method name found on line ``
  41. public get_export_collection_params()
  42. ERROR: no method name found on line ``
  43. ERROR: no method name found on line ``
  44. ERROR: no method name found on line ``
  45. ERROR: no method name found on line ``
  46. ERROR: no method name found on line ``
  47. ERROR: no method name found on line ``
  48. ERROR: no method name found on line ``
  49. ERROR: no method name found on line ``
  50. ERROR: no method name found on line ``
  51. ERROR: no method name found on line ``
  52. ERROR: no method name found on line `}`
  53. public get_export_public_schema()
  54. public get_export_status_public_schema()
  55. ERROR: no method name found on line `);`
  56. ERROR: no method name found on line `$export_id = $request['export_id'];`
  57. ERROR: no method name found on line ``
  58. ERROR: no method name found on line ``
  59. ERROR: no method name found on line ``
  60. public if ( 0 === $total_rows )
  61. ERROR: no method name found on line ``
  62. ERROR: no method name found on line `'percent_complete' => $percentage,`
  63. public sanitize_file_name( $export_id )
  64. ERROR: no method name found on line `*`
  65. ERROR: no method name found on line ``
  66. ERROR: no method name found on line `* @return WP_Error|WP_REST_Response`
  67. ERROR: no method name found on line ``
  68. public isset( $request['email'] )
  69. ERROR: no method name found on line `$report_type = $request['type'];`
  70. ERROR: no method name found on line ``
  71. ERROR: no method name found on line ``
  72. ERROR: no method name found on line `'type' => 'string',`
  73. ERROR: no method name found on line `'type' => 'string',`
  74. ERROR: no method name found on line ``
  75. ERROR: no method name found on line ``
  76. ERROR: no method name found on line `'type' => 'boolean',`
  77. ERROR: no method name found on line `'type' => 'object',`
  78. ERROR: no method name found on line `'validate_callback' => 'rest_validate_request_arg',`
  79. ERROR: no method name found on line `*`
  80. ERROR: no method name found on line ``
  81. ERROR: no method name found on line `}`
  82. ERROR: no method name found on line ``
  83. ERROR: no method name found on line `'methods' => \WP_REST_Server::READABLE,`
  84. public add_additional_fields_schema( $schema )
  85. public register_routes()
  86. ERROR: no method name found on line `);`
  87. ERROR: no method name found on line `);`
  88. ERROR: no method name found on line `}`
  89. ERROR: no method name found on line `$this->namespace,`
  90. ERROR: no method name found on line `* Get the query params for collections.`
  91. ERROR: no method name found on line `);`
  92. ERROR: no method name found on line `*`
  93. ERROR: no method name found on line `* @return array`
  94. public rest_url( sprintf( '%s/reports/%s/export/%s/status', $this->namespace, $report_type, $export_id ) )
  95. ERROR: no method name found on line ``
  96. ERROR: no method name found on line ``
  97. ERROR: no method name found on line ``
  98. ERROR: no method name found on line ``
  99. ERROR: no method name found on line ``
  100. ERROR: no method name found on line ``
  101. ERROR: no method name found on line ``
  102. ERROR: no method name found on line ``
  103. ERROR: no method name found on line `);`
  104. ERROR: no method name found on line `}`
  105. public queue_report_export( $export_id, $report_type, $report_args, $send_email )
  106. ERROR: no method name found on line `}`
  107. ERROR: no method name found on line `}`
  108. public get_export_percentage_complete( $report_type, $export_id )
  109. ERROR: no method name found on line ``
  110. ERROR: no method name found on line `)`
  111. ERROR: no method name found on line ``
  112. ERROR: no method name found on line ``
  113. ERROR: no method name found on line ``
  114. ERROR: no method name found on line ``
  115. ERROR: no method name found on line ``
  116. ERROR: no method name found on line ``
  117. ERROR: no method name found on line ``
  118. ERROR: no method name found on line ``
  119. ERROR: no method name found on line ``
  120. ERROR: no method name found on line ``
  121. ERROR: no method name found on line ``
  122. public add_links(
  123. ERROR: no method name found on line `'type' => 'string',`
  124. ERROR: no method name found on line ``
  125. ERROR: no method name found on line `'readonly' => true,`
  126. ERROR: no method name found on line `),`
  127. public apply_filters( 'woocommerce_admin_export_id', $default_export_id )
  128. ERROR: no method name found on line `'readonly' => true,`
  129. ERROR: no method name found on line `),`
  130. ERROR: no method name found on line `$this->namespace,`
  131. ERROR: no method name found on line `* Get the Export status schema, conforming to JSON Schema.`
  132. ERROR: no method name found on line `),`
  133. ERROR: no method name found on line `*/`
  134. ERROR: no method name found on line `'readonly' => true,`
  135. ERROR: no method name found on line `),`
  136. ERROR: no method name found on line `'readonly' => true,`
  137. ERROR: no method name found on line ``
  138. ERROR: no method name found on line ``
  139. ERROR: no method name found on line ``
  140. ERROR: no method name found on line `/**`
  141. ERROR: no method name found on line `),`
  142. ERROR: no method name found on line `* Export data based on user request params.`
  143. ERROR: no method name found on line `),`
  144. ERROR: no method name found on line ``
  145. ERROR: no method name found on line `'type' => 'int',`
  146. ERROR: no method name found on line `$this->namespace,`
  147. ERROR: no method name found on line `/**`
  148. ERROR: no method name found on line `'/' . $this->rest_base,`
  149. ERROR: no method name found on line `$this->namespace,`
  150. ERROR: no method name found on line `'/' . $this->rest_base,`
  151. ERROR: no method name found on line `$this->namespace,`
  152. ERROR: no method name found on line `*`
  153. ERROR: no method name found on line `'/' . $this->rest_base,`
  154. ERROR: no method name found on line ``
  155. ERROR: no method name found on line `* Register routes.`
  156. ERROR: no method name found on line `/**`
  157. ERROR: no method name found on line `'$schema' => 'http://json-schema.org/draft-04/schema#',`
  158. ERROR: no method name found on line `$report_type = $request['type'];`
  159. ERROR: no method name found on line ``
  160. ERROR: no method name found on line ``
  161. ERROR: no method name found on line ``

Код Controller{} WC 6.9.4

class Controller extends \Automattic\WooCommerce\Admin\API\Reports\Controller {
	/**
	 * Endpoint namespace.
	 *
	 * @var string
	 */
	protected $namespace = 'wc-analytics';

	/**
	 * Route base.
	 *
	 * @var string
	 */
	protected $rest_base = 'reports/(?P<type>[a-z]+)/export';

	/**
	 * Register routes.
	 */
	public function register_routes() {
		register_rest_route(
			$this->namespace,
			'/' . $this->rest_base,
			array(
				array(
					'methods'             => \WP_REST_Server::EDITABLE,
					'callback'            => array( $this, 'export_items' ),
					'permission_callback' => array( $this, 'get_items_permissions_check' ),
					'args'                => $this->get_export_collection_params(),
				),
				'schema' => array( $this, 'get_export_public_schema' ),
			)
		);

		register_rest_route(
			$this->namespace,
			'/' . $this->rest_base . '/(?P<export_id>[a-z0-9]+)/status',
			array(
				array(
					'methods'             => \WP_REST_Server::READABLE,
					'callback'            => array( $this, 'export_status' ),
					'permission_callback' => array( $this, 'get_items_permissions_check' ),
				),
				'schema' => array( $this, 'get_export_status_public_schema' ),
			)
		);
	}

	/**
	 * Get the query params for collections.
	 *
	 * @return array
	 */
	protected function get_export_collection_params() {
		$params                = array();
		$params['report_args'] = array(
			'description'       => __( 'Parameters to pass on to the exported report.', 'woocommerce' ),
			'type'              => 'object',
			'validate_callback' => 'rest_validate_request_arg', // @todo: use each controller's schema?
		);
		$params['email']       = array(
			'description'       => __( 'When true, email a link to download the export to the requesting user.', 'woocommerce' ),
			'type'              => 'boolean',
			'validate_callback' => 'rest_validate_request_arg',
		);
		return $params;
	}

	/**
	 * Get the Report Export's schema, conforming to JSON Schema.
	 *
	 * @return array
	 */
	public function get_export_public_schema() {
		$schema = array(
			'$schema'    => 'http://json-schema.org/draft-04/schema#',
			'title'      => 'report_export',
			'type'       => 'object',
			'properties' => array(
				'status'    => array(
					'description' => __( 'Export status.', 'woocommerce' ),
					'type'        => 'string',
					'context'     => array( 'view', 'edit' ),
					'readonly'    => true,
				),
				'message'   => array(
					'description' => __( 'Export status message.', 'woocommerce' ),
					'type'        => 'string',
					'context'     => array( 'view', 'edit' ),
					'readonly'    => true,
				),
				'export_id' => array(
					'description' => __( 'Export ID.', 'woocommerce' ),
					'type'        => 'string',
					'context'     => array( 'view', 'edit' ),
					'readonly'    => true,
				),
			),
		);

		return $this->add_additional_fields_schema( $schema );
	}

	/**
	 * Get the Export status schema, conforming to JSON Schema.
	 *
	 * @return array
	 */
	public function get_export_status_public_schema() {
		$schema = array(
			'$schema'    => 'http://json-schema.org/draft-04/schema#',
			'title'      => 'report_export_status',
			'type'       => 'object',
			'properties' => array(
				'percent_complete' => array(
					'description' => __( 'Percentage complete.', 'woocommerce' ),
					'type'        => 'int',
					'context'     => array( 'view', 'edit' ),
					'readonly'    => true,
				),
				'download_url'     => array(
					'description' => __( 'Export download URL.', 'woocommerce' ),
					'type'        => 'string',
					'context'     => array( 'view', 'edit' ),
					'readonly'    => true,
				),
			),
		);

		return $this->add_additional_fields_schema( $schema );
	}

	/**
	 * Export data based on user request params.
	 *
	 * @param  WP_REST_Request $request Request data.
	 * @return WP_Error|WP_REST_Response
	 */
	public function export_items( $request ) {
		$report_type = $request['type'];
		$report_args = empty( $request['report_args'] ) ? array() : $request['report_args'];
		$send_email  = isset( $request['email'] ) ? $request['email'] : false;

		$default_export_id = str_replace( '.', '', microtime( true ) );
		$export_id         = apply_filters( 'woocommerce_admin_export_id', $default_export_id );
		$export_id         = (string) sanitize_file_name( $export_id );

		$total_rows = ReportExporter::queue_report_export( $export_id, $report_type, $report_args, $send_email );

		if ( 0 === $total_rows ) {
			return rest_ensure_response(
				array(
					'message' => __( 'There is no data to export for the given request.', 'woocommerce' ),
				)
			);
		}

		ReportExporter::update_export_percentage_complete( $report_type, $export_id, 0 );

		$response = rest_ensure_response(
			array(
				'message'   => __( 'Your report file is being generated.', 'woocommerce' ),
				'export_id' => $export_id,
			)
		);

		// Include a link to the export status endpoint.
		$response->add_links(
			array(
				'status' => array(
					'href' => rest_url( sprintf( '%s/reports/%s/export/%s/status', $this->namespace, $report_type, $export_id ) ),
				),
			)
		);

		$data = $this->prepare_response_for_collection( $response );

		return rest_ensure_response( $data );
	}

	/**
	 * Export status based on user request params.
	 *
	 * @param  WP_REST_Request $request Request data.
	 * @return WP_Error|WP_REST_Response
	 */
	public function export_status( $request ) {
		$report_type = $request['type'];
		$export_id   = $request['export_id'];
		$percentage  = ReportExporter::get_export_percentage_complete( $report_type, $export_id );

		if ( false === $percentage ) {
			return new \WP_Error(
				'woocommerce_admin_reports_export_invalid_id',
				__( 'Sorry, there is no export with that ID.', 'woocommerce' ),
				array( 'status' => 404 )
			);
		}

		$result = array(
			'percent_complete' => $percentage,
		);

		// @todo - add thing in the links below instead?
		if ( 100 === $percentage ) {
			$query_args = array(
				'action'   => ReportExporter::DOWNLOAD_EXPORT_ACTION,
				'filename' => "wc-{$report_type}-report-export-{$export_id}",
			);

			$result['download_url'] = add_query_arg( $query_args, admin_url() );
		}

		// Wrap the data in a response object.
		$response = rest_ensure_response( $result );
		// Include a link to the export status endpoint.
		$response->add_links(
			array(
				'self' => array(
					'href' => rest_url( sprintf( '%s/reports/%s/export/%s/status', $this->namespace, $report_type, $export_id ) ),
				),
			)
		);

		$data = $this->prepare_response_for_collection( $response );

		return rest_ensure_response( $data );
	}
}